gpt4 book ai didi

phpunit - 测试输出不包含文本

转载 作者:行者123 更新时间:2023-12-04 20:12:38 24 4
gpt4 key购买 nike

我知道如何使用 PHPUnit 库测试 php 输出,使用 expectOutputString()expectOutputString() .现在我需要确保输出不包含给定的字符串。我可以使用输出缓冲和在里面搜索字符串来做到这一点,但可能更好的方法是使用 expectOutputString()用适当的表达。

这个表达式应该如何构建?

最佳答案

您想使用正则表达式,并且要进行否定匹配,您必须使用先行断言语法。例如。测试输出不包含“hello”:

class OutputRegexTest extends PHPUnit_Framework_TestCase
{
private $regex='/^((?!Hello).)*$/s';

public function testExpectNoHelloAtFrontFails()
{
$this->expectOutputRegex($this->regex);
echo "Hello World!\nAnother sentence\nAnd more!";
}

public function testExpectNoHelloInMiddleFails()
{
$this->expectOutputRegex($this->regex);
echo "This is Hello World!\nAnother sentence\nAnd more!";
}

public function testExpectNoHelloAtEndFails()
{
$this->expectOutputRegex($this->regex);
echo "A final Hello";
}

public function testExpectNoHello()
{
$this->expectOutputRegex($this->regex);
echo "What a strange world!\nAnother sentence\nAnd more!";
}
}

给出这个输出:
$ phpunit testOutputRegex.php 
PHPUnit 3.6.12 by Sebastian Bergmann.

FFF.

Time: 0 seconds, Memory: 4.25Mb

There were 3 failures:

1) OutputRegexTest::testExpectNoHelloAtFrontFails
Failed asserting that 'Hello World!
Another sentence
And more!' matches PCRE pattern "/^((?!Hello).)*$/s".


2) OutputRegexTest::testExpectNoHelloInMiddleFails
Failed asserting that 'This is Hello World!
Another sentence
And more!' matches PCRE pattern "/^((?!Hello).)*$/s".


3) OutputRegexTest::testExpectNoHelloAtEndFails
Failed asserting that 'A final Hello' matches PCRE pattern "/^((?!Hello).)*$/s".


FAILURES!
Tests: 4, Assertions: 4, Failures: 3.

关于phpunit - 测试输出不包含文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15435123/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com