Annoying Unit Tests

If you have read much in this blog, you realize that I have a very mixed relationship with Unit Testing. Half the time, I think they cause more trouble than they are worth. I mean you write code for code and coders like to write code and then the code ends up in the unit test code. So much code, that creates nothing but code that needs to be maintained.

No perceived value to the customer. Just lots of code. It is hard to justify.

Now, I do unit test. Far more today than even a year ago. I am not a 100% coverage person but I do it from time to time. Then I run into someone else’s unit tests and I stop for a while or use it only as disposable code and trash it at the end of development. Why do I do such things? I look at other people’s unit tests and realize. These are really bad. These are annoying unit tests. I don’t want to do that to someone else. Better no tests than a cluster fuck of bad, annoying unit tests.

So you write annoying unit tests?

No, I don’t think I write annoying unit tests. But when you look at true awfulness, you get self conscious about doing it to others. Most of the unit tests I have seen have been tightly coupled, condition laden nightmares of dependency. The very definition of an annoying unit test.

If your so smart, what should they be doing?

First, if you are using a loop, just stop. Just stop. I am sure there is somewhere a loop in a unit test is a good idea. Personally, I have not run into it but I am sure there is an exception. Your case is not likely one of them so stop.

Next, please, please, pretty please with a sugar on top with whipped cream and a cherry, be cautious about putting conditionals in your tests. If you put an if, please just end if immediately after, ok.

Visual Basic jokes aside, you should not need under most circumstances a conditional of any kind.

Now I get you need to test for conditions that are handled somewhere else rather than the code you are working on but just be careful.

Unlike children, unit tests play better alone. Keep your test to themselves and do not call another test from inside the current test. Unit tests should not be their brother’s keeper.

Finally, please follow the Arrange, Act and Assert pattern in your unit testing.

You don’t have to use stubs or mocks to make me happy. The unit testing Nazi, yes, me not so much but the test should be very minimal coding sorts of things. Just keep it simple.

Leave a Reply

Your email address will not be published. Required fields are marked *