Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force a failure with phpunit

Tags:

phpunit

Is there a more official way to force a phpunit failure than $this->assertTrue(false)?

like image 377
Parris Varney Avatar asked Nov 12 '10 16:11

Parris Varney


People also ask

How do I run a PHPUnit test case?

Save the file. To run the unit test, click the arrow next to the Run button on the Tool-bar, and select Run As | PHPUnit Test . From the Menu-bar, select Run | Run As | PHPUnit Test . To debug the PHPUnit Test Case, click the arrow next to the debug button on the toolbar, and select Debug As | PHPUnit Test .

What is PHPUnit used for?

PHPUnit is a unit testing framework for the PHP programming language. It is an instance of the xUnit design for unit testing systems that began with SUnit and became popular with JUnit. Even a small software development project usually takes hours of hard work.

What is assertion in PHPUnit?

The assertion methods are declared static and can be invoked from any context using PHPUnit\Framework\Assert::assertTrue() , for instance, or using $this->assertTrue() or self::assertTrue() , for instance, in a class that extends PHPUnit\Framework\TestCase .


2 Answers

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."); } 
like image 33
Jannie Theunissen Avatar answered Sep 22 '22 06:09

Jannie Theunissen


I believe this should work within a test case:

$this->fail('Message'); 
like image 167
rr. Avatar answered Sep 21 '22 06:09

rr.