![](/img/trans.png)
[英]symfony + phpunit - how to test UserFactory with PasswordEncoder dependencies
[英]How to run PHPUnit test with its dependencies
我的設置是這樣的:
class MyTest extends PHPUnit_Framework_TestCase
{
// More tests before
public function testOne()
{
// Assertions
return $value;
}
/**
* @depends testOne
*/
public function testTwo($value)
{
// Assertions
}
// More tests after
}
我想專注於testTwo,但是當我做phpunit --filter testTwo
我收到這樣的消息:
This test depends on "MyTest::testOne" to pass.
No tests executed!
我的問題:有沒有辦法運行一個帶有所有依賴項的測試?
沒有開箱即用的方法可以自動運行所有依賴項。 但是,您可以使用@group
注釋將測試分組,然后運行phpunit --group myGroup
。
我知道,這也不太方便,但你可以試試
phpunit --filter 'testOne|testTwo'
根據phpunit docs,我們可以使用regexps作為過濾器。
您也可以考慮使用數據提供程序為第二次測試生成值。 但請注意,數據提供程序方法將始終在所有測試之前執行,因此如果處理繁重,可能會降低執行速度。
另一種方法是創建一些輔助方法或對象,它將執行一些實際的作業和緩存結果,以供各種測試使用。 然后,您將不需要使用依賴項,您的數據將根據請求生成並緩存以由不同的測試共享。
class MyTest extends PHPUnit_Framework_TestCase
{
protected function _helper($someParameter) {
static $resultsCache;
if(!isset($resultsCache[$someParameter])) {
// generate your $value based on parameters
$resultsCache[$someParameter] = $value;
}
return $resultsCache[$someParameter];
}
// More tests before
public function testOne()
{
$value = $this->_helper('my parameter');
// Assertions for $value
}
/**
*
*/
public function testTwo()
{
$value = $this->_helper('my parameter');
// Get another results using $value
// Assertions
}
// More tests after
}
使用正則表達式
phpunit --filter='/testOne|testTwo/'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.