Using Docker Test Containers in Functional Testing in ASP.NET WEB API
WebAPI
19 Articles
In this article, let's learn about testing with real database using Docker Test Containers in Web API in ASP.NET Core.
Note: If you have not done so already, I recommend you read the article on Functional testing your ASP.NET WEB API.
Table of Contents
- Introduction
- Why Docker Test Containers ?
- Steps to do Functional Test using Test Containers
- Advantages
- Summary
Introduction
We are all aware that the softwares we build works rarely in isolation. Our apps will talk to many external systems like database, message brokers, web browsers, cloud services or just any other external systems. And validating the integration with these external systems is very important.
Testcontainers is a library that provides easy and lightweight APIs for bootstrapping local development and test dependencies with real services wrapped in Docker containers. Using Testcontainers, you can write tests that depend on the same services you use in production without mocks or in-memory services.
Why Docker Test Containers ?
Cloud-native infrastructure and microservices have taken control away from developers and made it painful to work locally. While you own only your Service and its datastore, you have several downstream dependencies that you need for local development and integration testing. This brings the following challenges:
- Before running tests, you must ensure that the infrastructure is up and running and is pre-configured in a desired state.
- If the resources (database, message broker, etc) are shared across multiple users or CI pipelines, then the test results are non-deterministic because of the possibility of data corruption and configuration drift.
A possible workaround could be using in-memory variations of the required services. But this is almost similar like using mocks. In-memory services might not have all the features of the production / real service amd they have their own problems.
Testcontainers solves this by using Docker to spin up real services for functional testing and helps executing reliable and repeatable tests by talking to those real services and providing a programmatic API for your test code.
Steps to do Functional Test using Test Containers
Let's learn how to do functional test using Test Containers in ASP.NET Web API. We will be using the same project we used in the previous article and try using test containers for database instead of in-memory database.
- Install Testcontainers.PostgreSql in your Functional Test Project
Setup SharedFixture as shown below
Code Sample - Shared Fixture for Test Containers in Function Test in ASP.NET
In the above code, we are initializing the database container and getting the connection string from the container and exposing it to be used in ConfigureServices in CustomWebApiFactory. We are then applying migrations to make sure schema is applied and created. We can also initialize database with initial seed data here. We are also disposing the container after all tests are run.
Update CustomWebApiFactory to replace WeatherForecastDbContext connection string with Test Container Connection String as shown below
Code Sample - Customer Web Application Factory with Test Containers in Function Test in ASP.NET
Finally we just need to run the tests and see the magic happening. We can see the database container spinning up and running and our tests are running against the real database within test container.
Code Sample - Testing against real database in Function Test in ASP.NET
Thats it. We are done with setup. Now the tests will run in the following flow.
- Start Test Method.
- TestContainers Setup.
- Create WebApplicationFactory.
- Run Program.cs.
- Service Registration.
- Overriding Configuration.
- Build Web Application.
- Create HTTP Client.
- Test Code Execution.
Advantages
The advantages of Test Containers are as follows,
- On-demand isolated infrastructure provisioning
- Consistent experience on both local and CI environments
- Testing with real dependencies helps to catch bugs earlier
- Light Weight
- No Mocks or In-Memory setup which behaves differently than real ones
- Easy to setup and automatic clean up
Summary
In this article, we learnt about testing with real dependencies using Docker Test Containers in functional testing in ASP.NET Web API. We learnt how to do that with ASP.NET functional test and understood its advantages. Hope you enjoyed reading it.