Unit Testing in .NET

93 lessons · 10 sections · updated 19 Nov 2026
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
  1. 01 What Is a Unit Test? Fast, Deterministic Tests for Pure Business Rules in xUnit Updated 19 Nov 2026
  2. 02 Unit vs Integration vs End-to-End Tests: The Boundary Matters, Not the Framework Updated 19 Nov 2026
  3. 03 The Test Pyramid in .NET: Tagging and Filtering Tests by Scope with xUnit Traits Updated 19 Nov 2026
  4. 04 MSTest vs NUnit vs xUnit: The Same Unit Tests Written in Three .NET Frameworks Updated 19 Nov 2026
  5. 05 xUnit v2 vs v3: ValueTask Lifetimes, TestContext.Current and Runtime Skips Updated 19 Nov 2026
  6. 06 Microsoft.Testing.Platform vs VSTest: Running One xUnit v3 Project with Three Runners Updated 19 Nov 2026
  7. 07 Organizing a .NET Test Solution by Scope: Unit and Integration Folders and Conventions Updated 19 Nov 2026

Section 02: xUnit and Assertions

08–17
  1. 08 Creating an xUnit Test Project with Directory.Build.props and Central Package Management Updated 19 Nov 2026
  2. 09 Arrange-Act-Assert: Writing Your First Readable Unit Test in xUnit Updated 19 Nov 2026
  3. 10 The xUnit Test Lifecycle: Constructor, IDisposable and IAsyncLifetime Updated 19 Nov 2026
  4. 11 Naming Unit Tests by Intent: The Method_Scenario_ExpectedResult Pattern Updated 19 Nov 2026
  5. 12 Core xUnit Assertions: Equal, Same, Null, Single, All, Collection and Equivalent Updated 19 Nov 2026
  6. 13 String Assertions in xUnit: StartsWith, Contains, Matches and Explicit StringComparison Updated 19 Nov 2026
  7. 14 Range and Type Assertions in xUnit: InRange, IsType and IsAssignableFrom Updated 19 Nov 2026
  8. 15 Testing Exceptions in xUnit: Throws, ThrowsAny and Awaiting ThrowsAsync Updated 19 Nov 2026
  9. 16 Readable Assertions with FluentAssertions 7: Should(), Collections, Exceptions and Licensing Updated 19 Nov 2026
  10. 17 Snapshot Testing in .NET: From a Hand-Made Approval Test to Verify with Scrubbing Updated 19 Nov 2026

Section 03: Theories and Data-Driven Tests

18–27
  1. 18 Fact vs Theory in xUnit: When One Example Is Enough and When You Need Many Updated 19 Nov 2026
  2. 19 InlineData in xUnit: Keeping Small Test Cases Next to the Test Updated 19 Nov 2026
  3. 20 MemberData in xUnit: Feeding Theories from Properties, Methods and Shared Classes Updated 19 Nov 2026
  4. 21 ClassData and TheoryData in xUnit: Scaling Test Data with Type Safety Updated 19 Nov 2026
  5. 22 Trait and Skip in xUnit: Categorizing Tests and Controlling What Runs Updated 19 Nov 2026
  6. 23 IClassFixture in xUnit: Sharing Expensive Setup Across Tests in One Class Updated 19 Nov 2026
  7. 24 Collection Fixtures in xUnit: Sharing One Context Across Multiple Test Classes Updated 19 Nov 2026
  8. 25 Parallel Test Execution in xUnit: Why Tests Need Real Isolation Updated 19 Nov 2026
  9. 26 Async Tests in xUnit: Awaiting Results, Exceptions and Cancellation Correctly Updated 19 Nov 2026
  10. 27 Property-Based Testing in .NET: From a Hand-Written Loop to FsCheck and CsCheck Updated 19 Nov 2026

Section 04: Test Double Types

