Sunday, May 1, 2011

Unit Testing / Extreme Programming

Unit Testing advocates use language that you hear from religious advocates.

"You can't write software without unit tests". "You must unit test your code".

That sounds a lot like:

"You will go to hell if you sin". "If you don't accept Jesus as your lord and savior, you will go to hell".

Where does that leave us?

You can write software without unit tests. A lot of software has and will be written without unit tests. No human will go to hell for not accepting Jesus as their lord savior.

Same question, where does that leave us?

I may have advocated the use of unit tests. I think the concept is beneficial. BUT there are a couple of issues. Developers not writing unit tests is a big one (I was thinking about creating a spreadsheet, software from github and sourceforge, projects that have unit tests and those that don't). Why don't developers write unit tests? Because they don't have to. That is the main reason. You need a compiler to compile code for languages that need to be compiled. You need some runtime. You need to your code. But you aren't required to write unit tests in order to execute your code.

So, maybe we need some form of system that checks for unit tests before allowing your code to run. In my mind, I kind of feel that unit tests are an extra compilation step or runtime step. Maybe if that sort of system was in place, we might see developers wanting to write more unit tests. I also want more information from the unit tests. It is great to know if the test passed or failed but I would like to see similar possible inputs fed into the unit testing systems and the output (possibly with a visual graph). QuickCheck and ScalaCheck are example tools that automate unit testing.

And like with most with religious practices, sometimes the underlying message is important but the message is delivered improperly.

If I say: "Write unit tests or else". If developers don't see a need or want to write unit tests, they won't. So telling developers to write unit tests doesn't really hold weight.

Now, I would like to hear, "Write unit tests in order to isolate this piece of code and test reasonable inputs and check the output of the tests. This is a core class in our library and we must validate the possible inputs and expected outputs. It will be interesting to look at this particular module, visualize the possible inputs from the calling client code and see what happens. Plus, isolating this particular component and testing it takes less time than running the entire system as a whole". That blurb won't make for a very good headline at an extreme programming conference but I think it is gets the point across, more so than, "Write unit tests or else". But my overly verbose blurb on unit testing still doesn't force developers to write unit tests. Even those that are well versed on testing will not write unit tests when under some critical deadline.

In Summary

I think the concept behind unit tests is beneficial but I still feel that implementation systems behind unit testing are still lacking. This is one possible reason that developers don't always write them (jUnit is one example of a system that lacks).

Response to critics of this post?

I have noticed if you step on any person's religion, critics tend to come at you from all directions. I don't mean to step on the unit testing religion. I am not advocating to stop writing unit tests. But I do want to point out that software is written without them. Maybe if we create a platform that forces the developer to write unit tests as part of some compilation or build step, then developers will start thinking about the practice.

How do you know that developers aren't writing unit tests? If it is possible not to write unit tests, then it is possible that developers won't write them. Also, I tend to want to include the edge cases of software development when talking about unit tests. Are people writing unit tests for embedded software development? Linux device driver development? Any Linux software development? Complex J2EE server environments?