C# Unit Testing with NUnit

Authors: Jason Roberts, Mel Grubb, Matthew Renze, Vladimir Khorikov

Unit testing has become standard practice for today's software developers and NUnit is one of the most popular and mature unit testing frameworks available for .NET. The goal of... Read more

What you will learn

  • Creating unit tests using NUnit
  • Running unit tests from Visual Studio
  • Mocking dependencies using the Moq framework
  • Validating method calls using the Moq framework
  • Creating partial mock classes
  • Writing testable code
  • Build a unit testing suite for your application
  • How to use AutoFixture to create test data


Familiarity with the C# programming language


The courses in this section will help you master the basics of writing unit tests. First, you will learn how to use NUnit, one of the most popular unit testing frameworks used in .NET. Then you will learn how to Moq, a mocking framework which allows you to replace dependencies in your code when you write tests. Mastering these two frameworks will help you create unit tests for a wide-ranging set of scenarios you encounter when writing code.

Introduction to .NET Testing with NUnit 3

by Jason Roberts

Dec 17, 2018 / 2h 6m

2h 6m

Start Course

Releasing bug-ridden code to production is a sure fire way of creating unhappy end-users, management, and development teams. Nobody likes to receive a phone call in the middle of the night saying that the production system has gone down and needs fixing, immediately. In this course, Introduction to .NET Testing with NUnit 3, you will gain the ability to create automated tests for you code. First, you will learn how to set up a test project and run tests. Next, you will discover the different ways to assert on test results such as numbers, collections, and expected exceptions. Finally, you will explore how to customize the test execution lifecycle, create data driven tests, and customize NUnit. When you’re finished with this course, you will have the skills and knowledge of automated testing with NUnit needed to help prevent bugs from making their way into production.

Table of contents
  1. Course Overview0m
  2. Writing Your First NUnit Test16m
  3. Understanding NUnit Tests19m
  4. Asserting on Different Types of Results36m
  5. Controlling Test Execution22m
  6. Creating Data Driven Tests and Reducing Test Code Duplication31m

Mocking with Moq and NUnit

by Jason Roberts

Feb 22, 2019 / 1h 53m

1h 53m

Start Course

Writing effective unit tests can be tricky when dependencies between classes make it hard to separate what's being tested from the rest of the system. In this course, Mocking with Moq and NUnit, you will gain the ability to easily isolate parts of your codebase for testing purposes. First, you will learn how to substitute real dependencies for mock objects. Next, you will discover how to configure mocked methods, properties, exceptions, and events. Finally, you will explore how to create behavior-based tests and also how to create partial mocks from existing classes. When you’re finished with this course, you will have the skills and knowledge of mocking with Moq needed to create automated tests that use mock objects.

Table of contents
  1. Course Overview1m
  2. Getting Started with Moq27m
  3. Configuring Mock Method Return Values20m
  4. Working with Mock Properties 17m
  5. Checking That Mock Methods and Properties Are Used20m
  6. Using Partial Mocks and Advanced Mocking Techniques25m

Creating Maintainable Contexts for Automated Testing

by Mel Grubb

Jun 4, 2019 / 1h 26m

1h 26m

Start Course

In this course, you'll see a demonstration of a typical hand-built test context, and the factors that make it brittle and hard to maintain. By using software principles we're all familiar with, and some patterns we may not have seen before, we can create contexts that remain focused on the information most important to understanding what the tests are trying to tell your team. We'll start with general principles, and then move into the Object Mother pattern, a common approach to extracting test context creation tasks. From there, we'll move into the Object Builder pattern, a more modern and expressive evolution of the Object Mother. Finally, you'll see how to use Roslyn-based C# scripts to generate the tedious parts of the Object Builder pattern, so that you can get back to the interesting stuff. When you’re finished with this course, you will have an appreciation for what makes for a good test context, and the skills to apply this knowledge to your current and future projects.

Table of contents
  1. Course Overview1m
  2. The Problem with Test Suites14m
  3. Extracting Test Data into an "Object Mother"18m
  4. Evolving an Object Mother into a Builder20m
  5. Automating Builder Creation with Code Generation30m


After you have mastered the basics, the courses in this section help you write better unit tests. You will learn how to write code that is more testable from the start and how by doing so, you improve the overall design of your code. Then you will learn about how to go from writing individual unit tests to building a whole suite of tests for your application in a thoughtful, pragmatic way.

Writing Testable Code

