ASP.NET Core
No matter if your tests require databases, message brokers, your own services or even a running instance of your entire application, leveraging Testcontainers in your tests means you can set up the infrastructure fast and reliably. You can also run tests in parallel against multiple lightweight or a single shared heavyweight instance, depending on the use case. xUnit.net's shared context and other test frameworks offers several methods to access resources efficiently among different tests and scopes.
Utilizing WebApplicationFactory
ASP.NET Core offers the WebApplicationFactory<TEntryPoint>
class for bootstrapping and creating a TestServer dedicated to integration tests. There are several methods to integrate Testcontainers and dependent app services into ASP.NET Core integration tests.
One approach extends or overrides the application's configuration section to set up the connection string before creating the TestServer. This approach requires manually starting the dependent service in advance.
Set Redis connection string | |
---|---|
1 2 3 4 5 6 7 |
|
A slightly more complex approach involves implementing the IConfigurationSource
interface and the ConfigurationProvider
class. This approach adds a new configuration source and automatically starts the dependent service before creating the TestServer.
Add Redis configuration source | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
|
WeatherForecast Example
The following example adds tests to an ASP.NET Core Blazor application. The tests cover the web front-end including the REST API of a weather forecast application. Testcontainers builds and ships our app in a Docker image, runs it in a Docker container, orchestrates the necessary resources, and executes the tests against it. This setup includes a Microsoft SQL Server to persist data and covers a common use case among many productive .NET applications. You find the entire example in the testcontainers-dotnet repository.
To use Testcontainers' pre-configured Microsoft SQL Server module, add the Testcontainers.MsSql NuGet dependency to your test project:
1 |
|
Note
When running on Macs with Apple Silicon, make sure that you are using Docker Desktop for macOS version 4.16 or newer, and that the Virtualization Framework is enabled, with Rosetta 2 installed to run x86-64 based containers.
The WeatherForecastContainer
class configures in the default constructor all dependencies to start the container that hosts our application.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
First, the class configures the Docker network. The application and database use this private network to communicate with each other. Followed by that, the class sets up the configuration of the Microsoft SQL Server container. Once all the dependencies are in place, the example configures the weather forecast application conveniently with environment variables. xUnit.net calls IAsyncLifetime.InitializeAsync
immediately after the class has been created. The test project uses this mechanism to create and start all dependencies before any test run:
1 2 3 4 5 6 7 8 9 10 11 |
|
xUnit.net passes the WeatherForecastContainer
class fixture instance to the test class WeatherForecastTest
. Each test collection Api
and Web
creates a new instance of WeatherForecastContainer
and spins up an isolated environment. Even multiple test sessions do not interfere with each other. To run tests against a single shared heavyweight instance (collection fixture), add all dependencies to a collection definition. This works not only for containers, but also for any other Docker resource like images, networks or volumes.
As soon as the container is up and the application is running, each test sends an HTTP request to the weather forecast application and validates the REST or web front-end response. To visualize the web front-end, Selenium takes a screenshot right before and after the test.
Testcontainers not only works great for testing an application or service out-of-process, as shown in the example above, but it is probably even better for testing it in-process as shown in this example. When used in conjunction with the ASP.NET WebApplicationFactory<TEntryPoint>
class, Testcontainers spins up the dependent container together with the application, resulting in much faster tests. Depending on the application or service configuration, adding Testcontainers to bootstrap dependent services significantly improves the development experience. There is no longer a need to ensure that dependent services are running and wired up correctly on the development machine or CI environment.