Unit Tests As Throw Away Design

Jan Vermeir

Unit tests are brittle: if you change the class under test there’s a more than average chance that you will have to change a load of unit test as well. On the other hand unit tests help you think about design on a micro level. The test shows what a method is supposed to do, without room for the interpretation errors you get when using abstractions as design.

So, should we use unit tests or not?

A few weeks ago we had a discussion at one of our knowledge exchange sessions about unit tests. The problem is that tests are brittle because they strongly depend on the code being tested. If you change the code under test it’s highly likely the test will break. If the test has intimate knowledge of the code being tested, this is what we would expect. If you take the test driven paradigm to the max, unit tests are supposed to test only a single class. So the class under test may not depend on any other code. A class that is independent of all other code is a rare thing, so we need a trick to ensure independence in tests. The standard solution is to mock all classes the class under test depends on. Because mocks and unit tests make assumptions explicit, they are a good way to design systems. Test code not only ensures the business code works, but it also documents business code. Making a change in this setup, however, may lead to quite a lot of work that doesn’t directly provide business value. The tests and mocks will not make it into production; they are only needed to create the real thing.

No problem so far, but lets go back in time to the hey day of case tools and code generators, just before the Internet hype hit us and made us all forget most of what we learned before. Back when life was easy and you never got hit by unpredictable loads and split second marketing decisions.

Way back, end of the eighties, systems were mainly designed in two phases. During design the system was specified using entity-relationship diagrams and functional decomposition. The first ended up as tables in a database. The functions were transformed to either reports or input forms. Rules about the data were specified as constraints that got translated to client and server side code in some database dialect. The transformation was mostly a manual process with only one exception: translating an ERD into a database schema is usually trivial and could therefore be automated easily. The input screen definitions specified data usage: what can a user change in the database using the input screen. Because constraints were specified on the data, it was easy to generate code to check the constraints in the input form as well.

But that was basically as far as you got. Code generators got you roughly half way there and everything else was craftsmanship with VI or Emacs. Tools got better in the Windows 3.11 and 95 timeframe but still we were writing most code using a text editor. The consequence was that the original design became less and less valuable over time: the details were added to the code while the design remained a stack of paper in a binder. Under such circumstances there is hardly any value in changing the design while coding and consequently the design slowly gathers dust on a shelf.

On a side note: we used to say designs were meant to convey the meaning of the system to end users. They were supposed to be understandable for customers. In my experience this is not the case. As IT personnel we live in a different parallel universe. Any form of abstraction is just too much for other people. If you don’t work in the IT industry you can say if the system does what you want it to do once you see it and not earlier. No amount of ERDs or class diagrams or whatever is going to help, ever. Only a working system will.

Back to unit tests. The parallel should be clear: logically Java code follows JUnit tests like Oracle Forms follows function diagrams. Unit test don’t make it into production just like ERDs. When you change a unit test the code has to be changed. Here the analogy starts to come apart: if you changed the ERD in the eighties typically nothing would break. Worse, you could have one group of people happily changing code while another group of people was changing the binders with ERDs and both would be happy living in parallel universes until the system went live.

Unit tests as design are like the recognition of the side note above: if we can’t make end users understand us anyway why bother them and ourselves with abstractions nobody uses? We might as well write test code that at least is useful during development.

The question is whether it is necessary to keep all this test code around even after we’re done with the part of the system the test code is validating. After a while the test becomes a nuisance: something you have to change while you actually want to change code. Fist thick binders full of functional decompositions spring to mind.

So how much unit tests do we actually need? My answer is that a unit test is required for interfaces to a component or module. A module is defined as a set of 10-15 classes accessible through an interface.

Since one of the most important requirements of unit tests is that they should be very fast, we need mocks if the module connects to slow or heavy weight resources like web services or databases. Mocking a database might be considered a bridge too far: databases are fast and easily available so often you might as well use the real thing in stead of a mock.

You should start development of the module by writing a test for the interface. It is perfectly acceptable to develop code inside the module by writing a unit test for a method of a class inside the module, but you should treat this as throw-away code. Once you’re done you can delete the test. Or you can let it lie around to be deleted when it gets in the way. The really important test is the test at the module level. This test has to be maintained along with the production code. All other tests are like the scraps of stone left behind after carving a statue.

Comments (4)

  1. Vikas Hazrati - Reply

    January 16, 2008 at 6:56 am

    Great thought provoking post.

    The question is whether it is necessary to keep all this test code around even after we’re done with the part of the system the test code is validating.

    In my view the value of unit tests remains much after the system is done. As per XP there are only 2 valuable artifacts in a system i.e. tests and code. The tests would help to understand the requirements.They help a new person get on board quickly. They also serve as an invaluable tool for refactoring of code.

  2. Machiel Groeneveld - Reply

    January 16, 2008 at 8:31 am

    I agree that tests on a component level are ultimately more important than the unit level. The problem is that it's very hard to get a high code coverage with only component level tests, so if you throw away the unit tests, you might introduce an error in a corner case that will never get caught. I do think that most unit tests today are still way too brittle. If they wouldn't be so tightly bound to the implementation, we could keep them around longer.

  3. Martin Schapendonk - Reply

    January 16, 2008 at 10:34 am

    Are you confident enough to make changes at class level and depend on the UT at module level?

    If yes, than why bother to write the tests at class level anyway?

    One reason for UT's existence is to build confidence with the developer to make changes at will, knowing that some test, somewhere, will fail if he makes the wrong change.

    Throwing away UT might mess up your team velocity in the future, because people loose confidence in the test suite and have to analyse their changes more thoroughly.

  4. Lars Vonk - Reply

    January 16, 2008 at 11:53 pm

    Hi Jan,

    Nice read ;-).

    From my experience I much rather see more of these module tests as these tests will really help you to refactor and proof your code is working as expected. After all in the end it matters that the different classes interact together right? A tool like FitNesse is excellent for these kind of tests. I would only write UT on class level for classes that have a lot of corner cases.

Add a Comment