Search code examples
c#dockernunitdotnet-test

No test is available when running tests in Docker


I created NUnit project through Visual Studio and trying to run it in Docker.

FROM mcr.microsoft.com/dotnet/sdk:7.0 AS base
WORKDIR /app

FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
WORKDIR /src
COPY ["TestProject1.csproj", "TestProject1/"]
RUN dotnet restore "TestProject1/TestProject1.csproj"
COPY . .
WORKDIR "/src/TestProject1"
RUN dotnet build "TestProject1.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "TestProject1.csproj" -c Release -o /app/publish /p:UseAppHost=false

FROM base as final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "test", "TestProject1.dll", "/TestAdapterPath:."]

I built an image with docker build -t tests . And finally, I ran it, but it did not execute tests. Without the last parameter it also says Additionally, path to test adapters can be specified using /TestAdapterPath command. Example /TestAdapterPath:<pathToCustomAdapters>.. I am not sure which parameter I should include to run dotnet test or am I using the wrong sdk image?

PM> docker run tests

Microsoft (R) Test Execution Command Line Tool Version 17.7.1 (x64) Copyright (c) Microsoft Corporation. All rights reserved.

Starting test execution, please wait... A total of 1 test files matched the specified pattern. No test is available in /app/TestProject1.dll. Make sure that test discoverer & executors > are registered and platform & framework version settings are appropriate and try again.

namespace TestProject1
{
    public class Tests
    {
        [SetUp]
        public void Setup()
        {
        }

        [Test]
        public void Test1()
        {
            Assert.Pass();
        }
    }
}

Solution

  • The Docker file was autogenerated by VS and I had to trim the file to make it work - dotnet test builds and restores the project, so build and publish steps were unnecessary. But I still don't know what's wrong with the Docker in my question.

    FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
    WORKDIR /app
    COPY . .
    RUN dotnet restore
    ENTRYPOINT ["dotnet", "test"]
    

    PM> docker run tests

    Determining projects to restore...

    All projects are up-to-date for restore.

    TestProject1 -> /app/bin/Debug/net7.0/TestProject1.dll Test run for /app/bin/Debug/net7.0/TestProject1.dll (.NETCoreApp,Version=v7.0) Microsoft (R) Test Execution Command Line Tool Version 17.7.1 (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: 1, Skipped: 0, Total: 1, >Duration: 17 ms - TestProject1.dll (net7.0)