PHPUnit passing a test with no assertions within config

Viewed 19245

I want to pass tests which get the following: "This test did not perform any assertions"

I know I could add something like assertTrue(true) however is it possible to add something to the config to make these tests pass cleaner?

I'm pretty sure this only happens since version PHPUnit 3.5.0 with the introduction of --strict

8 Answers

In my case, the test has no assertion because I'm just checking everything using prophecy lib to mock and check that methods are being called as expected...

PHPUnit by default expects that test has at least one assertion, and warn you to put it.

Just adding this line in any part of your test is enough.

$this->expectNotToPerformAssertions();

But also you can design a test that in some cases have assertions or not

public function testCodeNoAssertions()
{
    if ($something === true) {
        $this->expectNotToPerformAssertions();
    }
    // do stuff...

    if ($somethingElse === true) {
    //do any assertion
    }
}

In conclusion, It's not a bug... It's a feature.

PHPUnit 7.2+. $this->expectNotToPerformAssertions() can be used, if you have conditions, that might disable assertions. Seems it is not in the official docs, but can be found here: https://github.com/sebastianbergmann/phpunit/pull/3042/files

public function testCodeNoAssertions()
{
    $this->expectNotToPerformAssertions();
}

At least PHPUnit 7.0+. @doesNotPerformAssertions can be used. Current docs do not state when this was added. https://phpunit.readthedocs.io/en/7.0/annotations.html#doesnotperformassertions

/**
 * @doesNotPerformAssertions
 */
public function testCodeWithoutUsingAssertions()
{

}

If your test is simply incomplete, you should be using $this->markTestIncomplete() https://phpunit.readthedocs.io/en/7.0/incomplete-and-skipped-tests.html

public function testCodeThatIsIncomplete()
{
    $this->markTestIncomplete('Implementation of this test is not complete.');
}

If you want to temporarily disable a test $this->markTestSkipped() should be used. https://phpunit.readthedocs.io/en/7.0/incomplete-and-skipped-tests.html#skipping-tests

public function testCodeThatIsDisabledTemporarily()
{
    $this->markTestSkipped('This test is disabled to test, if it is interfering with other tests.');
}

I created a superclass and placed SimpleTest -like function there:

/**
 * Pass a test
 */
public function pass()
{
    //phpunit is missing pass()
    $this->assertTrue(true);
}

Then you can call it

$this->pass();
Related