Unit Testing in .NET
93 lessons · 10 sections · updated 19 Nov 2026
Continue at
Start at 01
Lessons are ordered. Work through them in order; each number is the lesson's position in the course.
Sections (10)
Section 01: Unit Testing Fundamentals
01–07- 01 What Is a Unit Test? Fast, Deterministic Tests for Pure Business Rules in xUnit
- 02 Unit vs Integration vs End-to-End Tests: The Boundary Matters, Not the Framework
- 03 The Test Pyramid in .NET: Tagging and Filtering Tests by Scope with xUnit Traits
- 04 MSTest vs NUnit vs xUnit: The Same Unit Tests Written in Three .NET Frameworks
- 05 xUnit v2 vs v3: ValueTask Lifetimes, TestContext.Current and Runtime Skips
- 06 Microsoft.Testing.Platform vs VSTest: Running One xUnit v3 Project with Three Runners
- 07 Organizing a .NET Test Solution by Scope: Unit and Integration Folders and Conventions
Section 02: xUnit and Assertions
08–17- 08 Creating an xUnit Test Project with Directory.Build.props and Central Package Management
- 09 Arrange-Act-Assert: Writing Your First Readable Unit Test in xUnit
- 10 The xUnit Test Lifecycle: Constructor, IDisposable and IAsyncLifetime
- 11 Naming Unit Tests by Intent: The Method_Scenario_ExpectedResult Pattern
- 12 Core xUnit Assertions: Equal, Same, Null, Single, All, Collection and Equivalent
- 13 String Assertions in xUnit: StartsWith, Contains, Matches and Explicit StringComparison
- 14 Range and Type Assertions in xUnit: InRange, IsType and IsAssignableFrom
- 15 Testing Exceptions in xUnit: Throws, ThrowsAny and Awaiting ThrowsAsync
- 16 Readable Assertions with FluentAssertions 7: Should(), Collections, Exceptions and Licensing
- 17 Snapshot Testing in .NET: From a Hand-Made Approval Test to Verify with Scrubbing
Section 03: Theories and Data-Driven Tests
18–27- 18 Fact vs Theory in xUnit: When One Example Is Enough and When You Need Many
- 19 InlineData in xUnit: Keeping Small Test Cases Next to the Test
- 20 MemberData in xUnit: Feeding Theories from Properties, Methods and Shared Classes
- 21 ClassData and TheoryData in xUnit: Scaling Test Data with Type Safety
- 22 Trait and Skip in xUnit: Categorizing Tests and Controlling What Runs
- 23 IClassFixture in xUnit: Sharing Expensive Setup Across Tests in One Class
- 24 Collection Fixtures in xUnit: Sharing One Context Across Multiple Test Classes
- 25 Parallel Test Execution in xUnit: Why Tests Need Real Isolation
- 26 Async Tests in xUnit: Awaiting Results, Exceptions and Cancellation Correctly
- 27 Property-Based Testing in .NET: From a Hand-Written Loop to FsCheck and CsCheck
Section 04: Test Double Types
28–33- 28 Test Doubles in .NET: Controlling a Class's Collaborators in Unit Tests
- 29 Dummy and Stub Test Doubles in C#: Filling Parameters and Feeding Input
- 30 Fake and Spy Test Doubles in C#: Working State and Recorded Calls
- 31 Writing a Mock by Hand in C#: Verifying Outgoing Interactions in xUnit
- 32 State, Output and Behaviour Verification in Unit Tests: Which to Prefer
- 33 Choosing the Lightest Test Double: A Decision Table for Dummy, Stub, Fake, Spy and Mock
Section 05: Testable Design and Moq
34–46- 34 Hidden Dependencies: Why Some .NET Classes Cannot Be Unit Tested
- 35 Abstractions and Dependency Injection as Test Seams in C#
- 36 Deterministic Time in .NET Tests with TimeProvider and FakeTimeProvider
- 37 Test Data Builders and Object Mothers for Readable C# Unit Tests
- 38 Generating Test Data with AutoFixture and Bogus Without Hiding the Scenario
- 39 Moq Basics: Creating and Configuring Test Doubles with Setup, It.IsAny and It.Is
- 40 Verifying Calls with Moq: Verify, Times and VerifyNoOtherCalls
- 41 Modeling Failures with Moq: ThrowsAsync, Callback and SetupSequence
- 42 Mocking Async Methods in Moq: Cancellation Tokens and TaskCompletionSource
- 43 Testing ILogger in .NET: Verify Log Level, EventId and Structured Properties
- 44 Faking HttpClient in Unit Tests with a Stub HttpMessageHandler
- 45 NSubstitute vs Moq: The Same Unit Test Written with Both Libraries
- 46 Over-Mocking: Why Verifying Every Interaction Makes Unit Tests Fragile
Section 06: Qualities of Good Unit Tests
47–54- 47 The Four Qualities of a Good Unit Test: Protection, Resistance, Speed, Maintainability
- 48 Balancing Unit Test Qualities: Trade-offs Between Trivial, Brittle and E2E Tests
- 49 Spotting Brittle and Logic-Heavy Unit Tests and Replacing Them with Explicit Examples
- 50 Removing Order Dependence from Unit Tests: Fresh State, Fake Clocks and Unique Names
- 51 Diagnosing Flaky Tests in .NET: Time, Culture, Randomness and Fixed Waits
- 52 Why Code Coverage Can Mislead: Same Coverage, Very Different Protection
- 53 Mutation Testing in .NET with Stryker.NET: Testing Your Tests
- 54 Getting Legacy Code Under Test with Characterization Tests and Safe Refactoring
Section 07: Testing ASP.NET Core MVC and APIs
55–64- 55 Structuring ASP.NET Core MVC Tests by Responsibility: Keep Controllers Thin
- 56 Testing ASP.NET Core MVC Controller Actions Scenario by Scenario
- 57 Verifying the ActionResult Contract: ViewResult, RedirectToAction and NotFound
- 58 Testing ModelState and Validation Branches in ASP.NET Core MVC Controllers
- 59 Unit Testing FluentValidation Validators Directly with TestValidate
- 60 Testing the CRUD Contract of an ASP.NET Core API Controller
- 61 Unit Testing ASP.NET Core Minimal API Handlers with TypedResults
- 62 Testing TempData and ClaimsPrincipal in ASP.NET Core Controller Unit Tests
- 63 Testing Business Rules Below the Web Layer in ASP.NET Core Applications
- 64 The TDD Cycle in Practice: Red, Green, Refactor with xUnit
Section 08: Layered Architecture and DDD Testing
65–74- 65 Testing in Layered Architectures: Architecture Changes Boundaries, Not Principles
- 66 Organizing Tests by Layer and Scope in a Layered .NET Solution
- 67 Domain Tests Protect Business Decisions: Testing an Aggregate in Plain C#
- 68 Application Layer Tests: Verifying Use Cases with Ports, Fakes and a Fake Clock
- 69 Testing CQRS Command Handlers and Pipeline Behaviors Without MediatR
- 70 Subcutaneous Tests: Testing a Full Use Case Just Below the UI
- 71 Value Objects, Entities and Aggregates: Three Building Blocks, Three Test Focuses
- 72 Testing Aggregate Invariants: Rules That Must Hold After Every Operation
- 73 Testing Domain Events and Domain Services with Behavioral Tests
- 74 Architecture Tests with NetArchTest: Making Dependency Rules Executable
Section 09: Integration Testing
75–84- 75 Integration Tests Cross a Real Boundary: Testing the Full ASP.NET Core Pipeline
- 76 Unit or Integration? Drawing the Boundary Between Test Types in .NET
- 77 WebApplicationFactory: Running Your ASP.NET Core API In Memory for Integration Tests
- 78 Replacing Services for Tests: ConfigureTestServices and a Test Authentication Handler
- 79 Choosing an EF Core Test Database Honestly: InMemory vs SQLite vs the Real Thing
- 80 Testing Real DbContext Behaviour: Query Translation, Transactions and Change Tracking
- 81 Simulating External APIs with WireMock.Net in ASP.NET Core Integration Tests
- 82 Testing .NET Background Services at Two Levels with FakeTimeProvider
- 83 Organizing Integration Tests: Separate Projects and Shared Fixtures in xUnit
- 84 End-to-End Tests with Playwright for .NET: Real Browser, Real Kestrel Host
Section 10: Testcontainers and CI
85–93- 85 Testcontainers in .NET: Testing Against a Real, Disposable PostgreSQL Database
- 86 Start Only the Infrastructure a Test Needs: Container Fixtures with Testcontainers
- 87 Async Fixture Lifecycle in xUnit: IAsyncLifetime and ICollectionFixture for Containers
- 88 Migrations as Part of the Test: Applying EF Core Migrations in a Testcontainers Fixture
- 89 Resetting Database State Between Tests with Respawn and Testcontainers
- 90 Testing RabbitMQ and Kafka with Testcontainers: Real Brokers, Real Protocols
- 91 System Testing a Distributed App with .NET Aspire: AppHost and Aspire.Hosting.Testing
- 92 Running Testcontainers Tests in GitHub Actions: A Staged CI Pipeline for .NET
- 93 Code Coverage Reports in .NET with coverlet and ReportGenerator, Locally and in CI