C# Unit Testing with xUnit

Paths

C# Unit Testing with xUnit

Authors: Jason Roberts, Mel Grubb, Thomas Claudius Huber, Matthew Renze

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

What you will learn

  • Creating unit tests using xUnit
  • 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

Pre-requisites

Familiarity with the C# programming language

Beginner

The courses in this section will help you master the basics of writing unit tests. First, you will learn how to use xUnit, one of the most popular unit testing frameworks used in .NET. Then you will learn how to use 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.

Testing .NET Code with xUnit.net: Getting Started

by Jason Roberts

Sep 5, 2019 / 2h 13m

2h 13m

Start Course
Description

It's usually more expensive, time-consuming, and stressful to fix software bugs after they have been released to production environments. A good set of automated tests helps to verify that the application is working as expected and helps to prevent bugs from getting into production. In this course, Testing .NET Code with xUnit.net: Getting Started, you'll get an overview of automated testing to empower you to be able to start testing your own applications. First, you'll learn how to create xUnit.net automated tests that verify different types of results. Next, you'll explore how to customize and control test execution and categorization. Finally, you'll discover how to reduce the amount of test code you need to write by using the power of data-driven tests. When you're finished with this course, you'll have a foundational knowledge of xUnit.net that will help you to start writing your own automated tests.

Table of contents
  1. Course Overview
  2. Getting Started
  3. Determining Passing and Failing Tests with Asserts
  4. Understanding and Controlling Test Execution
  5. Creating Data-driven Tests

Mocking with Moq and xUnit

by Jason Roberts

May 26, 2020 / 2h 14m

2h 14m

Start Course
Description

Writing unit tests can be difficult, time-consuming, and slow when you can't isolate the classes you want to test from the rest of the system. In this course, Mocking with Moq and xUnit, you'll learn how to create mocks and use them as dependencies to the classes you want to test. First, you'll discover how to configure mocked methods and properties to return specific values. Next, you'll learn how to perform behavior/interaction testing to check your classes are interacting correctly with their dependencies. Finally, you'll explore how to setup mock exceptions, mock events, and use additional mocking techniques. When you're finished with this course, you'll have the necessary knowledge to use Moq to unit test your classes in isolation by creating and using mock objects.

Table of contents
  1. Course Overview
  2. Getting Started with Mocking and Moq
  3. Configuring Mocked Methods
  4. Configuring Mock Object Properties
  5. Implementing Behavior Verification Tests
  6. Using Additional Moq Mocking Techniques

Creating Maintainable Contexts for Automated Testing

by Mel Grubb

Jun 4, 2019 / 1h 26m

1h 26m

Start Course
Description

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 Overview
  2. The Problem with Test Suites
  3. Extracting Test Data into an "Object Mother"
  4. Evolving an Object Mother into a Builder
  5. Automating Builder Creation with Code Generation

Intermediate

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

Test Driven Development in C#

by Thomas Claudius Huber

Jan 29, 2020 / 2h 26m

2h 26m

Start Course
Description

At the core of writing solid and robust C# code is a thorough knowledge of Test Driven Development (TDD). In this course, Test Driven Development in C#, you will learn the skills you need to be able to apply TDD in your .NET projects. First, you will discover the basics of TDD and about the red-green-refactor cycle. Next, you will learn how to test and implement business logic for a .NET Core application with TDD. You’ll use xUnit as a testing framework and Moq as a mocking library. Finally, your business logic will be used in an ASP.NET Core Razor Pages application, and you will use TDD to add additional features in that application. When you’re finished with this course, you’ll have a foundational knowledge of Test Driven Development that will help you as you move forward to test and implement new features in your C# projects.

Table of contents
  1. Course Overview
  2. Getting Started with Test Driven Development
  3. Testing and Implementing Business Logic
  4. Adding Features in an ASP.NET Core App

Writing Testable Code

by Matthew Renze

May 5, 2017 / 2h 2m

2h 2m

Start Course
Description

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 Overview
  2. Introduction
  3. Creating Seams in Code
  4. Constructing Testable Objects
  5. Working with Dependencies
  6. Managing Application State
  7. Maintaining Single Responsibility
  8. Next Steps

Advanced

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.

.NET Unit Testing with AutoFixture

by Jason Roberts

Aug 7, 2020 / 1h 59m

1h 59m

Start Course
Description

Automated tests can be time consuming to write and costly to maintain if every time you change the production code you must update all the tests. In this course, .NET Unit Testing with AutoFixture, you’ll learn to how to use AutoFixture to automatically generate anonymous test data. First, you’ll explore how to use AutoFixture to generate anonymous test data for both basic types and your own custom types. Next, you’ll discover powerful ways to control and customize how AutoFixture generates anonymous test objects. Finally, you’ll learn how to dramatically reduce the amount of test code you need to write by getting AutoFixture to also create the thing you’re testing and even automatically create mock dependencies. When you’re finished with this course, you’ll have the skills and knowledge of AutoFixture needed to write unit tests more quickly and easily.

Table of contents
  1. Course Overview
  2. Getting Started with AutoFixture
  3. Creating Anonymous Test Data and Objects with AutoFixture
  4. Customizing AutoFixture Object Creation
  5. Writing Less Test Code and Improving Test Maintenance

Improving Unit Tests with Fluent Assertions

by Jason Roberts

May 6, 2019 / 39m

39m

Start Course
Description

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 Overview
  2. Improving Unit Tests with Fluent Assertions
Offer Code *
Email * First name * Last name *
Company
Title
Phone
Country *

* Required field

Opt in for the latest promotions and events. You may unsubscribe at any time. Privacy Policy

By providing my phone number to Pluralsight and toggling this feature on, I agree and acknowledge that Pluralsight may use that number to contact me for marketing purposes, including using autodialed or pre-recorded calls and text messages. I understand that consent is not required as a condition of purchase from Pluralsight.

By activating this benefit, you agree to abide by Pluralsight's terms of use and privacy policy.

I agree, activate benefit