Chapter 1. The basics of unit testing


This chapter covers

  • Defining a unit test
  • Contrasting unit testing with integration testing
  • Exploring a simple unit testing example
  • Understanding test-driven development

There’s always a first step: the first time you wrote a program, the first time you failed a project, and the first time you succeeded in what you were trying to accomplish. You never forget your first time, and I hope you won’t forget your first tests. You may have already written a few tests, and you may even remember them as being bad, awkward, slow, or unmaintainable. (Most people do.) On a more upbeat note, you may have had a great first experience with unit tests, and you’re reading this to see what more you might be missing.

This chapter will first analyze the “classic” definition of a unit test and compare it to the concept of integration testing. This distinction is confusing to many. Then we’ll look at the pros and cons of unit testing versus integration testing and develop a better definition of a “good” unit test. We’ll finish with a look at test-driven development, because it’s often associated with unit testing. Throughout the chapter, I’ll also touch on concepts that are explained more thoroughly elsewhere in the book.

Let’s begin by defining what a unit test should be.

1.1. Defining unit testing, step by step

1.2. Properties of a good unit test

1.3. Integration tests

1.4. What makes unit tests good

1.5. A simple unit test example

1.6. Test-driven development

1.7. The three core skills of successful TDD

1.8. Summary