我正在开发一个将测试导出为PHP/PHPUnit的工具,我遇到了一个小问题。简单地解释一下,测试脚本只包含对actionwords对象的调用,该对象包含测试的所有逻辑(以便考虑不同测试场景中的各个步骤)。一个例子可能更清楚:
require_once('Actionwords.php');
class CoffeeMachineHiptestPublisherSampleTest extends PHPUnit_Framework_TestCase {
public $actionwords = new Actionwords();
public function simpleUse() {
$this->actionwords->iStartTheCoffeeMachine();
$this->actionwords->iTakeACoffee();
$this->actionwords->coffeeShouldBeServed();
}
}
在coffeeShouldBeServed
方法中,我需要运行断言,但这是不可能的,因为Actionwords类没有扩展PHPUnit_Framework_TestCase
(我不确定它应该扩展,它不是一个测试用例,只是一组助手)。
目前,我找到的解决方案是将测试对象传递给动作词,并使用断言的引用,诸如此类。
class CoffeeMachineHiptestPublisherSampleTest extends PHPUnit_Framework_TestCase {
public $actionwords;
public function setUp() {
$this->actionwords = new Actionwords($this);
}
}
class Actionwords {
var $tests;
function __construct($tests) {
$this->tests = $tests;
}
public function coffeeShouldBeServed() {
$this->tests->assertTrue($this->sut->coffeeServed);
}
}
它很好用,但我觉得它不是很优雅。我不是PHP开发人员,所以可能会有一些更好的解决方案,感觉更"PHP化"。
提前感谢,Vincent
断言方法是静态的,因此您可以使用PHPUnit_Framework_Assert::assertEquals()
。