Search code examples
c#asp.netdockerintegration-testing.net-6.0

.Net 5 to .Net 6 migration breaking integration tests in docker, works ok in local


While trying to migrate from .Net 5 to .Net 6 I am getting below error when I do docker build and run.

It works perfectly fine in .Net 5 VS + in docker. It works perfectly fine in .Net 6 VS but does not work in docker.

Things I have done:

  1. Updated framework to .Net 6
  2. Updated docker to use .Net 6
  3. Update packges to 6.*

Images I am using:

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base

FROM mcr.microsoft.com/dotnet/sdk:6.0-alpine AS tests

Error I get is as below:

 [xUnit.net 00:00:01.87]     XXXApi.IntegrationTests.Tests.AssetTests.GivenNoData_WhenGetAllAssets_ReturnEmptyList [FAIL]
      Failed XXXApi.IntegrationTests.Tests.AssetTests.GivenNoData_WhenGetAllAssets_ReturnEmptyList [1 ms]
      Error Message:
       System.IO.DirectoryNotFoundException : /src/XXXApi/
      Stack Trace:
         at Microsoft.Extensions.FileProviders.PhysicalFileProvider..ctor(String root, ExclusionFilters filters)
       at Microsoft.Extensions.FileProviders.PhysicalFileProvider..ctor(String root)
       at Microsoft.Extensions.Hosting.HostBuilder.CreateHostingEnvironment()
       at Microsoft.Extensions.Hosting.HostBuilder.Build()
       at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory`1.CreateHost(IHostBuilder builder)
       at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory`1.ConfigureHostBuilder(IHostBuilder hostBuilder)
       at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory`1.EnsureServer()
       at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory`1.get_Services()
       at XXXApi.IntegrationTests.Tests.BaseTests..ctor(CustomWebApplicationFactory`1 factory) in /src/XXXApi.IntegrationTests/Tests/BaseTests.cs:line 24
       at XXXApi.IntegrationTests.Tests.AssetTests..ctor(CustomWebApplicationFactory`1 factory) in /src/XXXApi.IntegrationTests/Tests/AssetTests.cs:line 12
    Results File: /src/TestResults/testresults.trx

Solution

  • Integration testing changed a bit with WebApplicationFactory in .NET 6 due to the "lack" of Startup. Follow the new instruction here: https://learn.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-6.0#basic-tests-with-the-default-webapplicationfactory

    Futhermore try copying your missing project output to the test layer COPY --from=build /out ./XXXApi/

    Rregards