9

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.

2
  • Could You expand [Your question] on what kind of log statements You want, and where You want them to be printed?
    – zafarkhaja
    Commented Nov 21, 2012 at 10:12
  • I provided some more basic information. Is the question clear now?
    – Chris
    Commented Nov 21, 2012 at 10:44

2 Answers 2

10

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:

  1. Use the built-in logging functionality (documentation), which can be configured through either phpunit.xml file or the command line arguments (documentation);
  2. 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).
  3. 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)
2
  • 3
    So basically there is no best practice? I am currently going like (3) but this approach is quite embarrassing to me.
    – Chris
    Commented Nov 21, 2012 at 12:51
  • 1
    It depends on what Your definition of best practice is. Regarding this case, from the PHPUnit perspective Option #2 is the preferred way of extending it through event listeners. Unfortunately, there's no easy way to register events to the code inside methods and PHPUnit (PHP) doesn't provide convenient means, so in order to add logging from inside our methods we've got only one choice: to explicitly call our logging mechanism defined in the parent class using the class inheritance (Option #3). Maybe it's not the "best" practice, but it's definitely a common one in the OOP world.
    – zafarkhaja
    Commented Nov 21, 2012 at 14:01
4

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.

2
  • Thank you, however I am aware, that it is possible, because everything is possible :-) I would like to have a snippet for the first version: monolog configuration in the phpunit.xml.dist.
    – Chris
    Commented Nov 21, 2012 at 11:18
  • 2
    If you would like that, take the small amount of time necessary to read the introductory documentation for Monolog, and apply the code in there to what the PHPUnit docs describe -- I'm hesitant to provide a cut-and-paste answer for you when what I would be writing would practically be a cut-and-paste from the official documentation links I already provided.
    – jdd
    Commented Nov 21, 2012 at 11:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.