O'Reilly logo

Professional ASP.NET MVC 4 by Scott Hanselman, K. Scott Allen, Brad Wilson, Phil Haack, Jon Galloway

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required