PHPUnit 中的expectOutputString 或expectOutputRegex
您好,我正在使用 PHPUnit 进行单元测试,
我在使用 ExpectOutputString/expectOutputRegex 方法测试输出时遇到问题
问题:
function test_myTest() {
$this->expectOutputString('testxzxzxzxzxz');
$this->expectOutputString('test');
echo 'test';
}
当我生成单元测试报告时,此测试通过,即使 第一个期望失败
与断言方法不同,如果有一个断言失败,则测试失败
示例assertTrue:
// this test fail because the first assertTrue fails
function test_myAssert() {
$this->assertTrue(false);
$this->assertTrue(true);
}
看起来这是PHPUnit中缺乏的功能..
有人有想法或替代方法来实现我在测试输出时想要的功能吗?
Hi I am using PHPUnit for my unit testing
I have a problem regarding testing output using the expectOutputString/expectOutputRegex method
Problem :
function test_myTest() {
$this->expectOutputString('testxzxzxzxzxz');
$this->expectOutputString('test');
echo 'test';
}
This test pass when i generate unit test report even though the
first expectation fails
Unlike in the assert methods, test fail if there is one assertion that fails
Example assertTrue :
// this test fail because the first assertTrue fails
function test_myAssert() {
$this->assertTrue(false);
$this->assertTrue(true);
}
Looks like this is a lacking functionality in PHPUnit..
Anyone have ideas or alternative way to achieve what I want when testing output?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
expectOutputString
存储给定的字符串以与整个测试的输出进行比较,但它会覆盖任何先前存储的字符串。换句话说,只有最后一次调用expectOutputString
才会产生任何效果。您必须构建完整的输出字符串并仅调用一次expectOutputString
。上面的代码将会失败,因为
testxzxzxzxzxztest
不等于输出test
。expectOutputString
stores the given string to compare against the output of the whole test, but it overwrites any previously stored string. In other words, only the last call toexpectOutputString
has any effect. You must build the full output string and callexpectOutputString
just once.The above will fail because
testxzxzxzxzxztest
does not equal the outputtest
.expectOutputString
测试整个输出,因此多次调用它并没有真正意义。但是,多次调用expectOutputRegex
是有意义的,因为一个字符串可以匹配多个正则表达式。然而,PHPUnit 只记住最后一个要匹配的正则表达式。因此,对expectOutputRegex
的后续调用会默默地覆盖预期值。我创建了以下支持多次调用
expectOutputRegex
的类:expectOutputString
tests the whole output, so calling it multiple times does not really makes sense. However, callingexpectOutputRegex
multiple times would make sense, since a string can match multiple regular expressions. However, PHPUnit remembers only the last regular expression to match against. So subsequent calls toexpectOutputRegex
silently overwrite the expected value.I created the following class that supports calling
expectOutputRegex
multiple times: