One To Many Class to Test Classes

Dave Astels argues pretty strongly that though Test Driven Development is catching on, many people could still be practicing it better. One issue is the idea of one-to-one production classes to test classes:

“A lot of people have a mantra. We’ll have ClassX and ClassXTest.”
– Dave Astels

His point is well taken, and honestly I often fall into the trap of doing approximately one-to-one test to production classes. The brilliant advice is to:

“Have lots of little test classes. Should be building test classes around the setup or fixture that your building. That gets you a long ways towards BDD (Behavior Driven Development).”
— Dave Astels

So a code smell when writing unit tests is that you have tests that depend on the setup and tests that don’t use it or depend on another setup. This is a sign that you’re not testing just one behavior and you should refactor things out to two test classes.

A very simple idea, but very easy to recognize while you’re coding. (This advice comes from a talk he gave at Google, but its a very enjoyable talk for about 45 minutes on BDD)

3 responses to “One To Many Class to Test Classes”

  1. I blogged about that on January 25th:

    Now, as Dave says, I organize my tests around the fixtures, and not around functionnality.

  2. Ed Gibbs says:

    Enjoyed your article. It is always a lot nicer if you can have a single assertion per test method, and a lot quicker to glean what broke.

    Strangely enough your bio lists the Adam as your first computer. It was probably my 4th or so, but I do miss the high speed tape drive.