Make WordPress Core

Opened 4 years ago

#53118 new task (blessed)

Tests: review of correctly setting output expectations

Reported by: jrf's profile jrf Owned by:
Milestone: Awaiting Review Priority: normal
Severity: minor Version:
Component: Build/Test Tools Keywords:
Focuses: docs Cc:

Description

While looking at the tests in the context of another ticket, @hellofromTonya and me came across various tests doing things like:

<?php
ob_start();
// Do something which generates output...
$contents = ob_get_clean();

$this->assertRegExp( $regex, $contents );

PHPUnit has dedicated functionality build-in to set output expectations which will then automatically test that the output conforms to those expectations.

When using those, the above code would become:

<?php
$this->expectOutputRegex( $regex );

// Do something which generates output...

I suspect that in most cases were ob_*() functions are being used to "catch" output before testing it, this can (and should) be replaced with using the PHPUnit native expectOutputString() and expectOutputRegex() functions.

Note: each test can only have ONE output expectation when using the PHPUnit native functionality.

Also see: https://phpunit.readthedocs.io/en/9.5/writing-tests-for-phpunit.html#testing-output

I'd recommending a complete review of the test suite to verify whether output expectations are tested correctly.

To find all instances of this code pattern a search should be done for the use of ob_*() functions in the tests/phpunit directory.

Change History (0)

Note: See TracTickets for help on using tickets.