【问题标题】:Sonar MSBuild: Getting coverage reports on LinuxSonar MSBuild:在 Linux 上获取覆盖率报告
【发布时间】:2018-07-18 21:11:09
【问题描述】:
我已经设法在 debian:stretch docker 容器上使用 SonarQube Scanner for MSBuild 对 .NET 核心项目执行静态代码分析。
我现在正在尝试生成一份覆盖率报告。
除非我错了,相关的guidelines暗示你不能只使用现有的报告,而是遵循一个动态的过程
-
beginning 通过指向报告(待完成)路径进行分析
- 通过以下工具之一运行实际覆盖范围:
a) Visual Studio 代码覆盖率
b) 点覆盖
c) 打开封面
- 导入报告
-
ending 由 msbuild 发起的分析
我的问题是是否可以在 Linux 上运行上述过程(尚未设法这样做或找到任何资源)
【问题讨论】:
标签:
.net
sonarqube
code-coverage
sonarqube-msbuild-runner
【解决方案1】:
我已经设法从覆盖物 (https://github.com/tonerdo/coverlet) 中获得覆盖,但到目前为止还无法让 msbuild 完成声纳扫描仪。一些配置问题,希望我能尽快解决。
我使用的脚本是:
#!/bin/bash -e
api_key=$API_KEY
# Get nuget packages and then build DEBUG
dotnet restore --packages packages --configfile nuget.config /nowarn:NU1701,NU1603,NU1605 MyApp.sln
# set the version on all the projects
echo Set SDK Versions to [$apiversion]
find sdk -name "*.csproj" -print | xargs -I £ sed -ie "s/<Version>.*<\/Version>/<Version>${apiversion}<\/Version>/g" £
echo Set Implementation Versions to [$version]
find implementation -name "*.csproj" -print | xargs -I £ sed -ie "s/<Version>.*<\/Version>/<Version>${version}<\/Version>/g" £
echo Starting SONAR...
export token=$SONAR_TOKEN
dotnet /bin/SonarScanner.MSBuild.dll begin \
/d:sonar.login=${token} \
/d:sonar.host.url=https://my-sonar-server.com \
/v:$version \
/k:$key \
/d:sonar.analysis.mode=preview \
/d:sonar.cs.opencover.reportsPaths="$(find . -name coverage.xml | tr '\n' ',')" \
/d:sonar.coverage.exclusions=tests/** \
/d:sonar.cs.vstest.reportsPaths="$(pwd)/.output/*.trx" \
/d:sonar.verbose=true
echo Build solution...
dotnet build --no-restore /nowarn:NU1701,NU1603,NU1605 Core.sln /clp:PerformanceSummary
# Run the tests
for testproj in $(ls tests/*.Tests -d); do
echo $testproj;
set +e
time \
dotnet test \
--no-build \
--no-restore \
--filter "TestCategory!=Integration" \
--logger:trx \
-r .output/ \
$testproj \
/nowarn:NU1701,NU1603,NU1605 \
/p:CollectCoverage=true \
/p:CoverletOutputFormat=opencover
set -e
done
dotnet /bin/SonarScanner.MSBuild.dll end /d:sonar.login=$token
您需要在每个测试程序集中引用 Coverlet。
【解决方案2】:
覆盖率报告以及 SonarQube 集成 可以通过 minicover 进行。
Minicover 现在使用 mini-OpenCover 生成(并上传到 SQ 服务器)与 SQ 兼容的覆盖率报告;
或多或少应该遵循的程序,脚本:
(假设tools是为minicover执行nugget安装的文件夹)
echo "Starting sonarqube integration"
mono /home/pathTo/SonarQube.Scanner.MSBuild.exe begin /k:"MyProject" /d:sonar.host.url="http://localhost:9000" /d:sonar.login="myLoginId" /d:sonar.cs.opencover.reportsPaths="coverage.xml"
dotnet restore
dotnet build
cd tools
export "MiniCover=dotnet minicover"
# Instrument assemblies inside 'MyTestFolder' folder to detect hits for source files inside 'MySrcFolder' folder
$MiniCover instrument --workdir ../ --assemblies MyTestFolder/**/bin/**/*.dll --sources MySrcFolder/**/*.cs
# Reset hits count in case minicover was run for this project
$MiniCover reset
cd ..
dotnet test --no-build ./MyTestFolder/MyTest.csproj
cd tools
# Uninstrument assemblies, it's important if you're going to publish or deploy build outputs
$MiniCover uninstrument --workdir ../
# Create html reports inside folder coverage-html
$MiniCover opencoverreport --workdir ../ --threshold 90
# Print opencover report
$MiniCover opencoverreport --workdir ../ --threshold 90
cd ..
echo "Ending sonarqube integration..."
mono /home/pathTo/SonarQube.Scanner.MSBuild.exe end /d:sonar.login="myLoginId"
更广泛的讨论/详细信息可以在 this 和 this 线程上找到。