No <system-out> with @runTestsInSeparateProcesses and --log-junit
| Q | A |
|---|---|
| PHPUnit version | 9.5.4 |
| PHP version | 7.4.19 |
| Installation Method | Composer / PHAR |
Summary
There is no <system-out> Tag in Junit logging if the Testcase is run in a separate process. (@runTestsInSeparateProcesses)
Current behavior
If you have a print/echo/var_dump etc. in a TestCase that is run in separated processes you get no
How to reproduce
Write a test case with @runTestsInSeparateProcesses, make an print in a test function, and fail afterward. Run it with --log-junit option. This should give you an output in the console but no <system-out> in the xml.
Expected behavior
If you have a print/echo/var_dump etc. in a TestCase that is run in separated processes you get a <system-out> in junit-log.
Error Analytics
The "normal" behavior is achieved with setting the private $output variable in TestCase during runBare(). But if the Test is run in a separate process the process returns only the result and the output of the test (which is run like normal and has the private $output variable set). Then the function processChildResult() takes the handling of the output. But there is no remapping of the output to the actual test that is relevant for the Logger. The output only gets printed to the console (https://github.com/sebastianbergmann/phpunit/blob/add3f7e459eba2764088f49bb249de70c855666c/src/Util/PHP/AbstractPhpProcess.php#L363-L365).
Suggested Solution
Remap the output to $test that is passed into processChildResult(). This could be done through a setter in TestCase or with reflection. I suggest the setter method.
https://github.com/sebastianbergmann/phpunit/blob/add3f7e459eba2764088f49bb249de70c855666c/src/Util/PHP/AbstractPhpProcess.php#L297 Replace with the following line with:
$test->setOutput($childResult['output']);
And add the following function to https://github.com/sebastianbergmann/phpunit/blob/master/src/Framework/TestCase.php:
public function setOutput(string $output): void {
$this->output = $output;
}
Reopened #4690
PR: #4954
In PHPUnit 10, the JUnit XML logfile no longer has <system-out> elements that contain the output printed to stdout by a test.
I do not think that the changes (adding a new method to TestCase, for instance) that would be required to make <system-out> work for tests run in isolated processes in PHPUnit 9.6 should be made.