PHPUnit - Why is PHPUnit running in strict mode?

Question: Why does PHPUnit work in strict mode?

Problem:

PHPUnit 4.3.1 by Sebastian Bergman.

Configuration read from /full/path/to/configuration.xml

R

Time: 2.65 seconds, Memory: 11.50Mb

OK, but incomplete, missed or risky tests! Tests: 1, Approvals: 1, Risky: 1. Done.

also:

Risky test: test code or verified code did not (only) close output buffers

My PHP version is 5.4.

As stated in the documentation ( https://phpunit.de/manual/current/en/strict-mode.html ), this only applies to strict PHPUnits settings.

PHPUnit may perform additional checks when running tests. In addition to fine-grained control over various strict modes (see below), you can use the --strict command line option or set strict = "true" in the PHPUnit XML configuration file to include all of them.

-

Exit during test execution

PHPUnit can be strict regarding output during tests. This check can be by using the -disallow-test-output option on the command line or by setting beStrictAboutOutputDuringTests = "true" in the PHPUnit XML configuration file.

A test that emits output, for example, by invoking printing in a test code or a verified code, will be flagged as dangerous when this check is made.

I believe that I did not activate strict mode. My command line: "/ usr / bin / php / usr / bin / phpunit --colors --bootstrap / full / path / to / bootstrap.php --configuration / full / path / to / configuration.xml / full / path /to/Test.php ". I also used the configuration as stated in " https://phpunit.de/manual/current/en/appendixes.configuration.html .

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.3/phpunit.xsd" backupGlobals="true" backupStaticAttributes="false" cacheTokens="false" colors="false" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" forceCoversAnnotation="false" mapTestClassNameToCoveredClassName="false" printerClass="PHPUnit_TextUI_ResultPrinter" processIsolation="false" stopOnError="false" stopOnFailure="false" stopOnIncomplete="false" stopOnSkipped="false" testSuiteLoaderClass="PHPUnit_Runner_StandardTestSuiteLoader" timeoutForSmallTests="1" timeoutForMediumTests="10" timeoutForLargeTests="60" strict="false" verbose="false"> </phpunit> 

Earlier, I used a shorter version of this configuration, which gave the same result.

 <phpunit beStrictAboutOutputDuringTests="false" strict="false" colors="false"> </phpunit> 
+7
php unit-testing phpunit
source share
3 answers

Looking at the code available on GitHub , it seems that no matter what the documentation can say, the problem of output buffering is checked and always reported .

Thus, the symptoms that you observe do not mean that the tests are performed in strict mode.

github.com/sebastianbergmann/phpunit/blob/4.3/src/Framework/TestCase.php#L818

 // ... try { $this->stopOutputBuffering(); } catch (PHPUnit_Framework_RiskyTestError $_e) { if (!isset($e)) { $e = $_e; } } 

github.com/sebastianbergmann/phpunit/blob/4.3/src/Framework/TestCase.php#L1938-L1946

 private function stopOutputBuffering() { if (ob_get_level() != $this->outputBufferingLevel) { while (ob_get_level() > 0) { ob_end_clean(); } throw new PHPUnit_Framework_RiskyTestError( 'Test code or tested code did not (only) close its own output buffers' ); } // ... $this->outputBufferingActive = false; $this->outputBufferingLevel = ob_get_level(); } 

Placing a breakpoint in the lines above in your favorite PHPUnit debugger may reveal some other dependencies (e.g. disallowTestOutput flag ...?)

+6
source share

Find your code base for error_reporting. Perhaps you can enable strict mode in your code. error_reporting(E_NOTICE) enough to trigger the dangerous warning you receive.

PHPUnit is not special binary code (I use this misconception), its just that PHP has some bootstrapping mechanisms that run your code. This means that your code has the same environment as PHPUnit, so it is likely that somewhere in your code you can configure the error report strictly.

0
source share

From the conclusion, I believe that I am right in believing that you are doing only one test, so setting --stop-on-risky will not really help.

I recommend making sure that you close output buffering in your code. If you ever use something like ob_start, make sure you call ob_end_clean or ob_end_flush before your script stops executing.

As a second thought, perhaps try passing the -d and -v flags at startup to see if it gives you more information.

0
source share

All Articles