English
Topic 6. Testing the data layer: xUnit v3, Testcontainers, Respawn and Shouldly
Goal: build the test infrastructure that every later topic relies on — a real PostgreSQL in a throw-away container, the real migrations, a clean database before each test — and prove the data layer with 19 tests: unique rules, keyset pages, unread counters, read positions, transactions, safe searches and file storage.
Where we are
The data layer is complete; now it gets its safety net (Fig. 6.1). From here on, every topic of modules 2–4 ends with tests.
Fig. 6.1. The test projects of the data layer
- Starting point: topic 5 — the data layer is complete; Docker Desktop is running.
- At the end of the topic
dotnet test --solution Chatex.slnxruns 19 tests against a fresh PostgreSQL in about 15 seconds, and they all pass.
Steps
- Test projects — why a real database · xUnit v3 on the Microsoft Testing Platform · shared settings for test projects
- A database for the tests — Testcontainers · migrations · Respawn · the base class
DatabaseTest - The first tests —
DbTestBasewith seeding helpers · Arrange, Act, Assert · Shouldly · running and filtering tests - Testing the repositories — paging · read positions · the chat list · transactions · searches · file storage
- Checkpoint — what must work, self-check questions and exercises
Materials
- Course slides, part 5, Testing and CI/CD (slides "Three levels of backend tests", "A real database in a container", "A test that reads like a rule")
Tests/Chatex.Testing/,Tests/Chatex.Data.Tests/,Tests/Directory.Build.propsDocs/Testing.md– how the tests are organised and run