如何用phpunit强制失败

问题描述:

有没有比$this->assertTrue(false)强制phpunit失败的官方方式?如何用phpunit强制失败

我相信这应该测试用例中工作:

$this->fail('Message'); 
+4

$ this-> fail()将停止测试的执行,因此如果您的测试中有多个断言,则不应该将此用作断言的替代以显示消息。 – Prusprus 2012-09-30 18:00:10

+1

将异常传递给'fail'将导致一个不错的堆栈跟踪 – 2015-09-09 21:15:04

是的,那里有一个办法,

$this->fail("your message"); 

,如果你想看到ü都未能超过

print_r(getResponse()->getContent()); 
页面
+0

'getResponse()'是一个特定于框架的函数,可能通常不可用。 – bishop 2016-08-12 17:07:17

做这件事的另一种方法(特别是在编写测​​试工具时很有帮助)将是:

use PHPUnit_Framework_ExpectationFailedException as PHPUnitException; 

try { 
    // something here 
} catch (SpecificException $e) { 
    // force a fail: 
    throw new PHPUnitException("This was not expected."); 
}