This is a sample repo for setting up API testing in .NET.
It includes:
- Demonstrating mocking services
- Getting application logs into test output by using xUnit's
ITestOutputHelper
- Using Testcontainers to run tests against a real SQL Server
The project contains 2 different APIs and 2 different test projects
NetWebApi
andTest.NetWebApi
using the regular Microsoft provided dependency injection containerNetWebApi.LightInject
andTest.NetWebApi.LightInject
using the LightInject dependency injection container
- Run the SQL Server compose file:
docker-compose up -d
- Build and run API through Visual Studio, Rider or command line.
-
Make sure you have Docker running.
-
Run the tests in either
Test.NetWebApi
orTest.NetWebApi.LightInject
- Docs: Integration tests in ASP.NET Core
- David Fowler's gist
- What is Testcontainers, and why should you use it?
The EF Core in-memory provider is NOT recommended for use in integration testing:
EF Core also comes with an in-memory provider. Although this provider was originally designed to support internal testing of EF Core itself, some developers use it as a database fake when testing EF Core applications. Doing so is highly discouraged.