php - 我应该如何开始使用 PHPUnit 作为我已经制作的一堆函数和类的测试框架?

标签 php testing phpunit

我已经阅读了文档。基本上,我在遵循 BankAccount 示例的同时尝试了测试。 但是我得到错误:

Warning: require_once(PHP/CodeCoverage/Filter.php) [function.require-once]: failed to open stream: No such file or directory in [...]/unitTest/phpunit.php on line 38

此外,PHP 脚本似乎以 #!/usr/bin/env php 开头,这表明它们应该从控制台运行。我宁愿从浏览器运行这些...

假设我有一个返回字符串的函数f1()。应该如何进行测试?我错过了什么吗?

最佳答案

A short introduction to the test framework

PHPUnit 提供了一个简单的框架,用于创建测试套件以自动测试函数和类。 PHPUnit 受到 JUnit 的启发,JUnit 由 Kent Beck 和 Erich Gamma 创建,作为极限编程的工具。 XP 的规则之一是尽可能早地测试小型软件组件,这样您就不必在设置和测试依赖于该类的大型应用程序时修复 API 中的错误和错误。虽然单元测试是 XP 中的基本规则之一,但您不必切换到 XP 就可以从 PHPUnit 中获益。 PHPUnit 独立作为测试类或一组函数的好工具,将简化您的开发周期并帮助您避免无休止的调试 session 。

Work routine

通常,您会编写一个类,使用 echo()var_dump() 进行一些非系统测试。在此之后,您在应用程序中使用该类并希望一切正常。要从 PHPUnit 中获益,您应该重新考虑流程。最好的方法是这样做:

  1. 设计你的类/API
  2. 创建测试套件
  3. 实现类/API
  4. 运行测试套件
  5. 修复失败或错误并再次转到#4

这似乎需要很多时间,但这种印象是错误的。使用 PHPUnit 创建测试套件只需几分钟,运行测试套件只需几秒钟。

Design a class

让我们从一个小例子开始:一个字符串类。首先我们创建一堆函数声明来处理字符串:

---- string.php ----

<?php
class String
{
    //contains the internal data
    var $data;

    // constructor
    function String($data) {
        $this->data = $data;
    }

    // creates a deep copy of the string object
    function copy() {
    }

    // adds another string object to this class
    function add($string) {
    }

    // returns the formated string
    function toString($format) {
    }
}
?>

Creating test suite

现在我们可以创建一个测试套件,它会检查字符串类的每个函数。测试套件是从 PHPUnit_TestCase 继承的普通 PHP 类,包含测试函数,由函数名称中的前导“测试”标识。在测试函数中,必须将预期值与要测试的函数的结果进行比较。此比较的结果必须委托(delegate)给 assert*()-family 的函数,该函数决定函数是否通过测试。

---- testcase.php ----

<?php

require_once 'string.php';
require_once 'PHPUnit.php';

class StringTest extends PHPUnit_TestCase
{
    // contains the object handle of the string class
    var $abc;

    // constructor of the test suite
    function StringTest($name) {
       $this->PHPUnit_TestCase($name);
    }

    // called before the test functions will be executed
    // this function is defined in PHPUnit_TestCase and overwritten
    // here
    function setUp() {
        // create a new instance of String with the
        // string 'abc'
        $this->abc = new String("abc");
    }

    // called after the test functions are executed
    // this function is defined in PHPUnit_TestCase and overwritten
    // here
    function tearDown() {
        // delete your instance
        unset($this->abc);
    }

    // test the toString function
    function testToString() {
        $result = $this->abc->toString('contains %s');
        $expected = 'contains abc';
        $this->assertTrue($result == $expected);
    }

    // test the copy function
    function testCopy() {
      $abc2 = $this->abc->copy();
      $this->assertEquals($abc2, $this->abc);
    }

    // test the add function
    function testAdd() {
        $abc2 = new String('123');
        $this->abc->add($abc2);
        $result = $this->abc->toString("%s");
        $expected = "abc123";
        $this->assertTrue($result == $expected);
    }
  }
?>

The first test run

现在,我们可以运行第一个测试。确保所有路径都正确,然后执行此 PHP 程序。

---- stringtest.php ----

<?php

require_once 'testcase.php';
require_once 'PHPUnit.php';

$suite  = new PHPUnit_TestSuite("StringTest");
$result = PHPUnit::run($suite);

echo $result -> toString();
?>

如果您从命令行调用此脚本,您将获得以下输出:

TestCase stringtest->testtostring() failed: expected true, actual false
TestCase stringtest->testcopy() failed: expected , actual Object
TestCase stringtest->testadd() failed: expected true, actual false

每个函数都未通过测试,因为您的字符串函数没有返回我们定义的预期值。

如果你想通过浏览器调用脚本,你必须将脚本放在正确的 html 页面中并调用 $result->toHTML() 而不是 $result-> toString()

Implementation

好的,让我们开始实现我们的字符串类。

---- string.php ----

<?php
class String
{
    //contains the internal data
    var $data;

    // constructor
    function String($data) {
        $this->data = $data;
    }

    // creates a deep copy of the string object
    function copy() {
        $ret = new String($this->data);
        return $ret;
    }

    // adds another string object to this class
    function add($string) {
        $this->data = $this->data.$string->toString("%ss");
    }

    // returns the formated string
    function toString($format) {
        $ret = sprintf($format, $this->data);
        return $ret;
    }
}
?>

实现完成,我们可以再次运行测试:

~>
php -f stringtest.php

TestCase stringtest->testtostring() passed
TestCase stringtest->testcopy() passed
TestCase stringtest->testadd() failed: expected true, actual false

噢!上次测试失败!我们打错了。将 string.php 中的第 16 行更改为

<?php
$this->data = $this->data.$string->toString("%s");
?> 

然后再次运行测试:

~>
php -f stringtest.php

TestCase stringtest->testtostring() passed
TestCase stringtest->testcopy() passed
TestCase stringtest->testadd() passed

现在一切正常!

Conclusion

测试三个简单的功能是不是看起来工作量很大?别忘了,这是一个小例子。考虑更大、更复杂的 API,例如商店应用程序中的数据库抽象或购物篮类。 PHPUnit 是检测类实现中错误的出色工具。

您通常会想要重新实现或重构用于多个不同应用程序的大型类。如果没有测试套件,您在依赖于您的类的应用程序之一中破坏某些东西的可能性非常高。多亏了单元测试,您可以为您的类创建一个测试套件,然后重新实现您的类,因为您知道只要新类通过测试,依赖于该类的应用程序就会正常运行。

使用的来源:http://pear.php.net

关于php - 我应该如何开始使用 PHPUnit 作为我已经制作的一堆函数和类的测试框架?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4354353/

相关文章:

php - 拉维尔 4.2 : Test Case Autoloading

php - 发送键来测试 Behat 中的选项卡索引和关闭选项卡(MacOSX、Behat、Chromedriver、Selenium2)

php - mPDF 中的自定义字体不会加载

angularjs - 如何使用 httpBackend 测试 angularJS 服务?

java - 在 JUnit 测试用例中找不到测试方法

PHPUnit/Silverstripe 测试返回 "Couldn' t run query", "Table doesn' t exist"

php - 模拟测试对象的公共(public)方法

php - 如何在 PHP 中打印每个子节点和孙节点旁边的最高级别父节点

php - 需要根据登录的用户名计算列中的行数

java - 有没有办法使用 EasyMock 部分模拟一个对象?