Book Image

C# and .NET Core Test Driven Development

By : Ayobami Adewole
Book Image

C# and .NET Core Test Driven Development

By: Ayobami Adewole

Overview of this book

This book guides developers to create robust, production-ready C# 7 and .NET Core applications through the practice of test-driven development process. In C# and .NET Core Test-Driven Development, you will learn the different stages of the TDD life cycle, basics of TDD, best practices, and anti-patterns. It will teach you how to create an ASP.NET Core MVC sample application, write testable code with SOLID principles and set up a dependency injection for your sample application. Next, you will learn the xUnit testing framework and learn how to use its attributes and assertions. You’ll see how to create data-driven unit tests and mock dependencies in your code. You will understand the difference between running and debugging your tests on .NET Core on LINUX versus Windows and Visual Studio. As you move forward, you will be able to create a healthy continuous integration process for your sample application using GitHub, TeamCity, Cake, and Microsoft VSTS. By the end of this book, you will have learned how to write clean and robust code through the effective practice of TDD, set up CI build steps to test and build applications as well as how to package application for deployment on NuGet.
Table of Contents (11 chapters)

xUnit.net shared test context

The test context setup is done in the test class constructor, since the test setup is not applicable in xUnit. For every test, xUnit creates a new instance of the test class, which implies that the codes in the class constructor are run for each test.

Oftentimes, it is desirable for unit test classes to share a test context because it can be expensive to create and clean up test contexts. xUnit offers three approaches to achieve this:

  • Constructor and dispose: Sharing setup or cleanup code without having to share the object instances
  • Class fixtures: Sharing object instances across tests in a single class
  • Collection fixtures: Sharing object instances across multiple test classes

You should use constructor and dispose when you want a fresh test context for every test in a test class. In the following code, the context object will be constructed and...