Skip to content
Advertisement

How to output in CLI during execution of PHP Unit tests?

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 example);

JavaScript

With the following result:

JavaScript

Notice there is none of the expected output.

I’m using the HEAD versions of the git repos as of September 19th, 2011.

Output of php -version:

JavaScript

Is there anything I’m doing wrong, or is this potentially a PHPUnit bug?

Advertisement

Answer

UPDATE

Just realized another way to do this that works much better than the --verbose command line option:

JavaScript

This lets you dump anything to your console at any time without all the unwanted output that comes along with the --verbose CLI option.


As other answers have noted, it’s best to test output using the built-in methods like:

JavaScript

However, sometimes it’s helpful to be naughty and see one-off/temporary debugging output from within your test cases. There is no need for the var_dump hack/workaround, though. This can easily be accomplished by setting the --verbose command line option when running your test suite. For example:

JavaScript

This will display output from inside your test methods when running in the CLI environment.

See: Writing Tests for PHPUnit – Testing Output.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement