PHPUnit命令行测试工具是通过phpunit命令调用的。如下代码显示如何通过PHPUnit命令行测试工具运行测试。 phpunitArrayTest PHPUnit2.3.0bySebastianBergmann. Time:0.067288 OK(2tests) 对每个测试,PHPUnit命令行测试工具打印一个字符表示进程: ·测试成功打印“.”。 ·运行测试方法是发生了断言失败打印“F”。 ·运行测试方法是发生了错误打印“E”。 ·测试没有完成或测试没有实现打印“I”(见本书后“未完成的测试”一章)。 PHPUnit可以区分失败和错误。一个失败是PHPUnit的断言违例,错误是一个意料外的异常或一个PHP错误。有时候这种差别是有用的,因为错误相比失败更容易修正。如果你有一大串问题列表,最好先解决所有错误,然后看看有没有失败遗留下来。 让我们看看如下一些代码命令行测试工具的选项: phpunit--help PHPUnit2.3.0bySebastianBergmann. Usage:phpunit[switches]UnitTest[UnitTest.php] --coverage-data<file>Writecode-coveragedatainrawformattofile. --coverage-html<file>Writecode-coveragedatainHTMLformattofile. --coverage-text<file>Writecode-coveragedataintextformattofile. --testdox-html<file>WriteagiledocumentationinHTMLformattofile. --testdox-text<file>WriteagiledocumentationinTextformattofile. --log-xml<file>LogtestprogressinXMLformattofile. --loader<loader>TestSuiteLoaderimplementationtouse. --skeletonGenerateskeletonUnitTestclassforUnitinUnit.php. --waitWaitsforakeystrokeaftereachtest. --helpPrintsthisusageinformation. --versionPrintstheversionandexits. phpunitUnitTest 运行类UnitTest提供的测试,该类应该定义在源文件UnitTest.php中。 类UnitTest必须继承PHPUnit2_Framework_TestCase类,或是提供了公有静态方法suite,并返回PHPUnit2_Framework_Test对象的类(例如,类PHPUnit2_Framework_TestSuite的一个实例) phpunitUnitTestUnitTest.php 运行类UnitTest提供的测试,该类要定义在命令指定的源文件(UnitTest.php)中。 --coverage-data,--coverage-html,and--coverage-text 控制运行测试的代码覆盖信息的分析和集合(参见本书后代码覆盖分析一节) --testdox-htmland--testdox-text 以HTML或普通文本格式生成运行测试的敏捷文档(参见本书后的“测试的其他用途”一章) --log-xml 生成运行测试的XML格式的日志文件。 下一个例子显示为ArrayTest中的测试生成的XML日志文件。 <?xmlversion="1.0"encoding="UTF-8"?> <testsuites> <testsuitename="ArrayTest"tests="2"failures="0"errors="0"time="0.020026"> <testcasename="testNewArrayIsEmpty"class="ArrayTest"time="0.014449"/> <testcasename="testArrayContainsAnElement"class="ArrayTest"time="0.005577"/> </testsuite> </testsuites> 下面的XML日志文件是为名为FailureErrorTest的测试类两个测试生成的,一个是testFailure,一个是testError。这显示了失败和错误是如何分别表示的。 <?xmlversion="1.0"encoding="UTF-8"?> <testsuites> <testsuitename="FailureErrorTest"tests="2"failures="1"errors="1"time="0.013603"> <testcasename="testFailure"class="FailureErrorTest"time="0.011872"> <failuremessage=""type="PHPUnit2_Framework_AssertionFailedError"></failure> </testcase> <testcasename="testError"class="FailureErrorTest"time="0.001731"> <errormessage=""type="Exception"></error> </testcase> </testsuite> </testsuites> --loader 指定将要使用的测试套件加载器。 标准测试套件加载器会在当前工作目录和PHP的include_pathconfiguration指令定义的路径中寻找源文件。按照PEAR的命名规则,形如Project_Package_Class的类名会映射到的源文件为Project/Package/Class.php。 --skeleton 为类Unit(在文件Unit.php中)生成一个名为UnitTest(在文件UnitTest.php中)的测试用例类的框架。对原始类的每个方法,在生成的测试用例类中提供了一个未完成的测试用例(见本书后的“未完成测试”部分)。 下面的例子显示了如何为一个名为Sample的类生成一个测试类的框架。 phpunit--skeletonSample PHPUnit2.3.0bySebastianBergmann. WrotetestclassskeletonforSampleto SampleTest.php. phpunitSampleTest
|