【发布时间】:2013-11-18 09:36:28
【问题描述】:
我正在构建一个简单的(单元)测试框架,用户可以在其中测试特定的功能。
但现在我需要拥有SetUp() 和TearDown() 功能。我只是不确定如何实现。
我的代码如下所示:
class TestCase
{
public function SetUp ( ) { }
public function TearDown ( ) { }
// Down here are alot of assertion methods
public function AssertTrue ( )
{
return $this;
}
public function AssertFalse( )
{
return $this;
}
}
另一个类然后扩展这个类:
class SomeTestCase extends TestCase
{
public function SetUp ( )
{
echo 'SetUp';
}
public function TearDown ( )
{
echo 'SetUp';
}
public function TestMethod1 ( )
{
// do some test
}
public function TestMethod2 ( )
{
// do some other test
}
}
现在SetUp() 方法需要在每个测试方法开始时运行。所以在这种情况下TestMethod1 和TestMethod2。
TearDown() 方法需要在每个测试方法结束时运行。
但是我如何从TestCase 类中做到这一点。我不希望用户需要在每个方法中手动添加$this->SetUp() 和$this->TearDown()。
我希望它的行为类似于__construct、__destruct,但是对于每种方法。
我该怎么做?
【问题讨论】:
-
作为一种可能的解决方案,您可以创建一些包装器,在测试用例执行期间始终运行
SetUp和TearDown方法。 -
没有测试过,但是这样的东西应该可以工作:codepad.org/w40NTDNu
标签: php unit-testing