asp.net-core - 使用 XUnit.NET 进行集成测试 - 无法使用应用程序根找到解决方案根
问题描述
我在解决方案中有两个项目。
- 使用 .NET Core 3.1 的 Web API
- 使用 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 管道。
以下是代理作业中的任务。
- 下载管道工件任务(从构建管道下载构建工件)
- Visual Studio 测试平台安装程序任务
- 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 管道中执行测试的任何帮助?
解决方案
推荐阅读
- javascript - javascript通过lodash获取具有相同键的数组对象的平均值
- css - 如何将 MDC Web 的 Filled TextField 的高度设置为更小,但仍允许浮动标签正确浮动?
- javascript - WebExtensions:onConnect 的意义何在?onMessage 似乎是多余的
- angular - 如果发生错误异常,我如何计算执行重试运算符的次数
- generics - 将 trait 中方法的返回类型与实现该 trait 的类型绑定
- r - 如何从 ggplot2 中的汇总统计信息制作箱线图?
- javascript - 在 MS Edge 中使用 beforeunload 事件处理程序
- solr - 分面搜索 - 使用带有 AND / OR 连接的多个方面
- python - 如何在 Python 中使用 Beautifulsoup 获取嵌套标签的文本?
- java - 使用 onOptionsItemSelected 和 Kotlin 启动新活动