by Matthew Renze

May 5, 2017 / 2h 2m

2h 2m

Start Course

Testable code is code that makes automated testing quick, easy, and enjoyable. In this course, Writing Testable Code, you'll learn how to write code that is easy to test. These skills are necessary to refactor code that is difficult to test into code that is easily testable. First, you'll be introduced to testable code, and how creating seams in your code makes code testable. Next, you'll learn a series of best practices that makes testing code easier such as, simplifying object construction, working with dependencies, and managing application state. Finally, you'll explore best practices further by walking through a code demo of each practice so you can see first-hand how they're implemented. These skills will make practices like unit testing significantly easier for you. By the end of this course, you'll have the skills necessary for writing testable code.

Table of contents
  1. Course Overview1m
  2. Introduction19m
  3. Creating Seams in Code22m
  4. Constructing Testable Objects19m
  5. Working with Dependencies14m
  6. Managing Application State21m
  7. Maintaining Single Responsibility15m
  8. Next Steps8m

Building a Pragmatic Unit Test Suite

by Vladimir Khorikov

Oct 7, 2016 / 3h 42m

3h 42m

Start Course

Unit testing is everywhere. It is one of the most widely spread engineering practices in the world of software development. It’s important to know how to apply it in a way that helps, and not prevents you from building successful products. This course, Building a Pragmatic Unit Test Suite, is going to show you how to do that. First, you'll learn about what makes a test valuable. Next, you'll learn about the different styles of unit testing, as well as the use of mocks. Finally, you'll wrap up the course by learning about integration testing and unit testing anti-patterns. By the end of this course, you’ll know how to make the most out of your unit and integration tests.

Table of contents
  1. Course Overview1m
  2. Introduction30m
  3. Understanding Styles of Unit Testing35m
  4. Getting the Most out of Your Unit Tests40m
  5. Refactoring to More Valuable Unit Tests54m
  6. Getting the Most out of Your Integration Tests37m
  7. Avoiding Unit Testing Anti-patterns22m


There are a number of open source libraries that can help you write more readable, maintainable unit tests, and the courses in this section are designed to introduce you to the most useful of these libraries. A short investment of time in watching these courses will save you lots of time later as you learn about the advanced capabilities these libraries add to your unit testing toolkit.

Improving Unit Tests with Fluent Assertions

by Jason Roberts

May 6, 2019 / 40m


Start Course

When tests fail it can be hard to understand exactly what caused the failures. If test failure messages are ambiguous or not detailed enough you sometimes have to fire up the debugger and run the test in debug mode just to find out what the failure was. In this course, Improving Unit Tests with Fluent Assertions, you will learn foundational knowledge of Fluent Assertions. First, you will discover how to improve the readability of your test code. Next, you will explore the different ways to assert on different result types such as strings, floating point numbers, collections, and exceptions. Finally, you will gain an understanding of how to improve test failure messages to reduce the need to waste time debugging through failing tests. When you are finished with this course, you will have the skills and knowledge of Fluent Assertions needed to improve readability and test failure messages for automated tests.

Table of contents
  1. Course Overview1m
  2. Improving Unit Tests with Fluent Assertions38m

Better .NET Unit Tests with AutoFixture: Get Started

by Jason Roberts

Apr 4, 2015 / 1h 58m

1h 58m

Start Course

The open source AutoFixture library helps reduce the amount of unnecessary code in the arrange phases of test code. It does this by automatically supplying values for test data that are required for the test to function, but where the exact value is unimportant. In addition to simple types, AutoFixture also creates object graphs of test data, reducing the amount of test code that needs to be written and reducing the chances that tests will break compilation when changes are made to production code.

Table of contents
  1. Introducing AutoFixture17m
  2. Creating Anonymous Test Data38m
  3. Customizing Object Creation28m
  4. Writing Less Test Code and Improving Test Maintenance33m

Approval Tests for .NET

by Jason Roberts

Sep 17, 2014 / 2h 34m

2h 34m

Start Course

ApprovalTests is an open source library that enables unit tests to go beyond simple asserts. It allows complex verification of system output that would otherwise be hard and time-consuming to implement using conventional asserting. It is also an essential tool to quickly get legacy code under test before refactoring it.

Table of contents
  1. Introduction15m
  2. Verifying Objects and Arrays25m
  3. Reporters36m
  4. Testing Application Views45m
  5. Introduction to Refactoring Legacy Code With Approval Tests31m