Passing a test without assertions with PHPUnit

While it may seem counter intuitive to have a test that doesn’t have any
assertions, PHPUnit’s unique method of sharing data between tests can put you in
a situation where you have a test that just doesn’t need to actually test
anything, and simply returns some data.

In those scenarios, you will need to tell PHPUnit that it’s A-OK to have a test
without any assertions.

PHPUnit 5.6+

/**
 * @doesNotPerformAssertions
 */
public function testSomething()
{
    // Awesome code... but no assertions
}

PHPUnit 7.2+

public function testSomething()
{
    self::expectNotToPerformAssertions

    // Awesome code... but no assertions
}

Configuration option

There are some options where you can tell PHPUnit to not worry about “useless
tests”, but I personally don’t recommend them, because you run the risk of
accidentally removing an assertion and PHPUnit won’t be the wiser.

If you are okay with this risk, you can set
beStrictAboutTestsThatDoNotTestAnything to false in your phpunit.xml file.

Josh Sherman - The Man, The Myth, The Avatar

About Josh

Husband. Father. Pug dad. Musician. Founder of Holiday API, Head of Engineering and Emoji Specialist at Mailshake, and author of the best damn Lorem Ipsum Library for PHP.


If you found this article helpful, please consider buying me a coffee.