單元測(cè)試類
單元測(cè)試是一種用于測(cè)試應(yīng)用程序中每個(gè)函數(shù)的軟件開發(fā)方法。如果你對(duì)此概念不熟悉,或許需要google一下相關(guān)概念。
CodeIgniter的單元測(cè)試類非常簡(jiǎn)單,由一個(gè)評(píng)估函數(shù)和兩個(gè)結(jié)果函數(shù)組成。它沒打算成為一個(gè)十全的測(cè)試集,只提供一個(gè)簡(jiǎn)單的能夠評(píng)測(cè)你的代碼是否可以產(chǎn)生正確的數(shù)據(jù)類型及結(jié)果的解決方案。
初始化單元類
和大多數(shù)其它的類一樣,在CodeIgniter中,單元測(cè)試類一樣要在控制器中用函數(shù)$this->load->library 來初始化:
$this->load->library('unit_test');
測(cè)試類一旦加載,單元測(cè)試對(duì)象可以這樣使用:$this->unit
運(yùn)行測(cè)試:
運(yùn)行一個(gè)測(cè)試應(yīng)提供一個(gè)測(cè)試及期望的結(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');
注意第二個(gè)參數(shù)"is_string"的使用,它告訴函數(shù)測(cè)試你的測(cè)試用例把產(chǎn)生的將是一個(gè)字符串。下面是合法的類型列表:
- is_object
- is_string
- is_bool
- is_true
- is_false
- is_int
- is_numeric
- is_float
- is_double
- is_array
- is_null
生成報(bào)告
你可以在每個(gè)測(cè)試后立刻顯示結(jié)果,也可以運(yùn)行若干測(cè)試后再產(chǎn)生報(bào)告。直接顯示一個(gè)報(bào)告,可以用echo或者返回run函數(shù)(的執(zhí)行結(jié)果):
echo $this->unit->run($test, $expected_result);
顯示所有測(cè)試的完整的報(bào)告,可以這樣:
echo $this->unit->report();
報(bào)告會(huì)以格式化了的HTML表格的形式呈現(xiàn)。如果你更喜歡看原始的數(shù)據(jù),可以用下面代碼獲取一個(gè)數(shù)組:
echo $this->unit->result();
嚴(yán)格模式
默認(rèn)情況下,單元測(cè)試類評(píng)測(cè)字面匹配(literal match)時(shí)比較寬松,考慮以下例子:
$this->unit->run(1, TRUE);
此測(cè)試評(píng)測(cè)的是一個(gè)整數(shù),但是期望結(jié)果卻是布爾值。不過,由于PHP是弱類型語言,使用正常的相等測(cè)試,以上代碼返回的將是TRUE:
if (1 == TRUE) echo 'This evaluates as true';
喜歡的話,你可以讓單元測(cè)試類在嚴(yán)格模式下執(zhí)行,這樣它會(huì)同時(shí)比較兩數(shù)據(jù)的類型及值:
if (1 === TRUE) echo 'This evaluates as FALSE';
啟用嚴(yán)格模式:
$this->unit->use_strict(TRUE);
啟用/禁用單元測(cè)試
如果你想放一些測(cè)試在代碼中,而又希望讓它只在需要的時(shí)候運(yùn)行,你可以禁用單元測(cè)試:
$this->unit->active(FALSE)
Unit Test Display
When your unit test results display, the following items show by default:
試翻譯:當(dāng)你顯示單元測(cè)試結(jié)果的時(shí)候,以下信息(原以為條目)將被默認(rèn)顯示:
- Test Name (test_name)
- 試翻譯:測(cè)試名稱(test_name)
- Test Datatype (test_datatype)
- 試翻譯:測(cè)試數(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)
- 試翻譯:手動(dòng)添加的注釋(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() ,你可以自定義顯示信息(條目). 比如,如果你只期望顯示測(cè)試名稱和測(cè)試結(jié)果:
Customizing displayed tests
試翻譯:自定義顯示測(cè)試結(jié)果
$this->unit->set_test_items(array('test_name', 'result'));
Creating a Template
試翻譯:創(chuàng)建顯示模板
如果你希望測(cè)試結(jié)果以自定義的格式顯示,這里提供一個(gè)簡(jiǎn)單的模板示例。注意必需的偽變量(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);
注意: 你的模板必須在單元測(cè)試運(yùn)行之前聲明。
?