Run your tests


Work through the steps to build an image and run it as a containerized application in Use containers for development.


Testing is an essential part of modern software development. In combination with 3rd party frameworks or services, Docker helps to test applications without mocks or complicated environment configurations fast and reliably.

Add .NET test project

To test our sample application, we create a standalone test project from a template using the .NET CLI. On your local machine, open a terminal, change the directory to the dotnet-docker directory and run the following command:

$ cd /path/to/dotnet-docker
$ dotnet new xunit -n myWebApp.Tests -o tests

Next, we'll update the test project and add the Testcontainers for .NET package that allows us to run tests against Docker resources (PostgreSQL container). Switch to the tests directory and run the following command:

$ dotnet add package Testcontainers --version 2.3.0

Add a test

Open the test project in your favorite IDE and replace the contents of UnitTest1.cs with the following code:

using System.Net;
using DotNet.Testcontainers.Builders;
using DotNet.Testcontainers.Containers;
using DotNet.Testcontainers.Networks;

public sealed class UnitTest1 : IAsyncLifetime, IDisposable
    private const ushort HttpPort = 80;

    private readonly CancellationTokenSource _cts = new(TimeSpan.FromMinutes(1));

    private readonly IDockerNetwork _network;

    private readonly IDockerContainer _dbContainer;

    private readonly IDockerContainer _appContainer;

    public UnitTest1()
        _network = new TestcontainersNetworkBuilder()

        _dbContainer = new TestcontainersBuilder<TestcontainersContainer>()
            .WithVolumeMount("postgres-data", "/var/lib/postgresql/data")

        _appContainer = new TestcontainersBuilder<TestcontainersContainer>()
            .WithPortBinding(HttpPort, true)

    public async Task InitializeAsync()
        await _network.CreateAsync(_cts.Token)

        await _dbContainer.StartAsync(_cts.Token)

        await _appContainer.StartAsync(_cts.Token)

    public Task DisposeAsync()
        return Task.CompletedTask;

    public void Dispose()

    public async Task Test1()
        using var httpClient = new HttpClient();
        httpClient.BaseAddress = new UriBuilder("http", _appContainer.Hostname, _appContainer.GetMappedPublicPort(HttpPort)).Uri;

        var httpResponseMessage = await httpClient.GetAsync(string.Empty)

        var body = await httpResponseMessage.Content.ReadAsStringAsync()

        Assert.Equal(HttpStatusCode.OK, httpResponseMessage.StatusCode);
        Assert.Contains("Welcome", body);

The test class picks up the configurations and lessons we learned in the previous steps. It connects our application and database through a custom Docker network and runs an HTTP request against our application. As you can see, running containerized tests allows us to test applications without mocks or complicated environment configurations. The tests run on any Docker-API compatible environments including CI.

Run the test

Before you run the test, stop any running containers from the previous sections.

To run the test, change directory to the dotnet-docker directory and run the following dotnet test command:

$ dotnet test tests

You should see output like the following:

Starting test execution, please wait...
A total of 1 test files matched the specified pattern.

Passed!  - Failed:     0, Passed:     1, Skipped:     0, Total:     1, Duration: < 1 ms - myWebApp.Tests.dll (net6.0)

Next steps

In the next module, we’ll take a look at how to set up a CI/CD pipeline using GitHub Actions. See:


Help us improve this topic by providing your feedback. Let us know what you think by creating an issue in the Docker Docsopen_in_new GitHub repository. Alternatively, create a PRopen_in_new to suggest updates.