Chapter 3. The anatomy of a unit test

 

This chapter covers

  • The structure of a unit test
  • Unit test naming best practices
  • Working with parameterized tests
  • Working with fluent assertions

In this remaining chapter of part 1, I’ll give you a refresher on some basic topics. I’ll go over the structure of a typical unit test, which is usually represented by the arrange, act, and assert (AAA) pattern. I’ll also show the unit testing framework of my choice—xUnit—and explain why I’m using it and not one of its competitors.

Along the way, we’ll talk about naming unit tests. There are quite a few competing pieces of advice on this topic, and unfortunately, most of them don’t do a good enough job improving your unit tests. In this chapter, I describe those less-useful naming practices and show why they usually aren’t the best choice. Instead of those practices, I give you an alternative—a simple, easy-to-follow guideline for naming tests in a way that makes them readable not only to the programmer who wrote them, but also to any other person familiar with the problem domain.

Finally, I’ll talk about some features of the framework that help streamline the process of unit testing. Don’t worry about this information being too specific to C# and .NET; most unit testing frameworks exhibit similar functionality, regardless of the programming language. If you learn one of them, you won’t have problems working with another.

3.1. How to structure a unit test

3.2. Exploring the xUnit testing framework

3.3. Reusing test fixtures between tests

3.4. Naming a unit test

3.5. Refactoring to parameterized tests

3.6. Using an assertion library to further improve test readability

Summary

sitemap