[wp-trac] [WordPress Trac] #53118: Tests: review of correctly setting output expectations

WordPress Trac noreply at wordpress.org
Fri Apr 30 15:32:35 UTC 2021


#53118: Tests: review of correctly setting output expectations
------------------------------+-----------------------------
 Reporter:  jrf               |      Owner:  (none)
     Type:  task (blessed)    |     Status:  new
 Priority:  normal            |  Milestone:  Awaiting Review
Component:  Build/Test Tools  |    Version:
 Severity:  minor             |   Keywords:
  Focuses:  docs              |
------------------------------+-----------------------------
 While looking at the tests in the context of another ticket,
 @hellofromTonya and me came across various tests doing things like:

 {{{#!php
 <?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
 <?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.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/53118>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list