How to log from within PHPUnit test case?

logging, php, phpunit

Solution

I would suggest that You use the Selenium Server Logging. It will provide You with all debugging information You might need.

If it's not an option for You and You still want to log from the `PHPUnit` test cases, You have 3 options:

- Use the built-in logging functionality (documentation), which can be configured through either `phpunit.xml` file or the command line arguments (documentation);

- Create Your own logger by implementing the `PHPUnit_Framework_TestListener` interface, which is kind of limited because it supports only a limited amount of events You can log, such as `startTest`, `endTest`, `addError`, `addFailure`, etc. See the (documentation) for the complete list of the supported events. You could possibly add some more events to your implementation but that would also require that You override the `PHPUnit_Framework_TestSuite::run()` method by subclassing the `PHPUnit_Extensions_TestDecorator` class (documentation) or by implementing the `PHPUnit_Framework_Test` interface (documentation).

- The easiest and more flexible option is to subclass the `PHPUnit_Framework_TestCase` class and implement the logging functionality there. This will allow You to log from within Your test methods.(documentation)

Problem

What is best practice for printing log statements from a PHPUnit test case? I am running selenium test cases and want to print out something like "Log in acccomplished", "Page XY opened". I want to see those in any log file I want. Would be good, if you can define a log level.

Original source