filmnut
filmnut

Reputation: 746

Getting More Context Around PHPUnit Test Failures

In PHPUnit, let's say I do this:

$this->assertTrue( $some_variable == "foo" );

When I run my tests with my code in a failing state, PHPUnit tells me: Failed asserting that false is true. Which is all fine and good, and I can usually track down WHY the test is failing. But...

I'm wondering if there is a way for PHPUnit to display more context to me when a test fails. For example, is it possible for PHPUnit to output something like: Failed asserting that false is true. When your test ran $some_variable was equal to "bar". That would allow me to more quickly track down why the test failed.

I'd love it if I could see this extra context right in the CLI. Is there a setting I can apply to PHPUnit to make this happen?

Upvotes: 3

Views: 490

Answers (1)

helmbert
helmbert

Reputation: 38004

Using assertTrue

The assertTrue method has a second parameter, where you can specify a message that the assertion should throw when the assertion was negative:

$this->assertTrue($someVariable == "foo", '$someVariable should have been "foo"')

Using specialized assertion functions

PHPUnit offers a myriad of specialized assertions (for equality, identity, array elements and many more), which produce reasonable error messages. Have a look at the documentation.

$this->assertEquals("foo", $someVariable);

Upvotes: 1

Related Questions