What is Unit Testing?

What is Unit Testing?

So what is unit testing? How would you define it?

Brian Marick made the observation that unit testing is an unfortunate term propagated by XP for something that really ought to be called "developer testing." I agree, with one caveat, and that is that developer testing seems to work best when developers focus their testing on units of code. Let's change our terminology, though, and ask a second question: "What is developer testing?" My definition is:

"Developer testing is the practice of verifying the behavior of a 'unit' of code by repeatably interacting with its external interface (inputs as well as outputs)."

That's about as clear as mud, so let's look at some of the key parts of that definition. First of all, developer testing is a practice. It is not something that just happens by chance, but rather a conscious behavior that developers choose to do. Second, testing is about verification, i.e. it makes certain checks that it expects to succeed. Third, developer testing is focused on verifying behavior. It is not necessary to directly test algorithms outside of how they affect the behavior of a unit of code. Fourth, developer testing focuses on the external interface of a unit of code. In an object-oriented language such as Ruby, this almost always involves testing the public interface of a class, and not testing its internal data or private interface. If the public interface works, those things either work or are not used. Finally, it is important to test both those interfaces that a class exposes as well its proper use of those interfaces it depends on.

So that's what developer testing is... but how do you do it?

Copyright (c) 2001 by Nathaniel Talbott. All Rights Reserved.