Is there a more official way to force a phpunit failure than $this->assertTrue(false)
?
Asked
Active
Viewed 1.8k times
64
![](../../users/profiles/448579.webp)
Parris Varney
- 10,663
- 12
- 44
- 71
3 Answers
108
I believe this should work within a test case:
$this->fail('Message');
![](../../users/profiles/181216.webp)
rr.
- 6,114
- 8
- 37
- 43
-
4The $this->fail() will stop the execution of the test, so this shouldn't be used as a replacement to assertions to display a message if you have multiple assertions in your test. – Prusprus Sep 30 '12 at 18:00
-
2Passing an exception to `fail` will result in a nice stacktrace – Koen. Sep 09 '15 at 21:15
-
is there an opposite function of `$this->fail()`? – emfi May 14 '18 at 09:17
-
3@emfi - yes, ````return;````. – HappyDog Aug 27 '18 at 20:54
3
Another way to do it (especially helpful when writing a testing tool) would be:
use PHPUnit_Framework_ExpectationFailedException as PHPUnitException;
try {
// something here
} catch (SpecificException $e) {
// force a fail:
throw new PHPUnitException("This was not expected.");
}
![](../../users/profiles/96944.webp)
Jannie Theunissen
- 21,664
- 18
- 85
- 111
0
Yes, theres a way,
$this->fail("your message");
if you want to see the page u have failed than
print_r(getResponse()->getContent());
![](../../users/profiles/2682255.webp)
Arpan Buch
- 1,220
- 4
- 18
- 41
-
4`getResponse()` is a framework specific function, which may not be generally available. – bishop Aug 12 '16 at 17:07