Warm tip: This article is reproduced from serverfault.com, please click

asp.net core-使用XUnit.NET进行集成测试

(asp.net core - Integration Tests using XUnit.NET)

发布于 2020-11-27 13:50:37

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

  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/zh-cn/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管道中执行测试?

Questioner
Bhanu
Viewed
12
Bhanu 2020-12-03 17:25:05

我进行了以下更改以解决已发布的问题。

  1. 将MainSolution.sln文件复制到Release build / publish文件夹中
  2. 创建如下的CustomWebApplicationRepository

公共类CustomWebApplicationFactory:WebApplicationFactory <WebAPIProject.Startup> {

protected override void ConfigureWebHost(IWebHostBuilder builder)
{
    builder.UseContentRoot(AppContext.BaseDirectory);
    base.ConfigureWebHost(builder);
    
}

}

  1. 在集成测试中使用CustomWebApplicationFactory。

    公共类CustomersControllerTests:IClassFixture <CustomWebApplicationFactory> {公共CustomerControllerTests(CustomWebApplicationFactory factory){_httpClient = factory.CreateClient(); _customerRepository = factory.Services.GetService();
    }}