首页 > 解决方案 > 使用 XUnit.NET 进行集成测试 - 无法使用应用程序根找到解决方案根

问题描述

我在解决方案中有两个项目。

  1. 使用 .NET Core 3.1 的 Web API
  2. 使用 XUnit.NET 和 .NET Core 3.1 进行集成测试,该项目参考了 Web API 项目

这里有一个示例集成测试类。

public class CustomersControllerTests : IClassFixture<WebApplicationFactory<WebAPIProject.Startup>> 
    {
            public CustomersControllerTests(WebApplicationFactory<WebAPIProject.Startup> factory)
            {
                _httpClient = factory.CreateClient();
                _customerRepository = factory.Services.GetService<ICustomerRepository>();            
            }
    }

我使用以下文章开发了集成测试。

https://docs.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-3.1

所有测试在 Visual Studio 中运行良好。因此,集成测试项目被放入 Azure DevOps 管道。

以下是代理作业中的任务。

  1. 下载管道工件任务(从构建管道下载构建工件)
  2. Visual Studio 测试平台安装程序任务
  3. Visual Studio 测试任务

所有测试在执行期间都失败了,这里是其中一个测试的堆栈跟踪。

System.InvalidOperationException : Solution root could not be located using application root D:\a\r1\a\_ReleaseManagement.API.IntegrationTests-CI\drop\ReleaseManagement.IntegrationTests\Release\.
[xUnit.net 00:00:06.59]       Stack Trace:
at Microsoft.AspNetCore.TestHost.WebHostBuilderExtensions.UseSolutionRelativeContentRoot(IWebHostBuilder builder, String solutionRelativePath, String applicationBasePath, String solutionName)
at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory`1.SetContentRoot(IWebHostBuilder builder)
at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory`1.<EnsureServer>b__20_0(IWebHostBuilder webHostBuilder)
at Microsoft.Extensions.Hosting.GenericHostWebHostBuilderExtensions.ConfigureWebHost(IHostBuilder builder, Action`1 configure)
at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory`1.EnsureServer()
at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory`1.CreateDefaultClient(DelegatingHandler[] handlers)
at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory`1.CreateDefaultClient(Uri baseAddress, DelegatingHandler[] handlers)
at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory`1.CreateClient(WebApplicationFactoryClientOptions options)
at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory`1.CreateClient()
/home/vsts/work/1/s/src/UnitTests/ReleaseManagement.IntegrationTests/Api/CustomersControllerTests.cs(29,0): at ReleaseManagement.IntegrationTests.Api.CustomersControllerTests..ctor(WebApplicationFactory`1 factory)
2020-11-27T09:54:43.4381123Z ##[error][xUnit.net 00:00:06.59]     ReleaseManagement.IntegrationTests.Api.CustomersControllerTests.CreateCustomer_Success [FAIL]

构建管道定义 -

steps:
 - task: UseDotNet@2
   displayName: "Use .NET Core SDK $(netCoreVersion)"
   inputs:
    version: $(netCoreVersion)


 - task: DotNetCoreCLI@2
   displayName: 'Restore project dependencies'
   inputs:
    command: 'restore'
    projects: '**/ABC.IntegrationTests/ABC.IntegrationTests.csproj'


 - task: DotNetCoreCLI@2
   displayName: 'Build API Integration Tests - $(buildConfiguration)'
   inputs:
    command: 'build'
    arguments: '--configuration $(buildConfiguration) --no-restore --output $(Build.ArtifactStagingDirectory)\ABC.IntegrationTests\Release'
    projects: '**/ABC.IntegrationTests/ABC.IntegrationTests.csproj'


 - task: PublishBuildArtifacts@1
   displayName: 'Publish build artifact: drop'
   condition: succeeded()

发布管道定义 -

steps:
- task: DotNetCoreCLI@2
  displayName: 'dotnet test'
  inputs:
    command: test
    arguments: '_ABC.API.IntegrationTests-CI\drop\ABC.IntegrationTests\Release\ABC.IntegrationTests.dll'

有关如何解决此问题以便可以在 azure devops 管道中执行测试的任何帮助?

标签: asp.net-coreazure-devopsasp.net-core-mvcintegration-testingxunit.net

解决方案


基本上“WebApplicationFactory”需要“WebApplicationFactoryContentRootAttribute”来找到根,所以在这里阅读更多。

试试这个修复 或者这个修复


推荐阅读