The Artima Developer Community
Sponsored Link

Java Buzz Forum
Unit tests with log statements are a code smell

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Rod Waldhoff

Posts: 99
Nickname: rwald
Registered: Jun, 2003

Rod Waldhoff is.
Unit tests with log statements are a code smell Posted: Jun 26, 2003 2:01 PM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Rod Waldhoff.
Original Post: Unit tests with log statements are a code smell
Feed Title: Rod Waldhoff: Java Channel
Feed URL: http://radio-weblogs.com/0122027/categories/java/rss.xml
Feed Description: about the Java programming language
Latest Java Buzz Posts
Latest Java Buzz Posts by Rod Waldhoff
Latest Posts From Rod Waldhoff: Java Channel

Advertisement

While this is not an earth shattering realization, I've come hold the opinion that log statements (log4j, logkit, java.util.logging, commons-logging, what have you) within unit tests are a code smell, perhaps universally.

While I'll sometimes add a few System.out.println calls to a unit test while I'm trying to diagnose a particular failure, configuring a full-blown logging setup within a unit test always seemed like more time and trouble than it was worth. From time to time I'll encounter a heavily logged TestCase in some code base I'm working with. The more I work with such TestCases, the more I find this to be an indication that something is not right.

Here's why:

  • I find it hard to imagine a test first/test driven development approach that leads to log statements within unit tests (but I can imagine "test last" approaches that will). The presence of logging strongly suggests that the tested code was not developed in a test driven fashion.
  • The role of logging frameworks and that of automated unit testing frameworks are at odds. Logging calls provide persistent, if only intermittently used, diagnostic and informational messages, typically intended for manual inspection *. Automated unit tests are meant to be self-interpreting, success or failure should be obvious without manual inspection. The use of diagnostic or informational log messages within unit tests suggests your tests aren't sufficiently self-interpreting.
  • Anecdotally, the objects being tested by these cases are brittle in the face of change. This may stem from poor factoring: there are too many subtle and perhaps unplanned interactions between methods, or methods aren't well focused enough to allow for orthogonal changes.
  • Anecdotally, test failures remain difficult to diagnose and fix despite the log messages. This also stems from poor factoring: the individual test cases and assertions are too coarse grained to help identify the root cause of a test failure.

* If you find yourself wanting programmatic inspection of log messages, I'll suggest you're looking for messaging, not logging.

Read: Unit tests with log statements are a code smell

Topic: Dot Net Weblogs Previous Topic   Next Topic Topic: Database Refactorings

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use