Search code examples
.netdocker.net-corevisual-studio-2017

How to run .NET unit tests in a docker container


I have a .NET Core application containing MSTest unit tests. What would the command be to execute all tests using this Dockerfile?

FROM microsoft/dotnet:1.1-runtime
ARG source
COPY . .
ENTRYPOINT ["dotnet", "test", "Unittests.csproj"]

Folder structure is:

/Dockerfile
/Unittests.csproj
/tests/*.cs

Solution

  • Use a base image with .NET Core SDK installed. For example:

    microsoft/dotnet
    microsoft/dotnet:1.1.2-sdk
    

    You can't run dotnet test in a Runtime-based image without SDK. This is why an SDK-based image is required. Here is a fully-workable Dockerfile example:

    FROM microsoft/dotnet
    
    WORKDIR /app
    COPY . .
    
    RUN dotnet restore
    
    # run tests on docker build
    RUN dotnet test
    
    # run tests on docker run
    ENTRYPOINT ["dotnet", "test"]
    

    RUN commands are executed during a docker image build process.

    ENTRYPOINT command is executed when a docker container starts.