2

An Introduction to Unit Testing

WHAT'S IN THIS CHAPTER?

  • What a unit test is
  • How unit testing differs from other types of tests
  • How unit testing frameworks can help you write unit tests quickly and easily
  • Why mocking external resources in your test is important when practicing TDD
  • A brief overview of the NUnit unit testing framework and the Moq mocking framework, two very popular TDD tools in the .NET world

Unit testing (UT) is the cornerstone of test-driven development (TDD). When your unit tests are properly aligned with and correctly reflect your business requirements, they almost become a living design document — one that can validate the code you've written with the push of a button. Unit tests are not difficult to write, although they do require a minor change to how you usually approach writing software. They also represent a few new concepts to master, such as code isolation and the idea of having stand-in or mock objects to enable your tests to focus on only the code that is being tested. These new concepts are integral to TDD. The ability to write isolated, repeatable, and focused tests allows you to ensure that your code is meeting the business's needs and can continue to evolve without those changes disrupting the code's fidelity to the business's needs. To help you with these concepts, tools and frameworks such as NUnit and Moq can make your development process easier, faster, and more rewarding.

Get Professional Test-Driven Development with C#: Developing Real World Applications with TDD 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.