Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run PHPUnit test with its dependencies

My setup is something like this:

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
}

I'd like to focus on testTwo but when I do phpunit --filter testTwo I get message like this:

This test depends on "MyTest::testOne" to pass.
No tests executed!

My question: Is there a way to run one test with all its dependencies?

like image 896
rzajac Avatar asked Feb 12 '23 15:02

rzajac


2 Answers

There's not out of the box way to run automatically all the dependencies. You can however put your tests in groups with the @group annotation and then run phpunit --group myGroup.

like image 170
motanelu Avatar answered Feb 14 '23 09:02

motanelu


I know, this is also not much convenient, but you can try

phpunit --filter 'testOne|testTwo' 

According to phpunit docs we can use regexps as filter.

Also you may consider using data provider to generate your value for the second test. But be aware that data provider method will always be executed before all tests so it may slow down the execution if it has any heavy processing.

One more approach is to create some helper method or object that will do some actual job and cache results to be used by various tests. Then you won't need to use dependencies and your data will be generated on request and cached to be shared by different tests.

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
}
like image 45
Victor Avatar answered Feb 14 '23 09:02

Victor