When in PHPUnit test fails, actual and expected values are displayed.
But when the test passes, this information is not displayed.
How to force PHPUnit to always display expected and actual assertion result?
When in PHPUnit test fails, actual and expected values are displayed.
But when the test passes, this information is not displayed.
How to force PHPUnit to always display expected and actual assertion result?
running
phpunit --testdox
will show each test name. So as a workaround, you could maybe incorporate your expected and actual assertion results inside the test name ... still it's just a workaround ...
Either create your own Assertion class and have it behave like a proxy to the actual assertion class and echoing the values before delegating to the actual assertion, e.g.
$this->assertWithLogging('assertion', $expected, $actual, $message);
or override PHPUnit's own class (which I think will be very tricky) or simply do
$this->assertSame($expected, $actual, $message);
echo "$expected is $actual";
That's not pretty either, because it will screw up output when running through CLI. If you happen to use Zend Studio, you will see the output in the Debug Output Tab.
Another route would be with TestListeners, but I don't know enough about them to tell you any details. Looks like you can hook into the testing process.
You can actually just use the $message value in the assert??? method and put what ever you want in that field. I normally use it to show what the expected and actual values are as well as a unique name for the assertion (assuming that I have more than one in a given test).
Since you're most likely calling the assertions with $this->assert...(), you can just overwrite those methods in your test case. Quick example:
class YourTestCase extends PHPUnit_Framework_TestCase {
...
static private $messages = array();
...
static public function assertSame($var1, $var2, $message = '') {
parent::assertSame($var1, $var2, $message);
// assertSame() throws an exception if not true, so the following
// won't occur unless the messages actually are the same
$success = print_r($var1, true) . ' is the same as '
. print_r($var2, true);
self::$messages = array_merge(self::$messages, array($success));
}
static public function tearDownAfterClass() {
echo implode("\n", self::$messages);
}
}
Of course, tearDownAfterClass() may not be late enough for your liking. It's not the same as an assertion failure would be.
Another thing might be to write your own Listener. This way, you can give the output you want and leave the assertions to the phpunit. This might be the easiest and most customizable way to do it I guess.