4
我想確保我的代碼始終按預期工作,無論是否有可用的memcached服務器。PHPUnit - 用緩存開啓/關閉運行所有測試兩次?
我的大部分功能是這樣的:
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;
}
在一個TestCase
現在我這樣做:
:class MyClassTest extends PHPUnit_Framework_TestCase {
function testFoo()
{
$result = $myClass->foo($parameter);
$this->assertSomething($result);
}
}
我可以在PHPUnit的setUp()
這樣全局禁用緩存
class MyClassTest extends PHPUnit_Framework_TestCase {
protected function setUp()
{
Cache::disable();
}
}
致電Cache::disable()
後,所有致電Cache::get()
將在該請求期間返回false
。現在,我想兩次運行這門課的所有測試,一次是Cache::disable();
,一次沒有。
關於如何做到這一點的任何想法?
所以
setUp
方法,在'MyClassNoCacheTest'我將如何去從'MyClassTest'調用每一個測試方法? –當'MyClassNoCacheTest'擴展MyClassTest時,它將由'phpunit'處理,它也繼承了它的所有方法(測試),所以它們將再次運行,就像你手動將它們複製到不同的文件,只是加了'setUp'方法 – mrzard
很好,謝謝! –