【发布时间】:2021-03-10 07:18:17
【问题描述】:
我在解决方案中有两个项目。
- 使用 .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 管道中执行测试的任何帮助?
【问题讨论】:
-
您能否更新您的问题并添加构建和集成管道定义?
-
更新了构建和发布定义,谢谢。
-
嗨@Bhanu。这张票有更新吗?如果答案能给你一些帮助,请随时告诉我。只是提醒this。
标签: asp.net-core azure-devops asp.net-core-mvc integration-testing xunit.net