?
This document uses PHP Chinese website manual Release
單元測試是軟件開發(fā)的一種方法,其中為應(yīng)用程序中的每個(gè)功能編寫測試。如果你對這個(gè)概念不熟悉,你可以在這個(gè)主題上進(jìn)行一些搜索。
CodeIgniter的單元測試類非常簡單,包含一個(gè)評估函數(shù)和兩個(gè)結(jié)果函數(shù)。它不是一個(gè)完整的測試套件,而是一個(gè)簡單的機(jī)制來評估你的代碼,以確定它是否產(chǎn)生正確的數(shù)據(jù)類型和結(jié)果。
使用單元測試庫
定制顯示的測試
創(chuàng)建一個(gè)模板
初始化類
運(yùn)行測試
生成報(bào)告
嚴(yán)格模式
啟用/禁用單元測試
單元測試顯示
類參考
像CodeIgniter中的大多數(shù)其他類一樣,Unit Test類在您的控制器中使用$ this-> load-> library函數(shù)進(jìn)行初始化:
$this->load->library('unit_test');
加載后,單元測試對象將可用 $this->unit
運(yùn)行測試包括以下列方式提供測試和預(yù)期結(jié)果:
$this->unit->run(‘test’, ‘expected result’, ‘test name’, ‘notes’);
測試是您希望測試的代碼的結(jié)果,預(yù)期的結(jié)果是您期望的數(shù)據(jù)類型,測試名稱是可以給您測試的可選名稱,注釋是可選注釋。例:
$test = 1 + 1;$expected_result = 2;$test_name = 'Adds one plus one';$this->unit->run($test, $expected_result, $test_name);
您提供的預(yù)期結(jié)果可以是文字匹配,也可以是數(shù)據(jù)類型匹配。這是一個(gè)文字的例子:
$this->unit->run('Foo', 'Foo');
這是一個(gè)數(shù)據(jù)類型匹配的例子:
$this->unit->run('Foo', 'is_string');
注意在第二個(gè)參數(shù)中使用“is_string”?這告訴函數(shù)來評估你的測試是否產(chǎn)生一個(gè)字符串作為結(jié)果。以下是允許的比較類型列表:
is_object
is_string
is_bool
is_true
is_false
is_int
is_numeric
is_float
is_double
is_array
is_null
is_resource
您可以在每次測試后顯示結(jié)果,也可以運(yùn)行多個(gè)測試并在最后生成報(bào)告。要直接顯示報(bào)告,只需回顯或返回運(yùn)行功能:
echo $this->unit->run($test, $expected_result);
要運(yùn)行所有測試的完整報(bào)告,請使用以下命令:
echo $this->unit->report();
該報(bào)告將被格式化為HTML表格供查看。如果您更喜歡原始數(shù)據(jù),則可以使用以下方法檢索數(shù)組:
echo $this->unit->result();
默認(rèn)情況下,單元測試類會(huì)松散地評估文字匹配??紤]這個(gè)例子:
$this->unit->run(1, TRUE);
測試正在評估一個(gè)整數(shù),但預(yù)期的結(jié)果是一個(gè)布爾值。但是,由于PHP數(shù)據(jù)松散,PHP會(huì)使用普通的相等性測試將上述代碼評估為TRUE:
if (1 == TRUE) echo 'This evaluates as true';
如果你愿意,你可以把單元測試類放在嚴(yán)格模式下,它將比較數(shù)據(jù)類型和值:
if (1 === TRUE) echo 'This evaluates as FALSE';
要啟用嚴(yán)格模式,請使用以
$this->unit->use_strict(TRUE);
如果您想在腳本中保留一些測試,但是除非您需要,否則不會(huì)運(yùn)行測試,您可以使用以下命令禁用單元測試:
$this->unit->active(FALSE);
當(dāng)您的單元測試結(jié)果顯示時(shí),下列項(xiàng)目默認(rèn)顯示:
測試名稱(test_name)
測試數(shù)據(jù)類型(test_datatype)
預(yù)期的數(shù)據(jù)類型(res_datatype)
結(jié)果(結(jié)果)
文件名(文件)
行號(行)
您為測試輸入的任何筆記(筆記)
您可以使用$ this-> unit-> set_test_items()來自定義其中哪些項(xiàng)目顯示。例如,如果您只想顯示測試名稱和結(jié)果:
$this->unit->set_test_items(array('test_name', 'result'));
如果你希望你的測試結(jié)果格式不同,那么你可以設(shè)置你自己的模板。這是一個(gè)簡單模板的例子。請注意所需的偽變量:
$str = '<table border="0" cellpadding="4" cellspacing="1">{rows} <tr> <td>{item}</td> <td>{result}</td> </tr>{/rows}</table>';$this->unit->set_template($str);
注意
在運(yùn)行單元測試過程之前,必須聲明您的模板。
class CI_Unit_testset_test_items($items)
參數(shù): | $ items(array) - 可見測試項(xiàng)目列表 |
---|---|
返回: | 空虛 |
$ items(array) - 可見測試項(xiàng)目列表
Returns: void
設(shè)置應(yīng)在測試中可見的項(xiàng)目列表。有效的選項(xiàng)是:
test_name
test_datatype
res_datatype
result
file
line
notes
run($test[, $expected = TRUE[, $test_name = 'undefined'[, $notes = '']]])
參數(shù): | $ test(mixed) - 測試數(shù)據(jù)$ expected(混合) - 預(yù)期結(jié)果$ test_name(字符串) - 測試名稱$ notes(字符串) - 任何要附加到測試的注釋 |
---|---|
返回: | 測試報(bào)告 |
返回類型: | 串 |
$ test(混合) - 測試數(shù)據(jù)
$預(yù)期(混合) - 預(yù)期結(jié)果
$ test_name(string) - 測試名稱
$ notes(string) - 任何要附加到測試的注釋
Returns: Test report
Return type: string
Runs unit tests.
report([$result = array()])
參數(shù): | $ result(array) - 包含測試結(jié)果的數(shù)組 |
---|---|
返回: | 測試報(bào)告 |
返回類型: | 串 |
$ result(array) - 包含測試結(jié)果的數(shù)組
Returns: Test report
Return type: string
生成關(guān)于已完成測試的報(bào)告。
use_strict([$state = TRUE])
參數(shù): | $ state(bool) - 嚴(yán)格狀態(tài)標(biāo)志 |
---|---|
返回類型: | 空虛 |
$ state(bool) - 嚴(yán)格狀態(tài)標(biāo)志
Return type: void
Enables/disables strict type comparison in tests.
active([$state = TRUE])
參數(shù): | $ state(bool) - 是否啟用測試 |
---|---|
返回類型: | void |
$ state(bool) - 是否啟用測試
Return type: void
啟用/禁用單元測試。
result([$results = array()])
參數(shù): | $ results(array) - 測試結(jié)果列表 |
---|---|
返回: | 原始結(jié)果數(shù)據(jù)數(shù)組 |
返回類型: | 排列 |
$ results(array) - 測試結(jié)果列表
返回:原始結(jié)果數(shù)據(jù)的數(shù)組
Return type: array
返回原始測試結(jié)果數(shù)據(jù)。
set_template($template)
參數(shù): | $ template(string) - 測試結(jié)果模板 |
---|---|
返回類型: | void |
$ template(string) - 測試結(jié)果模板
Return type: void
設(shè)置用于顯示測試結(jié)果的模板。