简体   繁体   English

jenkins 中的 VStest 代码覆盖率报告

[英]VStest code coverage report in jenkins

I am setting CI for .Net project using Jenkins.我正在使用 Jenkins 为 .Net 项目设置 CI。

I used MSTest Plugin and VStestrunner plugin to run test.我使用 MSTest 插件和 VStestrunner 插件来运行测试。 Now I have .trx file and .Coverage file I am facing problem in displaying code coverage report现在我有 .trx 文件和 .Coverage 文件我在显示代码覆盖率报告时遇到问题

Please help me is you know any plugin to do this.请帮助我你知道任何插件来做到这一点。

I have struggled this for a long time, finally I found we can use "CodeCoverage.exe" "ReportGenarator.exe" and "Cobertura plugin" to show perfect coverage report.我为此苦苦挣扎了很久,终于发现我们可以使用“CodeCoverage.exe”、“ReportGenarator.exe”和“Cobertura 插件”来显示完美的覆盖率报告。

"ReportGenarator.exe" can be get from https://github.com/danielpalme/ReportGenerator/releases “ReportGenerator.exe”可以从https://github.com/danielpalme/ReportGenerator/releases获取

  • first use "CodeCoverage.exe" translate .coverage file to .xml file首先使用“CodeCoverage.exe”将 .coverage 文件转换为 .xml 文件
    "C:\Program Files (x86)\Microsoft Visual Studio 14.0\Team Tools\Dynamic Code Coverage Tools\CodeCoverage.exe" analyze -output:./TestResults/coverage.xml ./TestResults/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.coverage"
  • second use ReportGenarator.exe translate vstest xml format to Cobertura xml format第二次使用ReportGenerator.exe 将vstest xml 格式转换为Cobertura xml 格式
    "ReportGenerator_4.4.7\net47\ReportGenerator.exe" -reports:./TestResults/coverage.xml -targetdir:./TestResults -reporttypes:cobertura
  • finally install cobertura plugin use it to collect xml file, here give a pipeline useage example最后安装cobertura插件使用它来收集xml文件,这里给出一个管道使用示例
    post {
        always {
            cobertura coberturaReportFile: './TestResults/Cobertura.xml'
        }
    }
  • the result just like this结果就像这样cobertura.xml 报告

