When running a PHPUnit test, I would like to be able to dump output so I can debug one or two things.
I have tried the following (similar to the PHPUnit Manual examp
I output my Testresults HTML based, in this case it was helpfull to flush the content:
var_dump($array);
ob_flush();
There is a second PHP Method
flush()
which i not has tried.
Here are few methods useful for printing debug messages in PHPUnit 4.x:
syslog(LOG_DEBUG, "Debug: Message 1!");
More practical example:
syslog(LOG_DEBUG, sprintf("%s: Value: %s", __METHOD__, var_export($_GET, TRUE)));
Calling syslog() will generate a system log message (see: man syslog.conf
).
Note: Possible levels: LOG_DEBUG
, LOG_INFO
, LOG_NOTICE
, LOG_WARNING
, LOG_ERR
, etc.
On macOS, to stream the syslog messages in realtime, run:
log stream --level debug --predicate 'processImagePath contains "php"'
fwrite(STDERR, "LOG: Message 2!\n");
Note: The STDERR
constant is not available if reading the PHP script from stdin. Here is the workaround.
Note: Instead of STDERR
, you can also specify a filename.
file_put_contents('php://stderr', "LOG: Message 3!\n", FILE_APPEND);
Note: Use this method, if you don't have STDERR constant defined.
register_shutdown_function('file_put_contents', 'php://stderr', "LOG: Message 4!\n", FILE_APPEND);
Note: Use this method, if you'd like to print something at the very end without affecting the tests.
To dump the variable, use var_export()
, e.g. "Value: " . var_export($some_var, TRUE) . "\n"
.
To print above messages only during verbose or debug mode, see: Is there a way to tell if --debug or --verbose was passed to PHPUnit in a test?
Although if testing the output is part of the test it-self, check out: Testing Output docs page.
Update: See rdlowrey's update below regarding the use of fwrite(STDERR, print_r($myDebugVar, TRUE));
as a much simpler work around
This behaviour is intentional (as jasonbar has pointed out). The conflicting state of the manual has been reported to PHPUnit.
A work-around is to have PHPUnit assert the expected output is empty (when infact there is output) which will trigger the unexpected output to be shown.
class theTest extends PHPUnit_Framework_TestCase
{
/**
* @outputBuffering disabled
*/
public function testOutput() {
$this->expectOutputString(''); // tell PHPUnit to expect '' as output
print_r("Hello World");
print "Ping";
echo "Pong";
$out = "Foo";
var_dump($out);
}
}
gives:
PHPUnit @package_version@ by Sebastian Bergmann.
F
Time: 1 second, Memory: 3.50Mb
There was 1 failure:
1) theTest::testOutput
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-''
+'Hello WorldPingPongstring(4) "Foo"
+'
FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
Be certain to disable any other assertions you have for the test as they may fail before the output assertion is tested (and hence you wont see the output).
Try using --debug
Useful if you're trying to get the right path to an include or source data file.
It's not a bug, but very much intentional. Your best bet is to write to a log file of some kind and tail the log to watch for output.
If you are trying to TEST output, check this out.
Also:
Note: Please note that PHPUnit swallows all output that is emitted during the execution of a test. In strict mode, a test that emits output will fail.
Just use the --verbose flag when execute phpunit.
$ phpunit --verbose -c phpunit.xml
The advantage of this method is that you don't need to change the test code, you can print strings, var_dump's o anything you wish always and it will be shown in the console only when verbose mode is set.
I hope this helps.