PHPUnit:强制显示断言值

tak*_*hin 13 php phpunit unit-testing

在PHPUnit测试失败时,将显示实际值和期望值.
但是当测试通过时,不会显示此信息.

如何强制PHPUnit始终显示预期和实际的断言结果?

Dom*_*nik 14

赛跑

phpunit --testdox
Run Code Online (Sandbox Code Playgroud)

将显示每个测试名称.因此,作为一种解决方法,您可以在测试名称中包含您的预期和实际断言结果...仍然只是一种解决方法......


pin*_*hic 3

由于您很可能使用$this->assert...()调用断言,因此您可以在测试用例中覆盖这些方法。快速示例:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

当然,tearDownAfterClass()可能还不够晚,不符合您的喜好。这与断言失败不同。