To display the coverage report you need to convert it in XML format and use MSTest Plugin to publish the report.要显示覆盖率报告,您需要将其转换为 XML 格式并使用 MSTest 插件发布报告。 MSTest Plugin recommends ( https://wiki.jenkins-ci.org/display/JENKINS/MSTest+Plugin ) to use third party application to convert in XML format and powershell (you will need to install pugin for it) to run it. MSTest Plugin 建议( https://wiki.jenkins-ci.org/display/JENKINS/MSTest+Plugin )使用第三方应用程序转换为 XML 格式和 powershell(您需要为其安装 pugin)来运行它。

However you can convert it with PowerShell only.但是,您只能使用 PowerShell 进行转换。 There is example of script:有脚本示例:

$coverageFile = $(get-ChildItem -Path .\TestResults -Recurse -Include *coverage)[0]
$xmlCoverageFile = ".\TestResults\vstest.coveragexml"

Add-Type -path "C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\PrivateAssemblies\Microsoft.VisualStudio.Coverage.Analysis.dll"

[string[]] $executablePaths = @($coverageFile)
[string[]] $symbolPaths = @()

$info = [Microsoft.VisualStudio.Coverage.Analysis.CoverageInfo]::CreateFromFile($coverageFile, $executablePaths, $symbolPaths);
$data = $info.BuildDataSet()

$data.WriteXml($xmlCoverageFile)

You maybe will need to fix the path to Microsoft.VisualStudio.Coverage.Analysis.dll according to your VS version.您可能需要根据您的 VS 版本修复Microsoft.VisualStudio.Coverage.Analysis.dll的路径。

Following ghking's answer, cobertura complains that the xml is not found although it's on the disk.在 ghking 的回答之后,cobertura 抱怨虽然 xml 位于磁盘上,但未找到它。 I have to remove './' from the path, so that cobertura is able to find the file.我必须从路径中删除“./”,以便 cobertura 能够找到该文件。

   post {
       always {
           cobertura coberturaReportFile: 'TestResults/Cobertura.xml'
       }
   }

The complete script to do this is:执行此操作的完整脚本是:

<#
.SYNOPSIS
    Script to convert code coverage report into xml format that can then be published by external tools.

.DESCRIPTION
    Covering code coverage statistics as part of quality improvement initiatives.    
#>
Param(
    [String] $InputCoveragePath =@("..\GeneratedFiles\Docs\Reports"),
    [String] $OutputCoverageFileExtension =@(".coveragexml"),
    [String] $CoverageAnalysisAssembly =@("Microsoft.VisualStudio.Coverage.Analysis.dll"),
    [String[]] $ExecutablePaths =@(""),
    [String[]] $SymbolPaths =@("")
)
    $ScriptLocation = Split-Path $script:MyInvocation.MyCommand.Path -Parent
    Write-Host $ScriptLocation

$RunAs32Bit = {
    Param(
        [String] $InputCoveragePath =@("..\GeneratedFiles\Docs\Reports"),
        [String] $OutputCoverageFileExtension =@(".coveragexml"),
        [String] $CoverageAnalysisAssembly =@("Microsoft.VisualStudio.Coverage.Analysis.dll"),
        [String[]] $ExecutablePaths =@(""),
        [String[]] $SymbolPaths =@(""),
        [String] $ScriptLocation =@(".")
    )
    Write-Host "[CoverageConverter][Begin]: Coverage conversion started..."

    Write-Host "[CoverageConverter][InputCoveragePath]: $InputCoveragePath"
    Write-Host "[CoverageConverter][OutputCoverageFileExtension]: $OutputCoverageFileExtension"
    Write-Host "[CoverageConverter][CoverageAnalysisAssembly]: $CoverageAnalysisAssembly"
    Write-Host "[CoverageConverter][ExecutablePaths]: $ExecutablePaths"
    Write-Host "[CoverageConverter][SymbolPaths]: $SymbolPaths"
    Write-Host "[CoverageConverter][ScriptLocation]: $ScriptLocation"
    
    Add-Type -path "$CoverageAnalysisAssembly"

    $Result = 0
    if($InputCoveragePath -and (Test-Path "$InputCoveragePath") )
    {
        [string[]] $coverageFiles = $(Get-ChildItem -Path $InputCoveragePath -Recurse -Include *coverage)
        
        @($coverageFiles) | ForEach-Object {
            $coverageFile = $_
            $coverageFileOut = (Join-Path -Path $(Split-Path $_ -Parent) -ChildPath  ($(Get-Item $_).BaseName + "$OutputCoverageFileExtension"))

            Write-Host "If all OK the xml will be written to: $coverageFileOut"

            $info = [Microsoft.VisualStudio.Coverage.Analysis.CoverageInfo]::CreateFromFile($coverageFile, $ExecutablePaths, $SymbolPaths);
            if($info){
                $data = $info.BuildDataSet()
                $data.WriteXml($coverageFileOut)
            }
        }
    }
    else
    {
        Write-Host "Please specify a valid input coverage file."
        $Result = 1
    }

    Write-Host "[CoverageConverter][End]: Coverage conversion completed with result $Result"
    return $Result  
}

#Run the code in 32bit mode if PowerShell isn't already running in 32bit mode
If($env:PROCESSOR_ARCHITECTURE -ne "x86"){
    Write-Warning "Non-32bit architecture detected, processing original request in separate 32bit process."
    $Job = Start-Job $RunAs32Bit -RunAs32 -ArgumentList ($InputCoveragePath, $OutputCoverageFileExtension, $CoverageAnalysisAssembly, $ExecutablePaths, $SymbolPaths, $ScriptLocation)
    $Result = $Job | Wait-Job | Receive-Job
}Else{
    $Result = Invoke-Command -ScriptBlock $RunAs32Bit -ArgumentList ($InputCoveragePath, $OutputCoverageFileExtension, $CoverageAnalysisAssembly, $ExecutablePaths, $SymbolPaths, $ScriptLocation)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM