如何使用“ .Net FrameWork 4.7”项目在Azure DevOps管道上创建覆盖率报告?

问题描述

我目前正在尝试使用Azure DevOps上的管道创建Coverage覆盖率报告。但是,由于我的项目是“ .Net FrameWork 4.7”项目,因此无法使用“ .Net Core”项目之类的“ DotNetCoreCLI @ 2”任务来创建覆盖率报告。

有我的管道代码

trigger:
- master

pool:
  vmImage: 'windows-latest'

variables:
  buildplatform: 'Any cpu'
  buildConfiguration: 'release'
  Tests.Filter: '**\UnitTestProject1.dll'
  Tests.Filter.Criteria: 'TestCategory!=Medium&TestCategory!=Large'
  Tests.Filter.SettingsFile:

steps:
- task: NuGetToolInstaller@1

- task: NuGetCommand@2
  inputs:
    restoreSolution: '**/*.sln'

- task: DotNetCoreCLI@2
  displayName: Test   
  inputs:
    command: 'test'
    projects: |
            $(Tests.Filter)
            !**\obj\**
    publishTestResults: true
    arguments: -c $(BuildConfiguration) --collect:"XPlat Code Coverage"
    
- task: DotNetCoreCLI@2
  inputs:
    command: custom
    custom: tool
    arguments: install --tool-path . dotnet-reportgenerator-globaltool
  displayName: Install ReportGenerator tool

- script: reportgenerator -reports:$(Agent.TempDirectory)/**/*.coverage -targetdir:$(Build.sourcesDirectory)/coverlet/reports -reporttypes:"Cobertura"
  displayName: Create reports
- task: PublishCodeCoverageResults@1
  displayName: 'Publish code coverage'
  inputs:
    codeCoveragetool: Cobertura
    summaryFileLocation: '$(Build.sourcesDirectory)\TestResults\Coverage\*.xml'

所有测试成功后,我会收到:

Starting: Create reports
==============================================================================
Task         : Command line
Description  : Run a command line script using Bash on Linux and macOS and cmd.exe on Windows
Version      : 2.164.2
Author       : Microsoft Corporation
Help         : https://docs.microsoft.com/azure/devops/pipelines/tasks/utility/command-line
==============================================================================
Generating script.
Script contents:
reportgenerator -reports:D:\a\_temp/**/*.coverage -targetdir:D:\a\1\s/coverlet/reports -reporttypes:"Cobertura"
========================== Starting Command Output ===========================
"C:\windows\system32\cmd.exe" /D /E:ON /V:OFF /S /C "CALL "D:\a\_temp\e4754795-32ae-47f2-bd62-d1c8b925eb84.cmd""
2020-09-28T14:56:09: Arguments
2020-09-28T14:56:09:  -reports:D:\a\_temp/**/*.coverage
2020-09-28T14:56:09:  -targetdir:D:\a\1\s/coverlet/reports
2020-09-28T14:56:09:  -reporttypes:Cobertura
2020-09-28T14:56:09: The report file pattern 'D:\a\_temp/**/*.coverage' is invalid. No matching files found.
2020-09-28T14:56:09: No report files specified.
##[error]Cmd.exe exited with code '1'.    
Finishing: Create reports

我还尝试使用VSTest @ 2并激活“ CodeCoverageEnable”,但是代码覆盖率与Azure DevOps不兼容,需要下载才能查看覆盖率报告。

我有VSTest @ 2的管道代码

trigger:
- master

pool:
  vmImage: 'windows-latest'

variables:
  buildplatform: 'Any cpu'
  buildConfiguration: 'release'
  Tests.Filter: '**\UnitTestProject1.dll'
  Tests.Filter.Criteria: 'TestCategory!=Medium&TestCategory!=Large'
  Tests.Filter.SettingsFile:

steps:
- task: NuGetToolInstaller@1

- task: NuGetCommand@2
  inputs:
    restoreSolution: '**/*.sln'


