首页 > 解决方案 > 在 azure devops yaml 管道上使用 dotnet 测试收集代码覆盖率

问题描述

我已经尽我所能来解决这个问题。

我有几个基于 Azure DevOps Services 构建的新 .net core 3.1 或 .net standard 2 项目。我正在尝试为这些项目设置代码覆盖率,但我一无所获。

在本地运行测试时,我获得了成功并创建了代码覆盖率结果。

3/24/20 3:39 [jgust] [portChangesFromAggregateRepo ≡]
>: dotnet test .\Common.UnitTests\ --collect:"Code coverage"
Test run for C:\src\bio\lib\Common\src\Common.UnitTests\bin\Debug\netcoreapp2.2\Common.UnitTests.dll(.NETCoreApp,Version=v2.2)
Microsoft (R) Test Execution Command Line Tool Version 16.3.0
Copyright (c) Microsoft Corporation.  All rights reserved.

Starting test execution, please wait...

A total of 1 test files matched the specified pattern.

Attachments:
  C:\src\bio\lib\Common\src\Common.UnitTests\TestResults\74f826e4-4a46-46dd-9fe5-7fd17b8cee1c\2020-03-24.15_40_28.coverage
Test Run Successful.
Total tests: 794
     Passed: 794
 Total time: 19.3390 Seconds

但是,在 Azure DevOps 上运行此项目时,生成报告找不到收集器。

A total of 1 test files matched the specified pattern.
Data collection : Could not find data collector 'Code coverage'
Results File: d:\a\_temp\VssAdministrator_fv-az130_2020-03-24_20_53_07.trx

Test Run Successful.
Total tests: 794
     Passed: 794

管道任务如下:

在此处输入图像描述

是的,我在这里使用 Windows 平台

在此处输入图像描述

关于为这些项目设置代码覆盖率集合,我缺少什么。

据我所知,我要把书里的一切都写得精疲力尽

另外,我正在使用描述的包版本:

<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />

标签: unit-testing.net-corecode-coverageazure-pipelines

解决方案


您应该在 中指定测试项目的 csproj 文件的路径Path to project(s),并确保测试项目引用Microsoft.NET.Test.SDK版本 15.8.0 或更高版本

虽然指定.dll文件的路径将使dotnet test任务下的命令能够运行测试,但它不允许收集覆盖率数据。该操作需要.csproj文件路径。

尝试使用 $(System.DefaultWorkingDirectory)\**\*${{ parameters.slnName }}*[T|t]est*.csproj

注意:这个规范应该使否定过时。


推荐阅读