Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPUnit - assertion failed but I want to continue testing

->assertTrue(false);
->assertTrue(true);

First assertion was failed and execution was stopped. But I want to continue the further snippet of code.

Is there possible in PHPUnit

like image 532
hellboy Avatar asked Jul 26 '11 15:07

hellboy


People also ask

How do I run a single PHPUnit test?

You can run all the tests in a directory using the PHPUnit binary installed in your vendor folder. You can also run a single test by providing the path to the test file. You use the --verbose flag to get more information on the test status. The output shows that we ran 1 test, and made 3 assertions in it.

What is assertion in PHPUnit?

PHPUnit assertTrue() Function The assertTrue() function is a builtin function in PHPUnit and is used to assert whether the assert value is true or not. This assertion will return true in the case if the assert value is true else returns false.

What is assert failed?

An assertion statement specifies a condition that you expect to hold true at some particular point in your program. If that condition does not hold true, the assertion fails, execution of your program is interrupted, and this dialog box appears.


1 Answers

The other answerers are correct - you really should separate out your assertions into separate tests, if you want to be able to do this. However, assuming you have a legitimate reason for wanting to do this... there is a way.

Phpunit assertion failures are actually exceptions, which means you can catch and throw them yourself. For example, try this test:

public function testDemo()
{
    $failures = [];
    try {
        $this->assertTrue(false);
    } catch(PHPUnit_Framework_ExpectationFailedException $e) {
        $failures[] = $e->getMessage();
    }
    try {
        $this->assertTrue(false);
    } catch(PHPUnit_Framework_ExpectationFailedException $e) {
        $failures[] = $e->getMessage();
    }
    if(!empty($failures))
    {
        throw new PHPUnit_Framework_ExpectationFailedException (
            count($failures)." assertions failed:\n\t".implode("\n\t", $failures)
        );
    }
}

As you can see, it tries two assertions, both of which fail, but it waits until the end to throw all of the failure output messages as a single exception.

like image 142
Benubird Avatar answered Sep 23 '22 11:09

Benubird