Passing a test without assertions with PHPUnit

Josh Sherman
1 min read
Software Development PHP

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.

Join the Conversation

Good stuff? Want more?

Weekly emails about technology, development, and sometimes sauerkraut.

100% Fresh, Grade A Content, Never Spam.

About Josh

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

Currently Reading

Parasie Eve

Previous Reads

Buy Me a Coffee Become a Sponsor

Related Articles