2016-10-17 101 views
0

當我使用@depends(在Yii2中)編寫phpunit測試用例時,這個帶有@depends的測試用例將被跳過。似乎函數依賴於找不到。 下面是代碼:PHPUnit @depends annoation不起作用

測試用例代碼:

E:\xampp_5_5_32\php\php.exe C:/Users/huzl/AppData/Local/Temp/ide-phpunit.php --bootstrap E:\MIC\vagrant\rental\frontend\tests\_bootstrap.php --no-configuration --filter "/::testPush(.*)?$/" frontend\tests\example\GoodsServiceTest E:\MIC\vagrant\rental\frontend\tests\example\GoodsServiceTest.php 
Testing started at 15:35 ... 
PHPUnit 4.8.27 by Sebastian Bergmann and contributors. 

This test depends on "frontend\tests\example\GoodsServiceTest::pull" to pass. 

Time: 430 ms, Memory: 4.50MB 

No tests executed! 

Process finished with exit code 0 

誰能幫助:

class GoodsServiceTest extends \PHPUnit_Framework_TestCase 
{ 
    private $service; 

    public function pull(){ 
     return [1,2]; 
    } 

    /** 
    * @depends pull 
    */ 
    public function testPush($stack){ 
     $this->assertEquals([1,2],$stack); 
    } 
} 

控制檯消息運行測試後?

+1

不應該'拉'需要有一個斷言通過?當'testPush'取決於'push','push'它自己需要成功才執行'testPush' – masterFly

+1

將你的圖片替換成你的代碼和錯誤 –

+0

@masterFly我這麼認爲,但我不知道why.Is任何可能的'推'無法找到? –

回答

1

我發現我必須運行整個測試類GoodsServiceTest但不是唯一的測試方法testPush。與此同時,我必須testPush前確認testPull寫作。 希望這個答案能夠幫助別人

class GoodsServiceTest extends \PHPUnit_Framework_TestCase 
{ 
    private $service; 

    public function testPull(){ 
      return [1,2]; 
    } 


    /** 
    * @depends pull 
    */ 
    public function testPush($stack){ 
     $this->assertEquals([1,2],$stack); 
    } 

} 
1

測試只能依賴於其他測試。 pull不是測試,因爲它沒有testPrefix。

但你實際上想要使用的是data provider

class GoodsServiceTest extends \PHPUnit_Framework_TestCase 
{ 
    private $service; 

    public function getStacks() 
    { 
     return [ //a list of test calls 
        [ // a list of test arguments 
         [1,2], //first argument 
         3 //second argument 
        ], 
        [ 
         [3,5], 
         8 
        ] 
       ]; 
    } 


    /** 
    * @dataProvider getStacks 
    */ 
    public function testStacks($stack, $expectedResult) 
    { 
     $this->assertEquals($expectedResult, array_sum($stack)); 
    } 
} 
+0

將'pull'替換爲'testPull'後,它還沒有工作。 –

+0

您是否將註釋更改爲'@depends testPull'? – Naktibalda

+0

當然,應用程序沒有進入'testPull' –