Testing in practice
Integration tests, unit tests and test strategies that keep large APIs safe to change — for humans and AI agents.
- One job only — a base class for API integration testsMost integration test base classes start as "host the API" and end as a 2,000-line junk drawer of test data helpers. A WebApplicationFactory-based base class that starts the API once, hands out clients and does nothing else.
- Test patterns for GET, POST and DELETEMost API test suites test the happy path of GET and call it a day. A repeatable checklist per HTTP verb, from the 401 to the "deleted means gone" check, plus the setup and cleanup rules that keep those tests independent of each other and of the system state.
- Test-first for APIs — start with the 401Test-first for APIs isn't about red-green ceremony. It forces you to be the first consumer of your own endpoint, to think about security before data, and to build the endpoints your tests need. A step-by-step walk-through with a minimal API.
- From Postman to C# and backA bug report arrives as a Postman request, a consuming team asks how to call your API, and your tests are written in object initializers nobody else can use. How to keep test data in the format that travels, turn every test call into a curl command, and turn real requests into tests.
- One test suite, many environmentsThe production pipeline that runs its tests against staging, the tenant-specific tests that are commented out before every commit, and three developers deleting each other's test data on QA. How to run the same integration tests against several environments without copying them.
- Testing as a specific user — without pasting tokens into codeA bug only happens for one kind of user, so someone pastes a bearer token into a test and swaps the header on the shared client. How to run API tests as specific users, in-process and against deployed environments, without leaking tokens or breaking unrelated tests.
- Unit tests that explain themselves — structure and namingA test called CalculateSomething with five asserts tells you one thing when it fails, and you have to run it five times to learn the rest. Naming conventions, test class structure and assertion habits that make a failing unit test explain itself.
- Snapshot testing for graph logicWhen deleting one entity cascades through a graph of relations, classic assertions miss exactly the bugs that hurt most — the relation that wasn't deleted and the neighbour that was. Why snapshotting the whole graph, plus a generated visual diff, works better.
- AI-friendly tests — failures built for humans and agentsA bare Assert.IsTrue failure was always a bad failure message. Now that AI agents read test output and try to fix what broke, it's an expensive one. How to design failures that say what broke, why, where and how to fix it, and why one diagnostic model should render for both humans and machines.
- Real dependencies in tests with TestcontainersThe in-memory database that happily accepts what PostgreSQL would reject, and the shared dev database that three test runs fight over. How Testcontainers gives every test run its own real database and cache, wired into a WebApplicationFactory, and what it costs.
- Integration tests for things that happen in the backgroundThe API answers 202 Accepted and the real work happens later in a background worker. Task.Delay makes those tests slow and flaky, test-only events pollute production code. Polling through the public API and listening to the telemetry the app already emits are the two patterns that work.