【问题标题】:How to force a failure with phpunit如何使用 phpunit 强制失败
【发布时间】:2011-05-09 04:01:10
【问题描述】:

有没有比$this->assertTrue(false) 更正式的强制phpunit 失败的方法?

【问题讨论】:

    标签: phpunit


    【解决方案1】:

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

    $this->fail('Message');
    

    【讨论】:

    • $this->fail() 将停止测试的执行,因此如果您的测试中有多个断言,则不应将其用作断言以显示消息的替代。
    • 将异常传递给fail 将产生一个不错的堆栈跟踪
    • $this->fail()的相反功能吗?
    • @emfi - 是的,return;
    • 如果你只是return;,你可能还需要@doesNotPerformAssertions注解。
    【解决方案2】:

    是的,有办法,

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

    如果你想查看你失败的页面

    print_r(getResponse()->getContent());
    

    【讨论】:

    • getResponse() 是框架特定的功能,可能不普遍可用。
    【解决方案3】:

    另一种方法(在编写测试工具时特别有用)是:

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

    【讨论】:

      猜你喜欢
      • 2019-01-21
      • 2013-04-06
      • 2023-02-08
      • 2019-02-27
      • 1970-01-01
      • 1970-01-01
      • 2012-11-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多