PHPUnit袖珍指南之命令行测试工具

  •   2009-07-31/22:51
  • PHPUnit命令行测试工具是通过phpunit命令调用的。如下代码显示如何通过PHPUnit命令行测试工具运行测试。

    phpunit ArrayTest
    PHPUnit 2.3.0 by Sebastian Bergmann.

    Time: 0.067288

    OK (2 tests)

    对每个测试,PHPUnit命令行测试工具打印一个字符表示进程:

    ·测试成功打印“.”。

    ·运行测试方法是发生了断言失败打印“F”。

    ·运行测试方法是发生了错误打印“E”。

    ·测试没有完成或测试没有实现打印“I”(见本书后“未完成的测试”一章)。

    PHPUnit可以区分失败和错误。一个失败是PHPUnit的断言违例,错误是一个意料外的异常或一个PHP错误。有时候这种差别是有用的,因为错误相比失败更容易修正。如果你有一大串问题列表,最好先解决所有错误,然后看看有没有失败遗留下来。

    让我们看看如下一些代码命令行测试工具的选项:

    phpunit --help
    PHPUnit 2.3.0 by Sebastian Bergmann.

    Usage: phpunit [switches] UnitTest [UnitTest.php]
    --coverage-data <file> Write code-coverage data in raw format to file.
    --coverage-html <file> Write code-coverage data in HTML format to file.
    --coverage-text <file> Write code-coverage data in text format to file.
    --testdox-html <file> Write agile documentation in HTML format to file.
    --testdox-text <file> Write agile documentation in Text format to file.
    --log-xml <file> Log test progress in XML format to file.
    --loader <loader> TestSuiteLoader implementation to use.
    --skeleton Generate skeleton UnitTest class for Unit in Unit.php.
    --wait Waits for a keystroke after each test.
    --help Prints this usage information.
    --version Prints the version and exits.

    phpunit UnitTest

    运行类UnitTest提供的测试,该类应该定义在源文件UnitTest.php中。

    类UnitTest必须继承PHPUnit2_Framework_TestCase类,或是提供了公有静态方法suite,并返回PHPUnit2_ Framework_Test对象的类(例如,类PHPUnit2_Framework_TestSuite的一个实例)

    phpunit UnitTest UnitTest.php

    运行类UnitTest提供的测试,该类要定义在命令指定的源文件(UnitTest.php)中。

    --coverage-data, --coverage-html, and --coverage-text

    控制运行测试的代码覆盖信息的分析和集合(参见本书后代码覆盖分析一节)

    --testdox-html and --testdox-text

    以HTML或普通文本格式生成运行测试的敏捷文档(参见本书后的“测试的其他用途”一章)

    --log-xml

    生成运行测试的XML格式的日志文件。

    下一个例子显示为ArrayTest中的测试生成的XML日志文件。

    <?xml version="1.0" encoding="UTF-8"?>
    <testsuites>
    <testsuite name="ArrayTest" tests="2" failures="0" errors="0" time="0.020026">
    <testcase name="testNewArrayIsEmpty" class="ArrayTest" time="0.014449"/>
    <testcase name="testArrayContainsAnElement" class="ArrayTest" time="0.005577"/>
    </testsuite>
    </testsuites>

    下面的XML日志文件是为名为FailureErrorTest的测试类两个测试生成的,一个是testFailure,一个是testError。这显示了失败和错误是如何分别表示的。

    <?xml version="1.0" encoding="UTF-8"?>
    <testsuites>
    <testsuite name="FailureErrorTest" tests="2" failures="1" errors="1" time="0.013603">
    <testcase name="testFailure" class="FailureErrorTest" time="0.011872">
    <failure message="" type="PHPUnit2_Framework_AssertionFailedError"></failure>
    </testcase>
    <testcase name="testError" class="FailureErrorTest" time="0.001731">
    <error message="" type="Exception"></error>
    </testcase>
    </testsuite>
    </testsuites>
    --loader

    指定将要使用的测试套件加载器。

    标准测试套件加载器会在当前工作目录和PHP的include_path configuration指令定义的路径中寻找源文件。按照PEAR的命名规则,形如Project_Package_Class的类名会映射到的源文件为Project/Package/Class.php。

    --skeleton

    为类Unit(在文件Unit.php中)生成一个名为UnitTest(在文件UnitTest.php中)的测试用例类的框架。对原始类的每个方法,在生成的测试用例类中提供了一个未完成的测试用例(见本书后的“未完成测试”部分)。

    下面的例子显示了如何为一个名为Sample的类生成一个测试类的框架。

    phpunit --skeleton Sample
    PHPUnit 2.3.0 by Sebastian Bergmann.
    Wrote test class skeleton for Sample to
    SampleTest.php.
    phpunit SampleTest
    PHPUnit 2.3.0 by Sebastian Bergmann.
    I
    Time: 0.007268
    There was 1 incomplete test case:
    1) testSampleMethod(SampleTest)
    OK, but incomplete test cases!!!
    Tests run: 1, incomplete test cases: 1.

    当你为现有代码书写测试时,你不得不重复很多相同的代码片断,如:

    public function testSampleMethod( ) {}

    PHPUnit能帮助你分析现有代码,生成测试用例类的框架。

    --wait

    每个测试结束时,等待一次击键。这很有用,特别是你在一个只有测试一直运行在打开的窗口中运行测试时。

    提示 当被测试代码中有PHP语法错误时,文本界面的测试会直接退出,不输出任何错误信息。标准的测试套件加载器会检查测试套件的源文件的PHP语法错误,但是,它不会检查测试套件包含的源文件的语法错误。PHPUnit的未来版本会用在砂箱中PHP解释器类解决这个问题。


    评论 {{userinfo.comments}}

    {{money}}

    {{question.question}}

    A {{question.A}}
    B {{question.B}}
    C {{question.C}}
    D {{question.D}}
    提交

    驱动号 更多