开发者

PHPUnit: force display of asserted values

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


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.


I came to this post looking for something similar. I have this testcase:

/**
 * test routing logic (numbers method returns an array of numbers and expected outputs to test)
 * @dataProvider numbers
 */
function testRoute($input,$expected)
{
   $route = new Route($input,'',false);
   $route->route();
   $this->assertEquals($expected,$route->routingResult);
}

and my numbers method is this:

/**
 * read pairs of numbers (input <tab> expected) from tests.input separater by tab
 * return an array like this: array(array(number1,expected1), array(number2,expected2), ...)
 * provide this array to my tests by returning it
 */
function numbers()
{
    $testcases = file('tests.input');
    $tests = array();
    foreach($testcases as $test_case)
    {
        list($input,$output) = explode("\t",$test_case,2);
        $tests[] = array(trim($input),trim($output));
    }
    return $tests;
}

What happens is you get an output like this from phpunit:

 Starting test 'RouteTest::testRoute with data set #0 ('8596000000', 'rejected (dp not found)x')'.
 F
 Starting test 'RouteTest::testRoute with data set #1 ('8596000001', 'rejected (rejected by scheme)')'.
 .
 Starting test 'RouteTest::testRoute with data set #2 ('8596000003', '1599000003')'.
 .

It won't tell you the actual result of the tested function unless the test fails but at least you get to see all the asserted values.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