PHPUnit袖珍指南-第五章 命令行测试工具

第五章 命令行测试工具

PHPUnit命令行测试工具是通过phpunit命令调用的。如下代码显示如何通过PHPUnit命令行测试工具运行测试。

phpunit ArrayTest
  
  
PHPUnit 2.3.0 by Sebastian Bergmann.
  
  

  
  
   
    
  
  
Time: 0.067288
  
  

  
  
   
    
  
  
OK (2 tests)
  
  

 

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

u       测试成功打印“.”。

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

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

u       测试没有完成或测试没有实现打印“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

 

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

标准测试套件加载器会在当前工作目录和PHPinclude_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解释器类解决这个问题。

 

--------------------------------------------------------------------------------------------------------------------

原文:

Chapter 5. The Command-Line Test Runner

The PHPUnit command-line test runner is invoked through the phpunit command. The following code shows how to run tests with the PHPUnit command-line test runner:

 

phpunit ArrayTest

PHPUnit 2.3.0 by Sebastian Bergmann.

 

..

 

Time: 0.067288

 

OK (2 tests)

 

 

 

For each test run, the PHPUnit command-line tool prints one character to indicate progress:

 

. Printed when the test succeeds.

F Printed when an assertion fails while running the test method.

E Printed when an error occurs while running the test method.

I Printed when the test is marked as being incomplete or not yet implemented (see "Incomplete Tests," later in this book).

PHPUnit distinguishes between failures and errors. A failure is a violated PHPUnit assertion. An error is an unexpected exception or a PHP error. Sometimes this distinction proves useful because errors tend to be easier to fix than failures. If you have a big list of problems, it's best to tackle the errors first and see if you have any failures left when the errors are all fixed.

 

Let's take a look at the command-line test runner's switches in the following code:

 

 

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

 

Runs the tests that are provided by the class UnitTest. This class is expected to be declared in the UnitTest.php source file.

 

UnitTest must be either a class that inherits from PHPUnit2_Framework_TestCase or a class that provides a public static suite( ) method that returns a PHPUnit2_ Framework_Test object (for example, an instance of the PHPUnit2_Framework_TestSuite class).

 

 

 

phpunit UnitTest UnitTest.php

 

Runs the tests that are provided by the class UnitTest. This class is expected to be declared in the specified source file.

 

 

 

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

 

Controls the collection and analysis of code-coverage information for the tests that are run. (See the section "Code-Coverage Analysis," later in this book.)

 

 

 

--testdox-html and --testdox-text

 

Generates agile documentation in HTML or plain text format for the tests that are run. (See "Other Uses for Tests," later in this book.)

 

 

 

--log-xml

 

Generates a logfile in XML format for the tests run.

 

The following example shows the XML logfile generated for the tests in ArrayTest:

 

    <?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>

 

 

 

The following XML logfile was generated for two tests, testFailure and testError, of a test-case class named FailureErrorTest. It shows how failures and errors are denoted.

 

    <?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

 

Specifies the PHPUnit2_Runner_TestSuiteLoader implementation to use.

 

The standard test-suite loader will look for the source file in the current working directory and in each directory that is specified in PHP's include_path configuration directive. Following the PEAR Naming Conventions, a class name such as Project_Package_Class is mapped to the source file name Project/Package/Class.php.

 

 

 

--skeleton

 

Generates a skeleton test-case class UnitTest (in UnitTest. php) for a class Unit (inUnit.php). For each method in the original class, there will be an incomplete test case (see "Incomplete Tests,"later in this book) in the generated test-case class.

 

The following example shows how to generate a skeleton test class for a class named 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.

 

When you are writing tests for existing code, you have to write the same code fragments over and over again, as in the following example:

    public function testSampleMethod( ) {

    }

PHPUnit can help you by analyzing the existing code and generating a skeleton test-case class for it.

 

--wait

 

Waits for a keystroke after each test. This is useful if you are running the tests in a window that stays open only as long as the test runner is active.

 

TIP

When the tested code contains PHP syntax errors, the TextUI test runner might exit without printing error information. The standard test-suite loader will check the test-suite source file for PHP syntax errors, but it won't check source files included by the test-suite source file. Future versions of PHPUnit will solve this issue by using a sandboxed PHP interpreter.

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值