.Net 5 到 .Net 6 的迁移破坏了 docker 中的集成测试,在本地工作正常

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

在尝试从 .Net 5 迁移到 .Net 6 时,我在 docker build 和 运行.

时遇到错误

它在 .Net 5 VS + docker 中工作得很好。 它在 .Net 6 VS 中工作得很好,但在 docker.

中不起作用

我做过的事情:

  1. 将框架更新为 .Net 6
  2. 已更新 docker 以使用 .Net 6
  3. 更新包到 6.*

我正在使用的图像:

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS 基础

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

我得到的错误如下:

 [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

由于“缺少”Startup,.NET 6 中的 WebApplicationFactory 的集成测试发生了一些变化。 按照此处的新说明进行操作:https://docs.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-6.0#basic-tests-with-the-default-webapplicationfactory

此外,尝试将丢失的项目输出复制到测试层 COPY --from=build /out ./XXXApi/

问候