Skip to content

Mastering Integration Testing for .NET Web APIs with WebApplicationFactory and TestContainers

License

Notifications You must be signed in to change notification settings

rufer7/dotnet-webapi-integration-testing-with-testcontainers

Repository files navigation

dotnet-webapi-integration-testing-with-testcontainers

.NET License

About

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.

Getting Started

Prerequisites

Run the Application locally

  1. Clone this GitHub repository

  2. Open the solution (src\ArbitraryApp.sln) in Visual Studio or Visual Studio Code

  3. Create app registration in azure tenant (see here)

  4. Create app role on app registration (see here)

    • Display Name: Admin
    • Value: Arbitrary.Admin
    • Description: Admins can read, write and delete all entities
  5. Update the appsettings.json file in the ArbitraryApp.Server project with the app registration details

    IMPORTANT: store ClientSecret in managed user secrets

  6. Set the ArbitraryApp.Server project as startup project

  7. Launch profile ArbitraryApp.Server

Terminal Commands

dotnet build .\src\ArbitraryApp.sln
dotnet run --project .\src\ArbitraryApp\Server\ArbitraryApp.Server.csproj

Run the Tests and Integration Tests locally

  1. Start Docker Desktop
  2. Open the solution (src\ArbitraryApp.sln) in Visual Studio or Visual Studio Code
  3. Run all tests in the solution

Terminal Commands

dotnet test .\src\ArbitraryApp.sln

Credits

Useful Links

About

Mastering Integration Testing for .NET Web APIs with WebApplicationFactory and TestContainers

Resources

License

Stars

Watchers

Forks

Packages

No packages published