在CakePHP测试中显示断言的结果和预期值

时间:2012-10-05 11:56:00

标签: unit-testing cakephp netbeans phpunit assertion

有没有人知道是否有办法强制CakePHP TestSuite在失败时查看断言的Expected和Result值?典型的PHPUnit测试默认显示在输出中,但不是Cake的TestSuite(使用PHPUnit)。一方面,当我在NetBeans中调试测试用例时,每当我尝试为变量设置监视时,我都会遇到某种Socket异常,并且它只发生在CakePHP测试用例中,它在evry其他源文件中工作正常。还有解决方案吗?

2 个答案:

答案 0 :(得分:0)

退房 http://www.dereuromark.de/2011/12/04/unit-testing-tips-for-2-0-and-phpunit/

基本上,您可以扩展TestCase类并创建自己的其他方法,如

public function details($is, $expected) {
    echo 'is:' . $is; echo '<br />';
    echo 'expected: ' . $expected; ob_flush();

}

并在内部调用它,或者在失败时调用它。

或者你到处调试()。

答案 1 :(得分:0)

好的,我以某种方式解决了当你更新Cake Library文件时解决方案将被删除的问题。您需要编辑文件lib\Cake\TestSuite\Reporter\CakeHtmlReporter并更改方法:

public function paintFail($message, $test) 
{
    $trace = $this->_getStackTrace($message);
    $testName = get_class($test) . '(' . $test->getName() . ')';
    echo "<li class='fail'>\n";
    echo "<span>Failed</span>";
    echo "<div class='msg'><pre>" . $this->_htmlEntities($message) . "</pre></div>\n";
    echo "<div class='msg'>" . __d('cake_dev', 'Test case: %s', $testName) . "</div>\n";
    echo "<div class='msg'>" . __d('cake_dev', 'Stack trace:') . '<br />' . $trace . "</div>\n";
    echo "</li>\n";
}

为:

public function paintFail($message, $test) {
    $trace = $this->_getStackTrace($message);
    $testName = get_class($test) . '(' . $test->getName() . ')';

    echo "<li class='fail'>\n";
    echo "<span>Failed</span>";
    echo "<div class='msg'><pre>" . $this->_htmlEntities($message->getComparisonFailure()->toString()) . "</pre></div>\n";
    echo "<div class='msg'>" . __d('cake_dev', 'Test case: %s', $testName) . "</div>\n";
    echo "<div class='msg'>" . __d('cake_dev', 'Stack trace:') . '<br />' . $trace . "</div>\n";
    echo "</li>\n";
}

当断言失败时,这将输出类似的内容:

Failed asserting that two arrays are equal.--- Expected
+++ Actual
@@ @@
 Array (
-    0 => 'Crypto3DS'
-    1 => 'Zlib'
+    1 => 'Crypto3DS'
+    2 => 'Zlib'
 )

Cake Yeppie的快速测试:D