9951 explained code solutions for 126 technologies


phpunitHow to order tests with PHPUnit?


PHPUnit provides a way to order tests using the @depends annotation. This annotation allows you to specify that a test method depends on another test method. The dependent test method will be run before the test method that depends on it.

For example:

class MyTest extends TestCase
{
    public function testA()
    {
        // ...
    }

    /**
     * @depends testA
     */
    public function testB()
    {
        // ...
    }
}

The @depends annotation can also be used to specify multiple test methods that the current test method depends on.

For example:

class MyTest extends TestCase
{
    public function testA()
    {
        // ...
    }

    public function testB()
    {
        // ...
    }

    /**
     * @depends testA
     * @depends testB
     */
    public function testC()
    {
        // ...
    }
}

In this example, testC will be run after both testA and testB have been run.

Helpful links

Edit this code on GitHub