我在解决方案中有两个项目。
这里有一个示例集成测试类。
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/zh-cn/aspnet/core/test/integration-tests?view=aspnetcore-3.1
所有测试在Visual Studio中都运行良好。因此,将集成测试项目放入Azure DevOps管道中。
以下是“代理”作业中的任务。
执行期间所有测试均失败,这是其中一项测试的堆栈跟踪。
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管道中执行测试?
我进行了以下更改以解决已发布的问题。
公共类CustomWebApplicationFactory:WebApplicationFactory <WebAPIProject.Startup> {
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.UseContentRoot(AppContext.BaseDirectory);
base.ConfigureWebHost(builder);
}
}
在集成测试中使用CustomWebApplicationFactory。
公共类CustomersControllerTests:IClassFixture <CustomWebApplicationFactory> {公共CustomerControllerTests(CustomWebApplicationFactory factory){_httpClient = factory.CreateClient(); _customerRepository = factory.Services.GetService();
}}