Snake.Hu Snake.Hu - 1 month ago 21
PHP Question

PHPUnit @depends annoation doesn't work

When I coding phpunit testcase with @depends(in Yii2), this testcase with @depends will be skipped.It seems that the function be depended on cannot be found.
Here is the code:

testcase code:



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


}


console message after running test:



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


Can anyone help?

Answer

I found out that I must run the whole test class GoodsServiceTest but not only the test method testPush.At the same time,I must confirm testPull writing before testPush. Hoping this answer will help someone else

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

}
Comments