Verify that the output does not contain text.

I know how to test php output using the PHPUnit library using expectOutputString() or expectOutputString() . Now I have to be sure that the output does not contain the given string. I can do this using output buffering and string search inside, but probably the best way is to use expectOutputString() with the correct expression.

How should this expression be constructed?

+4
source share
1 answer

You want to use a regex, and to perform a negative match, you must use the lookahead statement syntax. For instance. to verify that the output does not contain hi:

 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!"; } } 

Gives this conclusion:

 $ 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. 
+3
source

Source: https://habr.com/ru/post/1469538/


All Articles