9951 explained code solutions for 126 technologies


phpunitHow to group tests in PHPUnit?


PHPUnit provides a way to group tests into logical units. This can be done by using the @group annotation. Tests can be grouped by feature, type, or any other criteria.

<?php

use PHPUnit\Framework\TestCase;

class MyTest extends TestCase
{
    /**
     * @group feature
     */
    public function testFeatureA()
    {
        // ...
    }

    /**
     * @group feature
     */
    public function testFeatureB()
    {
        // ...
    }

    /**
     * @group type
     */
    public function testTypeA()
    {
        // ...
    }
}

The @group annotation can be used to group tests together. Tests can be grouped by feature, type, or any other criteria. Tests can then be run by group using the --group option.

$ phpunit --group feature

The code above will run all tests with the @group feature annotation.

Helpful links

Edit this code on GitHub