在phpunit中设置断言块的错误消息

时间:2017-09-27 18:16:13

标签: phpunit

有没有办法在块内设置 X 断言数的错误消息?我正在尝试提供更有意义的错误消息,目前我的测试代码部分看起来像:

    $helperString = 'testInsert() @ test conflicting reasonid';
    $this->assertEquals(..., ..., $helperString);
    $this->assertEquals(..., ..., $helperString);
    $this->assertEquals(..., ..., $helperString);
    $this->assertEquals(..., ..., $helperString);
    $this->assertEquals(..., ..., $helperString);
    $this->assertEquals(..., ..., $helperString);
    $this->assertEquals(..., ..., $helperString);
    $this->assertEquals(..., ..., $helperString);
    $this->assertEquals(..., ..., $helperString);

哪个有效,但它很难看。我希望能够做到这样的事情:

    $this->setMessage($helperString);
    $this->assertEquals(..., ...);
    $this->assertEquals(..., ...);
    $this->assertEquals(..., ...);
    $this->assertEquals(..., ...);
    $this->assertEquals(..., ...);
    ....

这样的功能是否存在?我不知道如何在phpunit中提供更好的输出,而不是Failed asserting that 1 matches 0

1 个答案:

答案 0 :(得分:1)

似乎没有内置此类功能 - 但您可以轻松添加自己的断言测试,例如:

$this->assertNotConflictingReasonid($x, $y);

然后在函数中,如果测试失败,请提供自定义消息。根据测试的内容和方式,他们还可以根据输入或比较自定义消息。