Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should unit tests use logging?

There seems to be two trends on this topic:

  1. Some answers (such as this one) suggest unit tests should not log anything.
  2. Some questions and answers (such as this one) suggest different logging techniques and formats used in unit tests.

Should unit tests log what they do? Would those additional informations be helpful in the unit test reports? Or should unit tests be silent as long as they don't fail?

My question targets Java unit tests, but input from programmers in other languages could be interesting as well.

like image 876
Laurent Pireyn Avatar asked May 12 '11 08:05

Laurent Pireyn


People also ask

What should a log of each test run should include?

The Test Log contains detailed information on all the executed tests, including all the script routines, keyword tests, low-level procedures, and so on. It contains the results of test operations in each test. It can also include images of the tested applications, links to files, and other entries.


1 Answers

This is obviously a bit subjective, but I don't see why you would disable logging in your unit tests.

I do think you're misinterpreting the first linked post; the poster is not claiming that you shouldn't log anything, he's saying that a pass/fail should not be something that is just in the logs. It should be returned to the testing framework. It should be a piece of data that is completely seperate from the normal logs.

I agree with him on that.

Apart from that you could still have your normal logging. You have it anyway in the classes you're testing (or should have). When a test fails, you might see something in the log which will help you debug it more quickly. I don't see how this could ever be a negative point.

like image 82
Joeri Hendrickx Avatar answered Sep 30 '22 18:09

Joeri Hendrickx