PHPUnit-在缓存打开/关闭的情况下运行所有测试两次?
无论memcached服务器是否可用,我都希望确保我的代码始终按预期工作 我的大多数函数如下所示:PHPUnit-在缓存打开/关闭的情况下运行所有测试两次?,php,unit-testing,phpunit,Php,Unit Testing,Phpunit,无论memcached服务器是否可用,我都希望确保我的代码始终按预期工作 我的大多数函数如下所示: function foo($parameter,$force = FALSE) { $result = Cache::get('foo_'.$parameter); if (empty($result) || $force) { // Do stuff with the DB... $result = "something";
function foo($parameter,$force = FALSE)
{
$result = Cache::get('foo_'.$parameter);
if (empty($result) || $force)
{
// Do stuff with the DB...
$result = "something";
Cache::put('foo_'.$parameter,$result,$timeout);
}
return $result;
}
class MyClassTest extends PHPUnit_Framework_TestCase {
protected function setUp()
{
Cache::disable();
}
}
现在,在一个测试用例中,我正在这样做:
class MyClassTest extends PHPUnit_Framework_TestCase {
function testFoo()
{
$result = $myClass->foo($parameter);
$this->assertSomething($result);
}
}
我可以在PHPUnit的setUp()
过程中全局禁用缓存,如下所示:
function foo($parameter,$force = FALSE)
{
$result = Cache::get('foo_'.$parameter);
if (empty($result) || $force)
{
// Do stuff with the DB...
$result = "something";
Cache::put('foo_'.$parameter,$result,$timeout);
}
return $result;
}
class MyClassTest extends PHPUnit_Framework_TestCase {
protected function setUp()
{
Cache::disable();
}
}
调用Cache::disable()
后,在该请求期间,对Cache::get()
的所有调用都将返回false
。现在,我想运行这个类中的所有测试两次,一次使用Cache::disable()代码>和一次,不带密码
有什么办法吗?一个选项是创建一个扩展自MyClassTest
的MyClassNoCacheTest
,只需覆盖(或实现)MyClassNoCacheTest中的设置
方法,在MyClassNoCacheTest
中,我将如何从MyClassTest
调用每个测试方法?它将由phpunit
处理,出于所有目的,当MyClassNoCacheTest
扩展MyClassTest
时,它也继承了它的所有方法(测试),因此它们将再次运行,就像您手动将它们复制到另一个文件中,然后添加了setUp
方法一样