2016-10-17 29 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' –