开发者

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 q开发者_如何转开发uestion targets Java unit tests, but input from programmers in other languages could be interesting as well.


Unit tests really should be so simple and focused, that a test failing already documents what went wrong. You shouldn't need to read through logs for a test case to find that out.

However, it is a good idea to log the total results of a large suite of automated tests so that you don't have to trawl through all the tests to find the ones that failed. It is nice to see a summary at the end that you can focus on.


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.


I've used both quiet and verbose logging during unit tests, and I personally prefer it when each test outputs a single line with test name and how it went. I find it more appealing when I can tell what is going on, though I can't say it has any real impact on my work.

If you run from console, colored output is a plus, I think.


I believe, there should be no logging in unit tests, but only assertions. The main reason is that logging hides important information, which is visible in logs only to their author. Here is my blog post about this: Logging in Unit Tests, a Bad Practice.


Normally when you write tests one of the first thing that you learn is that a unit tests should not connect to anything - database, filesystem, internet. A unit test should be blazing fast and work regardless of the environment you work in. If it connects to something it's an integration test. I'd argue that using a logging framework which may significantly reduce the speed of unit tests is something that goes against the philosophy of unit testing. The whole idea is that you can run thousands, tens of thousands tests on your every whim. The ideal would be to have your unit test suite plugged to your save button (not really plausible but you get my drift).


A logging framework is pretty useless if it doesn't lets you enable/disable the logs. So feel free to add logs, just make sure you can enable/disable them separately from everything else


Tests have assertions. If something is missing in assertions, add more assertions. Logs serve for investigating an error after the fact. But when you do unit testing, you have all the power to automate the search for errors.

If you want to look into logs, it means, you don't have enough test cases and checks. Add them.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