PHPUnit passing a test with no assertions within config

Use the @doesNotPerformAssertions annotation:

/**
 * @doesNotPerformAssertions
 */
public function testCodeWithoutUsingAssertions()
{
    // do stuff...
}

Edit: You've got a few choices depending on which version you're using, whether you want to ignore all risky tests or just a few, and if you want it to be permanent or temporary.

Prior to 5.6, if you didn't want to add bogus assertions to all your tests, you had to avoid passing --strict to PHPUnit or add strict="false" to your phpunit.xml. The point of this option is to "Mark a test as incomplete if no assertions are made."

At some point, PHPUnit added the related --dont-report-useless-tests command-line switch and beStrictAboutTestsThatDoNotTestAnything="false" config option. I haven't checked if they are replacements or additional fine-grained versions.

The above options affect all risky tests. Using them will leave you open to accidentally writing tests without assertions. The following new options are safer as you have to purposefully mark each risky test that you wish to allow.

PHPUnit 5.6 added the @doesNotPerformAssertions annotation to mark individual test cases as "not risky" even though they perform no assertions.

/**
 * @doesNotPerformAssertions
 */
public function testWithoutAsserting() {
    $x = 5;
}

PHPUnit 7.2 introduced TestCase::expectNotToPerformAssertions() which does the same thing.

public function testWithoutAsserting() {
    $this->expectNotToPerformAssertions();
    $x = 5;
}

Tags:

Phpunit