The Meaning of Unit Testing and Test-Driven Development

When we talk about software testing, we are referring to a whole host of different activities, including unit testing, acceptance testing, exploratory testing, performance testing, and scalability testing. To set the stage for this chapter, it's helpful to start with a shared understanding of what is meant by unit testing — the subject of this section.

Defining Unit Testing

Most developers have some exposure to unit testing and some opinion on what works best. In our experience, the following attributes tend to be present in most long-term successful unit testing:

  • Testing small pieces of production code (“units”)
  • Testing in isolation from the rest of the production code
  • Testing only public endpoints
  • Running the tests gets an automated pass/fail result

The following sections examine each of these rules and how they impact the way you write unit tests.

Testing Small Pieces of Code

When writing a unit test, you're often looking for the smallest piece of functionality that you can reasonably test. In an object-oriented language like C#, this usually means nothing larger than a class, and in most cases, you're testing a single method of a class. The reason to test small pieces of code is that it allows you to write simple tests. These tests should be easy to understand so that you can verify that you're accurately testing what you intended.

Source code is read far more often than it is written; this is especially important in ...

Get Professional ASP.NET MVC 4 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.