Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

phpunit: Avoid printing very long output

I have a PHPUnit test which checks that a rendered HTML output does not contain a certain string, I use:

public function testSomething() {
    $htmlOutput = ...;
    self::assertDoesNotMatchRegularExpression(
        '/...pattern to detect a certain error.../',
        $htmlOutput,
        'HTML response contained a certain error',
    );
}

When the test fails, PHPUnit prints an extremly long output:

There was 1 failure:

1) MyTest::testSomething
HTML response contained a certain error
Failed asserting that '<!DOCTYPE html>\r\n
<html lang="en">\r\n
<head>\r\n
...
... hundreds and hundreds of lines
....
</body>\r\n
</html>' does not match PCRE pattern "/...pattern to detect a certain error.../".

This is very annoying because all pieces of important information have now scrolled up in my terminal way beyond reach, which is the name of the failing test and the actual message "HTML response contained a certain error". Of course the exact string can potentially be important to figure out what went wrong, but in half of the cases the message is good enough.

What's the recommended approach here?

like image 904
jlh Avatar asked Oct 21 '25 04:10

jlh


1 Answers

I am afraid that this is what it is when assertDoesNotMatchRegularExpression() is used. That being said, I would suggest not to use regular expressions for verifying HTML or XML. Use specialized assertions that use CSS selectors or XPath expressions instead.

like image 175
Sebastian Bergmann Avatar answered Oct 22 '25 18:10

Sebastian Bergmann



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!