---
title: Run .NET tests in a container
linkTitle: Run your tests
weight: 30
keywords: .NET, test
description: Learn how to run your .NET tests in a container.
aliases:
- /language/dotnet/run-tests/
- /guides/language/dotnet/run-tests/
---
## Prerequisites
Complete all the previous sections of this guide, starting with [Containerize a .NET application](containerize.md).
## Overview
Testing is an essential part of modern software development. Testing can mean a
lot of things to different development teams. There are unit tests, integration
tests and end-to-end testing. In this guide you take a look at running your unit
tests in Docker when developing and when building.
## Run tests when developing locally
The sample application already has an xUnit test inside the `tests` directory. When developing locally, you can use Compose to run your tests.
Run the following command in the `docker-dotnet-sample` directory to run the tests inside a container.
```console
$ docker compose run --build --rm server dotnet test /source/tests
```
You should see output that contains the following.
```console
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 - /source/tests/bin/Debug/net8.0/tests.dll (net8.0)
```
To learn more about the command, see [docker compose run](/reference/cli/docker/compose/run/).
## Run tests when building
To run your tests when building, you need to update your Dockerfile. You can create a new test stage that runs the tests, or run the tests in the existing build stage. For this guide, update the Dockerfile to run the tests in the build stage.
The following is the updated Dockerfile.
```dockerfile {hl_lines="9"}
# syntax=docker/dockerfile:1
FROM --platform=$BUILDPLATFORM mcr.microsoft.com/dotnet/sdk:8.0-alpine AS build
ARG TARGETARCH
COPY . /source
WORKDIR /source/src
RUN --mount=type=cache,id=nuget,target=/root/.nuget/packages \
dotnet publish -a ${TARGETARCH/amd64/x64} --use-current-runtime --self-contained false -o /app