5.0 KiB
Running Tests with Docker
You can use Docker to run your unit tests in an isolated environment using the .NET SDK Docker image. This is useful if your development and production environments don't match, like, for example, Windows and Linux, respectively. There are multiple ways to run unit tests in containers, which are demonstrated in this document.
Building in an SDK container is a similar scenario and relies on similar patterns. Building and testing multiple projects with Docker sample offers additional test patterns that you may want to adopt.
This document uses the tests that are part of complexapp. The instructions assume that you are in the complexapp directory.
The following examples demonstrate using dotnet test
in a .NET SDK container. It builds tests and dependent projects from source and then runs them. You have to re-launch the container every time you want to test source code changes.
Alternatively, you can use dotnet watch test
. This command reruns tests within a running container with every local code change.
Requirements
The instructions assume that you have cloned the repository locally.
You may need to enable shared drives (Windows) or file sharing (macOS) first.
Container scenarios that use volume mounting can produce conflicts between the bin
and obj
directories in local and container environments. To avoid that, you need to use a different set of obj
and bin
folders for your container environment. The easiest way to do that is to copy a custom Directory.Build.props into the directory you are using (like the complexapp
directory in the following example), either via copying from this repo or downloading with the following command:
curl -o Directory.Build.props https://raw.githubusercontent.com/dotnet/dotnet-docker/main/samples/Directory.Build.props
Note: You may need to remove
bin
andobj
directories if you run these instructions on Windows in both Windows and Linux container modes.
Running tests
You can run dotnet test
within a .NET SDK container using the following pattern, with docker run
and volume mounting. This initial example is demonstrated on Windows with PowerShell (in Linux container mode). Instructions for all OSes follow.
> docker run --rm -v ${pwd}:/app -w /app/tests mcr.microsoft.com/dotnet/sdk:8.0 dotnet test
Determining projects to restore...
Restored /app/libbar/libbar.csproj (in 251 ms).
Restored /app/libfoo/libfoo.csproj (in 250 ms).
Restored /app/tests/tests.csproj (in 4.58 sec).
libbar -> /app/libbar/bin/Debug/net8.0/libbar.dll
libfoo -> /app/libfoo/bin/Debug/net8.0/libfoo.dll
tests -> /app/tests/bin/Debug/net8.0/tests.dll
Test run for /app/tests/bin/Debug/net8.0/tests.dll (.NETCoreApp,Version=v8.0)
Microsoft (R) Test Execution Command Line Tool Version 17.8.0 (x64)
Copyright (c) Microsoft Corporation. All rights reserved.
Starting test execution, please wait...
A total of 1 test files matched the specified pattern.
Passed! - Failed: 0, Passed: 3, Skipped: 0, Total: 3, Duration: 3 ms - tests.dll (net8.0)
In this example, the tests (and any other required code) are volume mounted into the container, and dotnet test
is run from the tests
directory (-w
sets the working directory). Test results can be read from the console or from logs, which can be written to disk with the --logger:trx
flag.
When the --logger:trx
flag is used, you should find a .trx
file in the TestResults folder. You can open this file in Visual Studio to see the results of the test run, as you can see in the following image. You can open it in Visual Studio (File -> Open -> File) or double-click on the TRX file (if you have Visual Studio installed). There are other TRX file viewers available as well, which you can search for.
The following instructions demonstrate this scenario in various configurations with logging enabled.
Linux or macOS
docker run --rm -v $(pwd):/app -w /app/tests mcr.microsoft.com/dotnet/sdk:8.0 dotnet test --logger:trx
Windows using Linux containers
This example uses PowerShell.
docker run --rm -v ${pwd}:/app -w /app/tests mcr.microsoft.com/dotnet/sdk:8.0 dotnet test --logger:trx
Windows using Windows containers
This example uses PowerShell.
docker run --rm -v ${pwd}:C:\app -w C:\app\tests mcr.microsoft.com/dotnet/sdk:8.0-nanoserver-ltsc2022 dotnet test --logger:trx