單元測試類
單元測試是一種用于測試應(yīng)用程序中每個函數(shù)的軟件開發(fā)方法。如果你對此概念不熟悉,或許需要google一下相關(guān)概念。
CodeIgniter的單元測試類非常簡單,由一個評估函數(shù)和兩個結(jié)果函數(shù)組成。它沒打算成為一個十全的測試集,只提供一個簡單的能夠評測你的代碼是否可以產(chǎn)生正確的數(shù)據(jù)類型及結(jié)果的解決方案。
初始化單元類
和大多數(shù)其它的類一樣,在CodeIgniter中,單元測試類一樣要在控制器中用函數(shù)$this->load->library 來初始化:
$this->load->library('unit_test');
測試類一旦加載,單元測試對象可以這樣使用:$this->unit
運行測試:
運行一個測試應(yīng)提供一個測試及期望的結(jié)果給以下函數(shù):
$this->unit->run( test, expected result, 'test name', 'notes');
Where test is the result of the code you wish to test, expected result is the data type you expect, test name is an optional name you can give your test, and notes are optional notes. Example:
$test = 1 + 1;
$expected_result = 2;
$test_name = 'Adds one plus one';
$this->unit->run($test, $expected_result, $test_name);
你所給出的期望結(jié)果($expected_result)可以是字面上匹配(literal match)的也可以是數(shù)據(jù)類型上匹配的。下例為字面匹配(literal match):
$this->unit->run('Foo', 'Foo');
下例為類型匹配(data type match):
$this->unit->run('Foo', 'is_string');
注意第二個參數(shù)"is_string"的使用,它告訴函數(shù)測試你的測試用例把產(chǎn)生的將是一個字符串。下面是合法的類型列表:
- is_object
- is_string
- is_bool
- is_true
- is_false
- is_int
- is_numeric
- is_float
- is_double
- is_array
- is_null
生成報告
你可以在每個測試后立刻顯示結(jié)果,也可以運行若干測試后再產(chǎn)生報告。直接顯示一個報告,可以用echo或者返回run函數(shù)(的執(zhí)行結(jié)果):
echo $this->unit->run($test, $expected_result);
顯示所有測試的完整的報告,可以這樣:
echo $this->unit->report();
報告會以格式化了的HTML表格的形式呈現(xiàn)。如果你更喜歡看原始的數(shù)據(jù),可以用下面代碼獲取一個數(shù)組:
echo $this->unit->result();
嚴格模式
默認情況下,單元測試類評測字面匹配(literal match)時比較寬松,考慮以下例子:
$this->unit->run(1, TRUE);
此測試評測的是一個整數(shù),但是期望結(jié)果卻是布爾值。不過,由于PHP是弱類型語言,使用正常的相等測試,以上代碼返回的將是TRUE:
if (1 == TRUE) echo 'This evaluates as true';
喜歡的話,你可以讓單元測試類在嚴格模式下執(zhí)行,這樣它會同時比較兩數(shù)據(jù)的類型及值:
if (1 === TRUE) echo 'This evaluates as FALSE';
啟用嚴格模式:
$this->unit->use_strict(TRUE);
啟用/禁用單元測試
如果你想放一些測試在代碼中,而又希望讓它只在需要的時候運行,你可以禁用單元測試:
$this->unit->active(FALSE)
Unit Test Display
When your unit test results display, the following items show by default:
試翻譯:當你顯示單元測試結(jié)果的時候,以下信息(原以為條目)將被默認顯示:
- Test Name (test_name)
- 試翻譯:測試名稱(test_name)
- Test Datatype (test_datatype)
- 試翻譯:測試數(shù)據(jù)類型(test_datatype)
- Expected Datatype (res_datatype)
- 試翻譯:期望數(shù)據(jù)類型(res_datatype)
- Result (result)
- 試翻譯:結(jié)果(result)
- File Name (file)
- 試翻譯:文件名稱(file)
- Line Number (line)
- 試翻譯:行數(shù)(line)
- Any notes you entered for the test (notes)
- 試翻譯:手動添加的注釋(notes)
You can customize which of these items get displayed by using $this->unit->set_items(). For example, if you only wanted the test name and the result displayed:
試翻譯:使用$this->unit->set_items() ,你可以自定義顯示信息(條目). 比如,如果你只期望顯示測試名稱和測試結(jié)果:
Customizing displayed tests
試翻譯:自定義顯示測試結(jié)果
$this->unit->set_test_items(array('test_name', 'result'));
Creating a Template
試翻譯:創(chuàng)建顯示模板
如果你希望測試結(jié)果以自定義的格式顯示,這里提供一個簡單的模板示例。注意必需的偽變量(seudo-variables):
$str = '
<table border="0" cellpadding="4" cellspacing="1">
????{rows}
????????<tr>
????????<td>{item}</td>
????????<td>{result}</td>
????????</tr>
????{/rows}
</table>';
$this->unit->set_template($str);
注意: 你的模板必須在單元測試運行之前聲明。
?