The Artima Developer Community
Sponsored Link

.NET Buzz Forum
Unit Testing Rules

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
Peter G Provost

Posts: 849
Nickname: pprovost
Registered: Aug, 2003

Peter G Provost is a Solution Architect for Interlink Group in Denver, CO.
Unit Testing Rules Posted: Sep 18, 2005 5:22 PM
Reply to this message Reply

This post originated from an RSS feed registered with .NET Buzz by Peter G Provost.
Original Post: Unit Testing Rules
Feed Title: Peter Provost's Geek Noise
Feed URL: /error.aspx?aspxerrorpath=/Rss.aspx
Feed Description: Technology news, development articles, Microsoft .NET, and other stuff...
Latest .NET Buzz Posts
Latest .NET Buzz Posts by Peter G Provost
Latest Posts From Peter Provost's Geek Noise

Advertisement

I was just sent this post by Michael Feathers where he asserts:

A test is not a unit test if:

  • It talks to the database
  • It communicates across the network
  • It touches the file system
  • It can't run at the same time as any of your other unit tests
  • You have to do special things to your environment (such as editing config files) to run it.

Tests that do these things aren't bad. Often they are worth writing, and they can be written in a unit test harness. However, it is important to be able to separate them from true unit tests so that we can keep a set of tests that we can run fast whenever we make our changes.

This is a very interesting set of rules and for the most part I agree with Michael. When I do TDD I try to avoid these things as much as possible. But when I find myself needing to break one of these "rules", I almost always end up doing the same thing.

I create a single minded little class that does that one thing. Then I test it to ensure that it does it. Then I walk away from it.

As an example, consider a business class that needs to pull some information from an XML file on the file system. That is what the customer requirement was. However, I would never even consider putting XML or file system logic in that business component. Instead I'll write a class that talks to the file system and returns some sort of domain model (not XML). I'm now free to test my business component using a mock of this persistence loader, completely in isolation of the file system.

I think Michael does the same thing. He continues:

That might sound a little severe, but it is medicine for a common problem. Generally, unit tests are supposed to be small, they test a method or the interaction of a couple of methods. When you pull the database, sockets, or file system access into your unit tests, they aren’t really about those methods any more; they are about the integration of your code with that other software. If you write code in a way which separates your logic from OS and vendor services, you not only get faster unit tests, you get a ‘binary chop’ that allows you to discover whether the problem is in your logic or in the things are you interfacing with. If all the unit tests pass but the other tests (the ones not using mocks) don’t, you are far closer to isolating the problem.

Frankly, we need both kinds of tests, but these "pure" unit tests are undervalued.

I guess the only part I don't really like is that distinction between other tests and pure unit tests (emphasis mine). I don't think it is safe to ignore these other tests and I don't know what to call them other than unit tests, so I'm hesitant to accept the part of Michael's post where he says, "A test is not a unit test if..."

I would much rather say this: "A unit test should try to avoid..." and then follow it with his five bullets.

Read: Unit Testing Rules

Topic: Java gets its Champions Previous Topic   Next Topic Topic: New and Notable 78

Sponsored Links



Google
  Web Artima.com   

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