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解釋器類解決這個問題。