How to Group Some PHPUnit Tests and Run Them Separately?

You can group and run a select few PHPUnit tests together by using the @group annotation like so:

/**
 * @group foo
 */
public function testMethod()
{
    // ...
}

Once you have tagged all the tests you wish to group, you can use the following command to only run the tests within that group:

phpunit --group foo

You can also specify multiple @group annotations for any single method, for example, like so:

/**
 * @group foo
 * @group bar
 */
public function testMethod()
{
    // ...
}

You can also specify the @group annotation to the class itself. In that case, all test methods of that test class would be considered a part of that group.


This post was published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.