Thursday, July 24, 2008

The eternal question, what are “Unit Tests” and are they if they talk to the database/file system/etc?

haacked - Unit Test Boundaries


Michael Feathers takes a harder stance in saying…

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.

Speed isn’t the only benefit of following these rules. In order to make sure your tests don’t reach across boundaries, you have to make sure the unit under test is easily decoupled from code across its boundary, which provides benefits for the code being tested.


In our Sprint Retrospective this week, our team decided that we were not happy with the level of unit testing we did in our last Sprint. That we felt we could do a much better job and we were not happy with the technical debt we incurred by not having them. So we made it a priority item for the next Sprint (which the planning meeting is on Monday) that we’re going to push much harder on unit testing. That we’re going to try to change our mindsets and development culture to try to make unit test writing a first class development task.

But since this is new to most of us, we’re still trying to figure out what unit testing “is” and how to do it in our real world.

That’s why this post caught my eye. It hit directly on a point that I want to make sure we all understand and execute on… That unit tests are not tightly coupled to anything, that they test one thing and can survive the test of time (i.e. I want to write unit tests that I can run 5 years from now, when we’re on a different domain, network, servers, shares, building, planet, etc)

No comments: