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

翻译 2006年05月22日 12:33:00

第五章 命令行测试工具

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.

 

相关文章推荐

PHPUnit袖珍指南-第三章 PHPUnit的目的

第三章PHPUnit的目的到此为止,我们只有两个对Array和内建函数sizeof()的测试。当我们开始测试大量的array_*()函数时,每个都需要一个测试。我们可以每个都从头写起。但是,更好的方法...

PHPUnit袖珍指南 第六章 装置器

第六章 装置器编写测试最耗时的部分是边编写设置整个程序到达一个已知状态,而后在测试结束后返回到原始状态。这个已知状态叫做测试的装置器。在例5中,装置器很简单,只是存储在变量$fixture中的数组。多...

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

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

PHPUnit袖珍指南-第二章 自动测试

第二章 自动测试 最好的程序员也会犯错误。好程序员和差程序员的区别在于:好程序员能通过测试尽可能的发现错误。你越快测试错误,你就越快发现它们,发现和修正的成本就越低。这解释了为什么只在软件发布前才测...

PHPUnit袖珍指南 第十二章 测试的其他用途

一旦你开始写自动测试,你就会想要发掘更多用途。以下是一些例子。   12-1. 敏捷文档 通常来说,在采用敏捷方法作为开发流程的项目中,如极限编程,文档很难和迅速变化的项目设计和代码...

PHPUnit袖珍指南 第八章 未完成测试

第八章 未完成测试 当开始书写新的测试用例类时,你也许想要从空的测试方法开始,譬如:        public function testSomething( ) {        } 我们...

体验PHP下的单元测试工具:PHPUnit

今天接到了个任务,需要对数字进行计算,因为涉及到整数,小数,和科学计数法等很多条件,所以人工测试非常麻烦,于是想到了PHP的单元测试工具PHPUnit,所以写个文档备查。     工作流程如下: ...

PHPUnit袖珍指南 第六章 装置器

第六章 装置器 编写测试最耗时的部分是边编写设置整个程序到达一个已知状态,而后在测试结束后返回到原始状态。这个已知状态叫做测试的装置器。 在例5中,装置器很简单,只是存储在变量$fixture中的...

PHPUnit袖珍指南-第一章 简介

很长时间里,我对“你什么时候会为PHPUnit写一个文档”这个问题的回答是:“你不需要PHPUnit文档,去读读JUnit文档或买一本Junit的书,试一试用PHP和PHPUnit来重写Java和Ju...

PHPUnit袖珍指南 第十章 代码覆盖率分析

第十章 代码覆盖率分析 你已经学会了怎么使用单元测试代码,但你怎么测试你的测试呢?你怎么发现没被测试的代码,换句话说,没被测试覆盖的代码?怎么衡量测试的完整性?所有这些问题的答案就是代码覆盖率分析。...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:PHPUnit袖珍指南-第五章 命令行测试工具
举报原因:
原因补充:

(最多只允许输入30个字)