phpunit/framework/testcase.php,实现 PHPUnit_Framework_Test

# 实现 PHPUnit_Framework_Test

`PHPUnit_Framework_Test` 接口是比较狭义的,十分容易实现。举例来说,你可以自行为 `PHPUnit_Framework_Test` 编写一个类似于 `PHPUnit_Framework_TestCase` 的实现来运行*数据驱动测试*。

[Example 14.6, “一个数据驱动的测试”](# "Example 14.6. 一个数据驱动的测试")展示了一个数据驱动的测试用例类,对来自 CSV 文件内的值进行比较。这个文件内的每个行看起来类似于 `foo;bar`,第一个值是期望值,第二个值则是实际值。

**Example 14.6. 一个数据驱动的测试**

~~~

class DataDrivenTest implements PHPUnit_Framework_Test

{

private $lines;

public function __construct($dataFile)

{

$this->lines = file($dataFile);

}

public function count()

{

return 1;

}

public function run(PHPUnit_Framework_TestResult $result = NULL)

{

if ($result === NULL) {

$result = new PHPUnit_Framework_TestResult;

}

foreach ($this->lines as $line) {

$result->startTest($this);

PHP_Timer::start();

$stopTime = NULL;

list($expected, $actual) = explode(';', $line);

try {

PHPUnit_Framework_Assert::assertEquals(

trim($expected), trim($actual)

);

}

catch (PHPUnit_Framework_AssertionFailedError $e) {

$stopTime = PHP_Timer::stop();

$result->addFailure($this, $e, $stopTime);

}

catch (Exception $e) {

$stopTime = PHP_Timer::stop();

$result->addError($this, $e, $stopTime);

}

if ($stopTime === NULL) {

$stopTime = PHP_Timer::stop();

}

$result->endTest($this, $stopTime);

}

return $result;

}

}

$test = new DataDrivenTest('data_file.csv');

$result = PHPUnit_TextUI_TestRunner::run($test);

?>

~~~

~~~

PHPUnit 5.0.0 by Sebastian Bergmann and contributors.

.F

Time: 0 seconds

There was 1 failure:

1) DataDrivenTest

Failed asserting that two strings are equal.

expected string

difference < x>

got string

/home/sb/DataDrivenTest.php:32

/home/sb/DataDrivenTest.php:53

FAILURES!

Tests: 2, Failures: 1.

~~~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值