1
給定一組測試數據文件如何組織在一系列類似輸入上運行的測試?
test001.txt
test002.txt
- 等
和預期結果的數據文件
expected001.txt
expected002.txt
- 等
我使用phpunit
測試數據處理功能:
public function test001()
{
$fn = "test001.txt";
$data = file_get_contents($fn);
// code to perform the test, and create $processedData from $data
$fn = "expected001.txt";
$expestedData = file_get_contents($fn);
$this->assertEquals($expestedData, $processedData);
}
過程代碼是完全對所有對測試數據文件和相應的結果數據文件相同的。
因此,應用測試上的所有文件,我可以做一個循環:
public function test001to213()
{
for ($k = 0; $k < 213; $k++) {
$fn = "test".sprintf('%03d', $k).".txt";
$data = file_get_contents($fn);
// data process code to create $processedData from $data
$fn = "expected".sprintf('%03d', $k).".txt";
$expectedData = file_get_contents($fn);
$this->assertEquals($expectedData, $processedData);
}
}
但是,這樣一來我有一個測試213個斷言,我失去了以下好處:
- 我不知道什麼測試數字失敗/通過,因爲測試停止首次失敗的斷言。
- 在第二次執行時,我無法只運行失敗的測試。
- 我不能選擇一個特定的測試數x - 在調試模式下
運行增加更多的代碼來獲得這種好處之前,有沒有更好的解決辦法? 什麼phpunit
功能可以幫助在這種情況下?