Phpunit: Can @depends reference a test from outside the class?

Created on 11 Apr 2017  路  11Comments  路  Source: sebastianbergmann/phpunit

| Q | A
| --------------------| ---------------
| PHPUnit version | 6.1.0
| PHP version | 7.0.15
| Installation Method | Composer

According to this article, it seems that from PHPUnit 3.4 @depends class::method references the test implemented in class::method().

However, when I try to execute the following tests with phpunit --configuration phpunit.xml tests/Functional/Example I get This test depends on "FirstTest::testProducerFirst" to pass..

namespace Functional\Example;

use PHPUnit\Framework\TestCase;

class FirstTest extends TestCase
{
    public function testProducerFirst()
    {
        $this->assertTrue(true);
        return 'first';
    }

    public function testProducerSecond()
    {
        $this->assertTrue(true);
        return 'second';
    }

    /**
     * @depends testProducerFirst
     * @depends testProducerSecond
     */
    public function testConsumer()
    {
        $this->assertEquals(
            ['first', 'second'],
            func_get_args()
        );
    }
}
namespace Functional\Example;

use PHPUnit\Framework\TestCase;

class SecondTest  extends TestCase
{
    public function testProducerFirst()
    {
        $this->assertTrue(true);
        return 'first';
    }

    public function testProducerSecond()
    {
        $this->assertTrue(true);
        return 'second';
    }

    /**
     * @depends FirstTest::testProducerFirst
     * @depends testProducerSecond
     */
    public function testConsumer()
    {
        $this->assertEquals(
            ['first', 'second'],
            func_get_args()
        );
    }


}

Most helpful comment

First

  • You have to use the complete namespace on:
    @depends Functional\Example\FirstTest::testProducerFirst
  • Second set the execuiton of class order phpunit.xml
<testsuites>
    <testsuite name="sys">
       <file>./tests/Unit/FirstTest.php</file>
        <file>./tests/Unit/SecondTest.php</file>
    </testsuite>
  </testsuites>

  • Finaly
    run: phpunit --testsuite sys

All 11 comments

any progress on this? I would like to know also.

I need this too, any progress ?

@matejvelikonja
@El-Sam
Yes, it can. Just make sure that test on which you depend runs before your test.

@GinoPane How exactly can you do that though?

@igknighton Exactly like it described in the example above. The file with "FirstTest" just need to be processed before "SecondTest" and they both must be in the same test suite. This can be achieved by XML configuration for your tests.

This is not a discussion or support forum, sorry.

That being said, yes, @depends can be used to denote a dependency on a test that is declared in another test case class.

Keep in mind, though, that PHPUnit cannot ensure that a depended-upon test is executed before the test(s) that depend(s) on it.

First

  • You have to use the complete namespace on:
    @depends Functional\Example\FirstTest::testProducerFirst
  • Second set the execuiton of class order phpunit.xml
<testsuites>
    <testsuite name="sys">
       <file>./tests/Unit/FirstTest.php</file>
        <file>./tests/Unit/SecondTest.php</file>
    </testsuite>
  </testsuites>

  • Finaly
    run: phpunit --testsuite sys

@Aeraphe FYi, the reason this ordering works is because of the ordering in the configuration. PHPUnit doesn't actively reorder @depends unless you switch on the recently introduced dependency resolver. Even then, the resolver will only reorder tests within the same TestSuite and will not put the suites themselves in a different order

Basically, use @Depends (note capital D) and then the class reference like so.

@Depends ExternalClass::MethodTest

no need to include full path as mentioned above.

FYI: I am working on ::class based @depends support: #3549

Give me 1-2 weeks as I'm changing jobs but it will be added to PHPUnit core.

Looks like @Depends doesn't care about passing values throughout tests (like @depends does).

Was this page helpful?
0 / 5 - 0 ratings