问题
->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
回答1:
You could just store up the failures for the end, say with a
$passing = true;
if (! false) { $passing = false; }
if (! true) { $passing = false; }
$this->assertTrue($passing);
but I highly discourage this form of testing. I have written tests like this, and they exponentially get out of hand, and worse, you start to get weird failures for hard-to-find reasons.
Additionally, smarter people than me agree, tests should not have any conditionals (if/else, try/catch), because each conditional adds significant complexity to the test. If a conditional is needed, perhaps both the test and the SUT, or System Under Test, should be looked at very carefully, for ways to make it simpler.
A much better way would be to change it to be two tests, and if they share a significant portion of the setup, then move those two tests into a new test class, with the shared setup performed in the Setup() method.
回答2:
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.
回答3:
that defeats the point of a unit test. you might want to break it into a few more test methods instead of having a monolithic test method.
Here is some pseudo-code, as a bad example.
MyBadTestMethod()
{
someResult = MyMethod();
assertIsCorrect(someResult);
myResult2 = MyMethod2(someResult);
assertIsCorrect(myResult2);
}
MyMethod2
and myResult2
will fail.
Here is a better example.
MyTestMethod1()
{
someResult = MyMethod();
assertIsCorrect(someResult);
}
MyTestMethod2()
{
myResult2 = MyMethod2(someCorrectResult);
assertIsCorrect(myResult2);
}
回答4:
I'm late to the party, but I recommend using a configuration file for the test suites to achieve this easily.
You can create phpunit.xml
file from where you are running the phpunit test. Hence phpunit will run the needed tests listed there.
PHPUnit 3.7.38 by Sebastian Bergmann.
Configuration read from /path/phpunit.xml
In that file you can specify that you don't want to stop on failure.
<phpunit bootstrap="vendor/autoload.php" stopOnFailure="false">
<testsuites>
<testsuite name="Test">
<file>tests/ClassTest.php</file>
</testsuite>
</testsuites>
</phpunit>
Hope this helps.
来源:https://stackoverflow.com/questions/6832263/phpunit-assertion-failed-but-i-want-to-continue-testing