28–33
  1. 28 Test Doubles in .NET: Controlling a Class's Collaborators in Unit Tests Updated 19 Nov 2026
  2. 29 Dummy and Stub Test Doubles in C#: Filling Parameters and Feeding Input Updated 19 Nov 2026
  3. 30 Fake and Spy Test Doubles in C#: Working State and Recorded Calls Updated 19 Nov 2026
  4. 31 Writing a Mock by Hand in C#: Verifying Outgoing Interactions in xUnit Updated 19 Nov 2026
  5. 32 State, Output and Behaviour Verification in Unit Tests: Which to Prefer Updated 19 Nov 2026
  6. 33 Choosing the Lightest Test Double: A Decision Table for Dummy, Stub, Fake, Spy and Mock Updated 19 Nov 2026

Section 05: Testable Design and Moq

34–46
  1. 34 Hidden Dependencies: Why Some .NET Classes Cannot Be Unit Tested Updated 19 Nov 2026
  2. 35 Abstractions and Dependency Injection as Test Seams in C# Updated 19 Nov 2026
  3. 36 Deterministic Time in .NET Tests with TimeProvider and FakeTimeProvider Updated 19 Nov 2026
  4. 37 Test Data Builders and Object Mothers for Readable C# Unit Tests Updated 19 Nov 2026
  5. 38 Generating Test Data with AutoFixture and Bogus Without Hiding the Scenario Updated 19 Nov 2026
  6. 39 Moq Basics: Creating and Configuring Test Doubles with Setup, It.IsAny and It.Is Updated 19 Nov 2026
  7. 40 Verifying Calls with Moq: Verify, Times and VerifyNoOtherCalls Updated 19 Nov 2026
  8. 41 Modeling Failures with Moq: ThrowsAsync, Callback and SetupSequence Updated 19 Nov 2026
  9. 42 Mocking Async Methods in Moq: Cancellation Tokens and TaskCompletionSource Updated 19 Nov 2026
  10. 43 Testing ILogger in .NET: Verify Log Level, EventId and Structured Properties Updated 19 Nov 2026
  11. 44 Faking HttpClient in Unit Tests with a Stub HttpMessageHandler Updated 19 Nov 2026
  12. 45 NSubstitute vs Moq: The Same Unit Test Written with Both Libraries Updated 19 Nov 2026
  13. 46 Over-Mocking: Why Verifying Every Interaction Makes Unit Tests Fragile Updated 19 Nov 2026

Section 06: Qualities of Good Unit Tests

47–54
  1. 47 The Four Qualities of a Good Unit Test: Protection, Resistance, Speed, Maintainability Updated 19 Nov 2026
  2. 48 Balancing Unit Test Qualities: Trade-offs Between Trivial, Brittle and E2E Tests Updated 19 Nov 2026
  3. 49 Spotting Brittle and Logic-Heavy Unit Tests and Replacing Them with Explicit Examples Updated 19 Nov 2026
  4. 50 Removing Order Dependence from Unit Tests: Fresh State, Fake Clocks and Unique Names Updated 19 Nov 2026
  5. 51 Diagnosing Flaky Tests in .NET: Time, Culture, Randomness and Fixed Waits Updated 19 Nov 2026
  6. 52 Why Code Coverage Can Mislead: Same Coverage, Very Different Protection Updated 19 Nov 2026
  7. 53 Mutation Testing in .NET with Stryker.NET: Testing Your Tests Updated 19 Nov 2026
  8. 54 Getting Legacy Code Under Test with Characterization Tests and Safe Refactoring Updated 19 Nov 2026

Section 07: Testing ASP.NET Core MVC and APIs

55–64
  1. 55 Structuring ASP.NET Core MVC Tests by Responsibility: Keep Controllers Thin Updated 19 Nov 2026
  2. 56 Testing ASP.NET Core MVC Controller Actions Scenario by Scenario Updated 19 Nov 2026
  3. 57 Verifying the ActionResult Contract: ViewResult, RedirectToAction and NotFound Updated 19 Nov 2026
  4. 58 Testing ModelState and Validation Branches in ASP.NET Core MVC Controllers Updated 19 Nov 2026
  5. 59 Unit Testing FluentValidation Validators Directly with TestValidate Updated 19 Nov 2026
  6. 60 Testing the CRUD Contract of an ASP.NET Core API Controller Updated 19 Nov 2026
  7. 61 Unit Testing ASP.NET Core Minimal API Handlers with TypedResults Updated 19 Nov 2026
  8. 62 Testing TempData and ClaimsPrincipal in ASP.NET Core Controller Unit Tests Updated 19 Nov 2026
  9. 63 Testing Business Rules Below the Web Layer in ASP.NET Core Applications Updated 19 Nov 2026
  10. 64 The TDD Cycle in Practice: Red, Green, Refactor with xUnit Updated 19 Nov 2026

