PHPUnit @depends 注释不起作用

PHPUnit @depends annoation doesn't work

当我用@depends 编写phpunit 测试用例时(在Yii2 中),这个带有@depends 的测试用例将是skipped.It 似乎无法找到所依赖的函数。 这是代码:

测试用例代码:

 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);
    }
}

运行 测试后的控制台消息:

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

有人可以帮忙吗?

测试只能依赖于其他测试。 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));
    }
}

我发现我必须 运行 整个测试 class GoodsServiceTest 而不仅仅是测试方法 testPush。同时,我必须确认 testPull写在testPush之前。 希望这个答案能帮助到其他人

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);
    }

}