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.
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:
phpunit.xml file or the command line arguments (documentation);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).PHPUnit_Framework_TestCase class and implement the logging functionality there. This will allow You to log from within Your test methods.(documentation)You can implement a test listener, specify it in your phpunit.xml.dist, and do whatever you want, with whatever logging system you want (Monolog is popular) See the documentation for a better writeup and example than I can write off the top of my head.
If you want to specify messages in the test methods, you could have your tests inherit from a common class that adds a log method. And do whatever you want in there. I don't like that as a solution, but it's certainly an option.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With