Section 08: Layered Architecture and DDD Testing

65–74
  1. 65 Testing in Layered Architectures: Architecture Changes Boundaries, Not Principles Updated 19 Nov 2026
  2. 66 Organizing Tests by Layer and Scope in a Layered .NET Solution Updated 19 Nov 2026
  3. 67 Domain Tests Protect Business Decisions: Testing an Aggregate in Plain C# Updated 19 Nov 2026
  4. 68 Application Layer Tests: Verifying Use Cases with Ports, Fakes and a Fake Clock Updated 19 Nov 2026
  5. 69 Testing CQRS Command Handlers and Pipeline Behaviors Without MediatR Updated 19 Nov 2026
  6. 70 Subcutaneous Tests: Testing a Full Use Case Just Below the UI Updated 19 Nov 2026
  7. 71 Value Objects, Entities and Aggregates: Three Building Blocks, Three Test Focuses Updated 19 Nov 2026
  8. 72 Testing Aggregate Invariants: Rules That Must Hold After Every Operation Updated 19 Nov 2026
  9. 73 Testing Domain Events and Domain Services with Behavioral Tests Updated 19 Nov 2026
  10. 74 Architecture Tests with NetArchTest: Making Dependency Rules Executable Updated 19 Nov 2026

Section 09: Integration Testing

75–84
  1. 75 Integration Tests Cross a Real Boundary: Testing the Full ASP.NET Core Pipeline Updated 19 Nov 2026
  2. 76 Unit or Integration? Drawing the Boundary Between Test Types in .NET Updated 19 Nov 2026
  3. 77 WebApplicationFactory: Running Your ASP.NET Core API In Memory for Integration Tests Updated 19 Nov 2026
  4. 78 Replacing Services for Tests: ConfigureTestServices and a Test Authentication Handler Updated 19 Nov 2026
  5. 79 Choosing an EF Core Test Database Honestly: InMemory vs SQLite vs the Real Thing Updated 19 Nov 2026
  6. 80 Testing Real DbContext Behaviour: Query Translation, Transactions and Change Tracking Updated 19 Nov 2026
  7. 81 Simulating External APIs with WireMock.Net in ASP.NET Core Integration Tests Updated 19 Nov 2026
  8. 82 Testing .NET Background Services at Two Levels with FakeTimeProvider Updated 19 Nov 2026
  9. 83 Organizing Integration Tests: Separate Projects and Shared Fixtures in xUnit Updated 19 Nov 2026
  10. 84 End-to-End Tests with Playwright for .NET: Real Browser, Real Kestrel Host Updated 19 Nov 2026

Section 10: Testcontainers and CI

85–93
  1. 85 Testcontainers in .NET: Testing Against a Real, Disposable PostgreSQL Database Updated 19 Nov 2026
  2. 86 Start Only the Infrastructure a Test Needs: Container Fixtures with Testcontainers Updated 19 Nov 2026
  3. 87 Async Fixture Lifecycle in xUnit: IAsyncLifetime and ICollectionFixture for Containers Updated 19 Nov 2026
  4. 88 Migrations as Part of the Test: Applying EF Core Migrations in a Testcontainers Fixture Updated 19 Nov 2026
  5. 89 Resetting Database State Between Tests with Respawn and Testcontainers Updated 19 Nov 2026
  6. 90 Testing RabbitMQ and Kafka with Testcontainers: Real Brokers, Real Protocols Updated 19 Nov 2026
  7. 91 System Testing a Distributed App with .NET Aspire: AppHost and Aspire.Hosting.Testing Updated 19 Nov 2026
  8. 92 Running Testcontainers Tests in GitHub Actions: A Staged CI Pipeline for .NET Updated 19 Nov 2026
  9. 93 Code Coverage Reports in .NET with coverlet and ReportGenerator, Locally and in CI Updated 19 Nov 2026