Effective Unit Testing

2 days
UTEST
2 days

Upcoming Sessions

Date:

Format:

Price:

Location:

Book now

Date:

Format:

Price:

Location:

Book now

Date:

Format:

Price:

Location:

Book now

Date:

Format:

Price:

Book now

Need a private training for your team?  Request a private training

Not ready to book yet?   Request an offer

Unit Testing in .NET

Every developer aspires to write code that is not only clean but also functional. However, achieving code that is easily maintainable and consistently operational poses a significant challenge. We will explore the role of unit testing in verifying the correctness of our code and ensuring its continued functionality.

  • What every developer wants: Quality code that works; and keeps on working
  • Finding bugs: not just in code. - Or how NASA lost a 125.000.000$ Mars orbiter
  • What is unit testing? And what is a good unit test?
  • Understanding the difference between a unit test and an integration test
  • Test Driven Development - Should you be doing it?
  • Role-playing: Marge, Homer and Bart, or happy vs negative path
  • The Triple-A of unit testing - and what has cooking to do with this?
  • Given-When-Then
  • The difference between a Fake, a Stub and a Mock

Unit testing with MSTest and xUnit

When it comes to Unit Testing, you will need to choose frameworks. There are several options available, so which one do you need? .NET comes with a built-in Microsoft framework MSTest, but you also have others, such as the popular xUnit.

  • MSTest - Built-in into Visual Studio
  • Building and running Unit Tests with MSTest
  • Using the Test Explorer Window
  • Using Test Settings
  • Live unit testing with Visual Studio
  • Unit Testing best practices with xUnit
  • Facts and Theories
  • Running Unit tests in parallel, or not
  • Test Naming Conventions
  • Measuring and improving Code Coverage
  • Comparing to NUnit and TUnit
  • LAB: Implementing a library with testing

Dependency Injection for Testing

When writing code, it's crucial to minimize unnecessary dependencies on other objects. Dependencies can increase maintenance overhead, reduce testability, and limit flexibility. Dependency injection allows you to dynamically determine which implementations to inject into dependencies at runtime, providing greater flexibility and decoupling within your codebase.

  • What is tight coupling and how to prevent it?
  • Using Inversion of Control (IoC) Containers
  • Constructor, Property and Method injection
  • Microsoft.Extensions.DependencyInjection as an example

Testable Legacy Code

If you aim to make your code testable, you must design it with testing in mind. In this chapter, you'll discover design patterns and techniques for introducing seams into existing code, allowing dependencies to be isolated without disrupting your testing process.

  • Testing dependencies - and the art of writing testable code
  • The Extract and Override pattern
  • Faking time with TimeProvider
  • Replacing configuration in tests
  • LAB: Use Extract and Override to test legacy code

Isolation Frameworks: NSubstitute

Building stubs and mocks can be laborious without an Isolation Framework. NSubstitute is one such framework that simplifies the creation of fake and stub objects required for robust testing.

  • Understanding Isolation Frameworks
  • Building Stubs and Mocks with NSubstitute
  • Checking arguments and return values
  • Using argument matchers
  • Configuring return values and exceptions
  • Verifying calls were made
  • Using the Object Mother Pattern
  • LAB: Using NSubstitute

Integration Testing

Integration testing verifies that multiple parts of an application work together correctly—especially where your code interacts with external systems or infrastructure. Unlike unit tests (which isolate a single class), integration tests validate real collaboration between components.

  • When Should You Write Integration Tests?
  • Using WebApplicationFactory for Integration Testing
  • Faking data access
  • Testing with Real Data Access
  • Using TestContainers
  • Testing your Controllers and Views
  • Using AngleSharp for View Verification
  • Testing ApiControllers
  • LAB: Using WebApplicationFactory

End-to-End Testing with Playwright

End-to-end (E2E) testing verifies that a system works from the user's point of view, by exercising the entire application stack from the UI through the backend to the database and back. It answers one key question: Can a real user complete a real scenario successfully?

  • End-to-end testing with Microsoft Playwright
  • Building your First Playwright Test
  • Recording a Playwright Test
  • Tracing Interactions, Screen Shots, Videos
  • LAB: Using Playwright

Architecture Testing

.NET architecture testing is the practice of automatically verifying that your .NET application's architectural rules and design decisions are respected—not just that the code works, but that it is structured correctly over time.

  • When is Architecture Testing Useful?
  • Authoring Architecture Tests using NetArchTest
  • Checking Layering Rules
  • Validating Dependency Direction
  • Forbidden Dependencies
  • Naming Conventions
  • LAB: Adding Architecture Tests

Writing solid, maintainable code that works is the goal of every developer. This course guides you into the art of software testing, where you learn to build testable code and various techniques to ensure high quality code. By the end of this course, you will gain a solid and practical understanding of software testing and its real-life applications. U2U is renowned for its hands-on approach to training, and each chapter is accompanied by a practical lab.

This course is intended for experienced programmers who are very familiar with C# and have a working experience with .NET 8 to 10. All examples and labs utilize the latest LTS version of .NET and Visual Studio.

Contact Us
  • Address:
    U2U nv/sa
    Z.1. Researchpark 110
    1731 Zellik (Brussels)
    BELGIUM
  • Phone: +32 2 466 00 16
  • Email: info@u2u.be
  • Monday - Friday: 9:00 - 17:00
    Saturday - Sunday: Closed
Say Hi
© 2026 U2U All rights reserved.