I kept running into the same issue when writing integration tests for ASP.NET Core APIs — there’s no clean way to assert how many SQL queries an endpoint executes.
Most of the time it ends up being:
- custom DbCommandInterceptor
- wiring it into WebApplicationFactory
- manually counting queries
So I wrapped that into a small library.
Example:
await using var guard = factory.TrackQueries<Program, AppDbContext>();
var client = guard.CreateClient();
await client.GetAsync("/api/orders");
guard.AssertCount(exact: 1);
That’s it — no manual interceptor or log parsing.
What it does:
- Counts SELECT/INSERT/UPDATE/DELETE queries triggered by HTTP requests
- Starts counting from CreateClient() (so startup/seeding queries are ignored)
- Works with any EF Core provider
- Designed for WebApplicationFactory-based integration tests
GitHub:
https://github.com/KiwiDevelopment/KiwiQuery
NuGet:
dotnet add package KiwiQuery.EFCore
MIT, very small codebase. Would love feedback — especially if you’re already solving this in a different way.