views:

357

answers:

5

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?

A: 

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 ...

Dominik
+1  A: 

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.

Gordon
To accomplish this, I may just modify `PHPUnit_Assert`, butI was hoping for some command line switch to have this feature on demand…
takeshin
@takehin modifying core libraries is **always** a bad idea. The next time you update PHPUnit, all your mods are gone. That's why I suggested extending or using a custom assertion instead.
Gordon
You're right Gordon. But that's just a hypothetical idea. I do not intend to rewrite PHPUnit nor put var_dumps everywhere, just when I want to see debug for a moment :)
takeshin
A: 

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).

RMatthews
That message will only show if the assertion fails, though.
pinkgothic
A: 

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.

pinkgothic
Yes, I have thought about this too, but I haven't found any base method for all the assertions.
takeshin
Oh! I see. But I do wonder: Would (if it existed) that be sensible? Different assertions have different semantics and different parameters. Wouldn't trying to boil them all down to one method not dilute (or worse) the effectiveness of the feedback you want?
pinkgothic
The only sensible option for what I want is a command line switch. And an implementation if this would be a little more hassle.
takeshin
A: 

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.

radalin