我正在使用Jenkins为.Net项目设置CI。
我使用MSTest插件和VStestrunner插件来运行测试。 现在我有.trx文件和.Coverage文件 我在显示代码覆盖率报告方面遇到问题
请帮助我,你知道任何插件吗?
答案 0 :(得分:3)
要显示覆盖率报告,您需要将其转换为XML格式并使用MSTest插件发布报告。 MSTest插件建议(https://wiki.jenkins-ci.org/display/JENKINS/MSTest+Plugin)使用第三方应用程序转换XML格式和powershell(你需要安装pugin)才能运行它。
但是,您只能使用PowerShell进行转换。有脚本的例子:
$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)
您可能需要根据您的VS版本修复Microsoft.VisualStudio.Coverage.Analysis.dll的路径。
答案 1 :(得分:3)
我为此苦苦挣扎了很长时间,终于发现我们可以使用“ CodeCoverage.exe”,“ ReportGenarator.exe”和“ Cobertura插件”来显示完美的覆盖率报告。
“ ReportGenarator.exe”可以从https://github.com/danielpalme/ReportGenerator/releases获取
"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"
"ReportGenerator_4.4.7\net47\ReportGenerator.exe" -reports:./TestResults/coverage.xml -targetdir:./TestResults -reporttypes:cobertura
post {
always {
cobertura coberturaReportFile: './TestResults/Cobertura.xml'
}
}
答案 2 :(得分:0)
按照ghking的回答,cobertura抱怨说xml虽然在磁盘上却找不到。我必须从路径中删除“ ./”,以便cobertura能够找到该文件。
post {
always {
cobertura coberturaReportFile: 'TestResults/Cobertura.xml'
}
}
答案 3 :(得分:0)
执行此操作的完整脚本是:
<#
.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)
}