Is there a more official way to force a phpunit failure than $this->assertTrue(false)
?
Asked
Active
Viewed 2.2k times
75

Parris Varney
- 11,320
- 12
- 47
- 76
3 Answers
122
I believe this should work within a test case:
$this->fail('Message');

rr.
- 6,484
- 9
- 40
- 48
-
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
-
If you just `return;`, you will probably also need the `@doesNotPerformAssertions` annotation. – Nye Dec 09 '21 at 17:14
3
Yes, theres a way,
$this->fail("your message");
if you want to see the page u have failed than
print_r(getResponse()->getContent());

Arpan Buch
- 1,380
- 5
- 19
- 41
-
4`getResponse()` is a framework specific function, which may not be generally available. – bishop Aug 12 '16 at 17:07
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.");
}

Jannie Theunissen
- 28,256
- 21
- 100
- 127