Chapter 2. A first unit test

 

This chapter covers

  • Exploring unit testing frameworks in .NET
  • Writing your first test with NUnit
  • Working with the NUnit attributes
  • Understanding the three output types of a unit of work

When I first started writing unit tests with a real unit testing framework, there was little documentation, and the frameworks I worked with didn’t have proper examples. (I was mostly coding in VB 5 and 6 at the time.) It was a challenge learning to work with them, and I started out writing rather poor tests. Fortunately, times have changed.

This chapter will get you started writing tests even if you have no idea where to start. It will get you well on your way to writing real-world unit tests with a framework called NUnit—a .NET unit testing framework. It’s my favorite framework in .NET for unit testing because it’s easy to use, easy to remember, and has lots of great features.

There are other frameworks in .NET, including some with more features, but NUnit is where I always start. If the need arises, I sometimes then expand to a different framework. We’ll look at how NUnit works, its syntax, and how to run it and get feedback when the test fails or passes. To accomplish this, I’ll introduce a small software project that we’ll use throughout the book to explore testing techniques and best practices.

You may feel like NUnit is forced on you in this book. Why not use the built-in MSTest framework in Visual Studio? The answer consists of two parts:

2.1. Frameworks for unit testing

2.2. Introducing the LogAn project

2.3. First steps with NUnit

2.4. Writing your first test

2.5. Refactoring to parameterized tests

2.6. More NUnit attributes

2.7. Testing results that are system state changes instead of return values

2.8. Summary

sitemap