- task: VSTest@2
  displayName: 'Running UnitTests'
  inputs:
    testSelector: 'testAssemblies'
    testAssemblyVer2: |
            $(Tests.Filter)
            !**\obj\**
    searchFolder: '$(System.DefaultWorkingDirectory)'
    testFiltercriteria: '$(Tests.Filter.Criteria)'
    runSettingsFile: '$(Tests.Filter.SettingsFile)'
    runInParallel: true
    testRunTitle: 'Running UnitTests for $(Tests.Filter)'
    platform: '$(buildplateform)'
    configuration: -c '$(buildConfiguration)'
    codeCoverageEnabled: true

是否可以为我的“ .Net FrameWork 4.7”项目创建与Azure DevOps(Cobertura或JaCoCo)兼容的覆盖率报告,而无需更改项目.Net FrameWork版本或项目类型?

解决方法

经过一些研究,我们设法为 .Net FrameWork 项目生成了 Cobertura 代码覆盖率报告。以下管道包含您应该集成到管道的两个关键点:

-启动管道部分中的行: disable.coverage.autogenerate:'true'

-代码覆盖率报告

部分
trigger:
- master
#----------------------
# Initiate pipeline 
#----------------------
stages:
- stage: Dev
  pool:
    vmImage: 'windows-latest'
  variables:
    solution: '**/*.sln'
    buildPlatform: 'Any CPU'
    buildConfiguration: 'Release'
    disable.coverage.autogenerate: 'true' # because we overwrite the coverage report generation

  jobs:
  - job: Build
    steps:
    - task: NuGetToolInstaller@1
#-------------------------------
#Intalation report generator
#-------------------------------
    - task: DotNetCoreCLI@2
      inputs:
        command: custom
        custom: tool
        arguments: install --tool-path . dotnet-reportgenerator-globaltool
      displayName: Install ReportGenerator tool
    - task: DotNetCoreCLI@2
      inputs:
        command: custom
        custom: tool
        arguments: install --tool-path . coverlet.console
      displayName: Install Coverlet.Console tool
#-------------------------------
#Execute build
#-------------------------------
    - task: NuGetCommand@2
      inputs:
        restoreSolution: '$(solution)'
    - task: VSBuild@1
      inputs:
        solution: '$(solution)'
        platform: '$(buildPlatform)'
        configuration: '$(buildConfiguration)'
#-------------------------------
#Execute Unit tests
#-------------------------------

    - task: VSTest@2
      inputs:
        platform: '$(buildPlatform)'
        configuration: '$(buildConfiguration)'
#-------------------------------
#Code coverage report
#-------------------------------
    - task: PowerShell@2
      inputs:
        targetType: inline
        script: |
          Get-ChildItem *\bin -Include *Tests.dll -Recurse |
          Foreach-Object { 
            .\coverlet.exe $_.FullName --target "dotnet" --targetargs "vstest $($_.FullName) --logger:trx" --format "cobertura"
            $NewName = $_.Name + ".coverage.cobertura.xml"
            Rename-Item coverage.cobertura.xml $NewName
          }
      displayName: Generate Coverlet coverage report for test libraries

    - script: |
        mkdir .\reports
        .\reportgenerator.exe -reports:*coverage.cobertura.xml -targetdir:reports -reporttypes:"HTMLInline;HTMLChart"
        dir .\reports
      displayName: Create coverage reports in html format to be displayed in AzureDevOps

    - script: |
        mkdir .\reports
        .\reportgenerator.exe -reports:*coverage.cobertura.xml -targetdir:reports -reporttypes:"cobertura"
        dir .\reports
      displayName: Create coverage reports in xml format to be used for summary
#-------------------------------
#Publish coverage report
#-------------------------------

    - task: PublishCodeCoverageResults@1
      displayName: 'Publish code coverage to AzureDevOps'
      inputs:
        codeCoverageTool: Cobertura
        summaryFileLocation: '$(system.defaultworkingdirectory)\reports\Cobertura.xml'
        reportDirectory: '$(system.defaultworkingdirectory)\reports'

    - task: PublishBuildArtifacts@1
      displayName: 'Publish artifacts'
      inputs:
        PathtoPublish: '$(system.defaultworkingdirectory)\bin'
        ArtifactName: 'PublishBuildArtifacts'

结果如下: Cobertura code coverage

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...