The other day I was debugging an error in one of my unit tests, and found it hard to track down because when I ran PHPUnit, it ran all the tests contained in the file where my problem unit test was located. After some Googling and reading the PHPUnit Api Docs, I found that you can specify a test, among other tests, by adding a comment with the @group annotation. Using @group and any group name you wish, you can tell PHPUnit, from the command line, to test only tests belonging to a specific group.

Consider the following:

public function testArrayCount()
{
    $this->assertCount(2, array('foo', 'bar');
}

/**
 * @group grain
 */
public function testArrayPopulated()
{
    $this->assertTrue(empty(array()));
}

Notice how I specified the testArrayPopulated() method with a @group grain annotation, now I can tell PHPUnit to only test tests belonging to the grain group.

./phpunit --colors --group grain path/to/your/tests/

Now, when PHPUnit runs, it will only run tests against those tests to which you have assigned the grain group. This @group annotation is a really cool feature of PHPUnit and helped me a lot with debugging my unit tests.

–UPDATE–

Per Edo’s comments: You can also filter tests by adding ‘–filter testYourTestName’ (two hypens). This will make it so only that test or any test matching the string will be run, same benefits as group, but easier to implement. It does require you to put some thought into how you name the methods within your test.

Per Patrick’s comment: I corrected the order for the command line arguments. The options should ALWAYS go before the path to your tests.

Thanks for the feedback.