
Using Docker Test Containers in Functional Testing in ASP.NET WEB API
webapi
17 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 ispre-configured in a desired state
. -
If the
resources (database, message broker, etc) are shared across multiple users or CI pipelines
, then thetest 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
.
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.
- Install
Testcontainers.PostgreSql
in your Functional Test Project Setup
Database Fixture
as shown belowCode Sample - Database Fixture for Test Containers in Function Test in ASP.NET
DatabaseFixture
inherits fromIAsyncLifetime
initialises the database container usingInitializeAsync()
and cleans up the container after the end of test usingDisposeAsync()
We are also creating a
FunctionalTestCollection
to be used asICollectionFixture<DatabaseFixture>
.Update
CustomWebApplicationFactory
to replaceWeatherForecastDbContext
connection string withTest Container Connection String
as shown belowCode 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 thedbcontext
inside test methodadd the data for test
andassert the same data
returned from endpoint in test as shown below.Code Sample - Testing against real database in Function Test in ASP.NET
Advantages
The advantages of Test Containers are as follows,
On-demand isolated infrastructure
provisioningConsistent experience
on both local and CI environmentsTesting with real dependencies
helps to catch bugs earlierLight
WeightNo 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.