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
功能可以帮助在这种情况下?