I have a dotnet project that I've been working on adding unit tests to - I took over the project somewhat recently, and the original team that managed it had some pretty lazy requirements about some things. In any case, in JetBrains Rider (and Visual Studio) when I run the unit tests, everything works fine. If I go to the directory in a terminal session and run 'dotnet test' everything works fine. In my dockerfile, adding the command after the build command correctly calls to run the tests, but it fails.
So: the project is structured fairly odd. It's split into 4 sub-projects, with the test framework set into its own sub-project. The docker file:
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/sdk:6.0.402 AS build
WORKDIR /src
COPY ["<company>.Web.csproj", "<company>.Web/"]
COPY ["<company>.Business.csproj", "<company>.Business/"]
COPY ["<company>.Storage.csproj", "<company>.Storage/"]
COPY ["<company>.Middleware.csproj", "<company>.Middleware/"]
COPY ["<company>.WebTest.csproj", "<company>.WebTest/"]
RUN dotnet restore "<company>.Web.csproj"
COPY . .
WORKDIR "/src/<company>.Web"
RUN dotnet build "<company>.Web.csproj" -c Release -o /app/build
FROM build AS test
WORKDIR "/src/<company>.WebTest"
RUN dotnet test
FROM test AS publish
WORKDIR "/src/<company>.Web"
RUN dotnet publish "<company>.Web.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "<company>.Web.dll"]
Sample output from running dotnet test:
[08:53:09] <machine> [<dir>.WebTest]: dotnet test
Determining projects to restore...
All projects are up-to-date for restore.
<company>.Middleware -> <dir>.Middleware\bin\Debug\net6.0\<company>.Middleware.dll
<company>.Storage -> <dir>.Storage\bin\Debug\net6.0\<company>.Storage.dll
<company>.Business -> <dir>.Business\bin\Debug\net6.0\<company>.Business.dll
<company>.WebTest -> <dir>.WebTest\bin\Debug\net6.0\<company>.WebTest.dll
Test run for <dir>.WebTest\bin\Debug\net6.0\<company>.WebTest.dll (.NETCoreApp,Version=v6.0)
Microsoft (R) Test Execution Command Line Tool Version 17.5.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: 58, Skipped: 0, Total: 58, Duration: 2 s - <company>.WebTest.dll (net6.0)
Output from the docker build is the same, up until this happens:
#21 6.356 Starting test execution, please wait...
#21 6.373 A total of 1 test files matched the specified pattern.
#21 7.413 The active test run was aborted. Reason: Test host process crashed : Unhandled exception. Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
The NullReference is to an object that the test execution is Mocking - so it seems like it's not initializing the tests for some reason, as the Mocks are constructed in [OneTimeSetUp] and [SetUp] annotated methods.
Any ideas? Just FYI, by the way, I'm far more experienced with Java - this is my first foray into c#/dotnet, so it's entirely possible I'm just missing something.