您可以为此使用 PHPUnit 的数据提供程序:
<?php
require_once 'PHPUnit/Framework/TestCase.php';
class ProviderTest extends PHPUnit_Framework_TestCase
{
public function testCaseProvider()
{
// parse your data file however you want
$data = array();
foreach (file('test_data.txt') as $line) {
$data[] = explode("\t", trim($line));
}
return $data;
}
/**
* @dataProvider testCaseProvider
*/
public function testAddition($num1, $num2, $expectedResult)
{
$this->assertEquals($expectedResult, $num1 + $num2);
}
}
?>
你的 test_data.txt 文件看起来像这样:
1 2 3
2 2 4
3 5 7
然后运行测试:
$ phpunit ProviderTest.php
PHPUnit 3.4.12 by Sebastian Bergmann.
...F
Time: 0 seconds, Memory: 5.75Mb
There was 1 failure:
1) ProviderTest::testAddition with data set #2 ('3', '5', '7')
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-7
+8
/Users/dana/ProviderTest.php:23
FAILURES!
Tests: 4, Assertions: 3, Failures: 1.