php - PHP 中的单元测试

标签 php

<分区>

你们用 PHP 进行单元测试吗?我不确定我是否做过……它到底是什么?能举个简单的例子吗?

最佳答案

单元测试

Do you guys do any unit testing in PHP? I'm not sure if I've ever done it... what exactly is it?

Unit testing是只测试一个单元(类)的做法。为了使单元测试发挥作用,应该允许(单独)独立运行并且运行速度非常快,否则您可能不会经常运行测试。

集成测试

你也可以写成integration tests将系统作为一个整体进行测试,大多数时候执行起来要慢得多。这些测试应该写在单元测试之后!

测试驱动

过去,当我更多地(频繁地)使用 PHP 语言编写代码时,我根本没有实践 TDD(测试驱动开发)(回想起来我的代码并没有成功)。但是现在当我不得不用 PHP 编写一些代码时,我真的很喜欢正确地测试(单元测试)我的代码。在我看来,您也应该这样做,因为这会让您对代码的质量深信不疑。

Over the years I have come to describe Test Driven Development in terms of three simple rules. They are:

  1. You are not allowed to write any production code unless it is to make a failing unit test pass.
  2. You are not allowed to write any more of a unit test than is sufficient to fail; and compilation failures are failures.
  3. You are not allowed to write any more production code than is sufficient to pass the one failing unit test.

You must begin by writing a unit test for the functionality that you intend to write. But by rule 2, you can't write very much of that unit test. As soon as the unit test code fails to compile, or fails an assertion, you must stop and write production code. But by rule 3 you can only write the production code that makes the test compile or pass, and no more.

If you think about this you will realize that you simply cannot write very much code at all without compiling and executing something. Indeed, this is really the point. In everything we do, whether writing tests, writing production code, or refactoring, we keep the system executing at all times. The time between running tests is on the order of seconds, or minutes. Even 10 minutes is too long


例子

Can you provide a simple example?

第一个a really simple example to get you started from the phpunit website .

<?php
class StackTest extends PHPUnit_Framework_TestCase
{
    public function testPushAndPop()
    {
        $stack = array();
        $this->assertEquals(0, count($stack));

        array_push($stack, 'foo');
        $this->assertEquals('foo', $stack[count($stack)-1]);
        $this->assertEquals(1, count($stack));

        $this->assertEquals('foo', array_pop($stack));
        $this->assertEquals(0, count($stack));
    }
}
?>

作为一个更详细的示例,我想向您指出 snippet of mine on github .

具有代码覆盖率的 PHPUnit

我喜欢练习一种叫做TDD的东西使用单元测试框架(在 PHP 中为 phpunit )。

我也很喜欢 phpunit是它还提供code coverage通过 xdebug。 从下图中可以看出,我的类(class)有 100% 的测试覆盖率。这意味着我的 Authentication 类中的每一行都已经过测试,这让我相信代码正在执行它应该执行的操作。请记住,覆盖率并不总是意味着您的代码经过了良好的测试。您可以在不测试一行生产代码的情况下实现 100% 的覆盖率。

Netbeans

我个人喜欢在 Netbeans 中测试我的代码(对于 PHP)。只需单击一下 (alt+f6),我就可以测试我的所有代码。这意味着我不必离开我的 IDE,我非常喜欢它并帮助您节省在 session 之间切换的时间。

enter image description here

关于php - PHP 中的单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4839676/

相关文章:

php - 如何使用 ajax 和 json 返回多个数据

php - 如果我在该图像中写入另一个变量,则图像标记在 php html 中不起作用。请找到下面所附的代码

javascript - 结合php、html和js,没有渲染正确的js

php - 是否可以在codeigniter中连接两个dbms MySQL和postgresQL?

javascript - 从表单字段提交数组到数据库

php - 替代地穴()

php - 在 Linux 操作系统上找不到作为运行进程的 PHP 脚本?

php - 值仅传递到单个数据库或条件。使困惑

php - Yii2 - 在事件记录中查找基于最大值的年份

php - 短信网关无法发送短信