This repository contains the source code of my presentation at .NET Day Switzerland 2024.
- Title: Mastering Integration Testing for .NET Web APIs with WebApplicationFactory and TestContainers
- Teaser: Reduce the fear of making changes to the code to a minimum and at the same time detect bugs early in the development process. This can be achieved by implementing integration tests. With the help of the WebApplicationFactory and the use of the Testcontainers library, .NET Web APIs including common 3rd party dependencies like SQL database, Redis cache and many more can be tested end-to-end and including authorization. In this session you will learn how to do this.
- Visual Studio 2022 or Visual Studio Code
- .NET 8 SDK
- Docker Desktop
- Azure tenant with permissions to create app registrations
-
Clone this GitHub repository
-
Open the solution (
src\ArbitraryApp.sln
) in Visual Studio or Visual Studio Code -
Create app registration in azure tenant (see here)
-
Create app role on app registration (see here)
- Display Name:
Admin
- Value:
Arbitrary.Admin
- Description:
Admins can read, write and delete all entities
- Display Name:
-
Update the
appsettings.json
file in theArbitraryApp.Server
project with the app registration detailsIMPORTANT: store
ClientSecret
in managed user secrets -
Set the
ArbitraryApp.Server
project as startup project -
Launch profile
ArbitraryApp.Server
dotnet build .\src\ArbitraryApp.sln
dotnet run --project .\src\ArbitraryApp\Server\ArbitraryApp.Server.csproj
- Start
Docker Desktop
- Open the solution (
src\ArbitraryApp.sln
) in Visual Studio or Visual Studio Code - Run all tests in the solution
dotnet test .\src\ArbitraryApp.sln
- Integration tests in ASP.NET Core (Microsoft docs)
- Testcontainers
- [HOWTO] Implement integration tests for ASP.NET Core Web API with AntiForgery token validation
- [FollowUp] Using Testcontainers in integration tests for ASP.NET Core Web API
- [NoBrainer] Avoid HTTPS redirection warnings in integration test logs