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
- Why Docker Test Containers ?
- Steps to do Functional Test using Test Containers
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 runningand is
pre-configured in a desired state.
resources (database, message broker, etc) are shared across multiple users or CI pipelines, then the
test results are non-deterministicbecause of the possibility of data corruption and configuration drift.
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
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
try using test containers for database instead of in-memory database.
Note: Before we begin, I would like to highlight that we are going to use single database container for all tests in our project. And the reason for this is when we have 100 or 1000 tests then spinning 100 or 1000 containers will make your test agent to go down consuming all memory and CPU.
Testcontainers.PostgreSqlin your Functional Test Project
Database Fixtureas shown below
Code Sample - Database Fixture for Test Containers in Function Test in ASP.NET
IAsyncLifetimeinitialises the database container using
InitializeAsync()and cleans up the container after the end of test using
We are also creating a
FunctionalTestCollectionto be used as
WeatherForecastDbContextconnection string with
Test Container Connection Stringas shown below
Code Sample - Customer Web APplication Factory with Test Containers in Function Test in ASP.NET
Finally mark the test class with
[Collection("Database Collection")]and inject the
dbcontextinside test method
add the data for testand
assert the same datareturned from endpoint in test as shown below.
Code Sample - Testing against real database in Function Test in ASP.NET
The advantages of Test Containers are as follows,
On-demand isolated infrastructureprovisioning
Consistent experienceon both local and CI environments
Testing with real dependencieshelps to catch bugs earlier
No Mocks or In-Memory setupwhich behaves differently than real ones
- Easy to setup and
automatic clean up
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.