简体   繁体   English

如何将 nUnit/OpenCover 结果导入 SonarQube

[英]How to import nUnit/OpenCover results to SonarQube

We are running SonarQube Community Edition Version 7.9.1 (build 27448) (the currently latest docker image)我们正在运行 SonarQube 社区版 7.9.1 版(内部版本 27448)(当前最新的 docker 镜像)

I have some nUnit tests but I'm struggling to get the code coverage data imported to SonarQube.我有一些 nUnit 测试,但我正在努力将代码覆盖率数据导入 SonarQube。

I have successfully used SonarScanner.MSBuild (sonar-scanner-msbuild-4.6.2.2108-net46) to import the code analysis and number of nUnit tests to SonarQube using the following commands:我已成功使用 SonarScanner.MSBuild (sonar-scanner-msbuild-4.6.2.2108-net46) 使用以下命令将代码分析和 nUnit 测试数量导入 SonarQube:

SonarScanner.MSBuild.exe begin /k:"ProjectName" /d:sonar.host.url="..." /d:sonar.login="..." /d:sonar.cs.nunit.reportsPaths="NUnitResults.xml" 
MSBuild.exe ....sln /t:Rebuild
nunit3-console.exe --result=NUnitResults.xml path\to\tests.dll
SonarScanner.MSBuild.exe end /d:sonar.login="..."

Now I want to use OpenCover to get the code coverage info to SonarQube so I'm using the following commands现在我想使用 OpenCover 来获取 SonarQube 的代码覆盖率信息,所以我使用以下命令

SonarScanner.MSBuild.exe begin /k:"ProjectName" /d:sonar.host.url="..." /d:sonar.login="..." /d:sonar.cs.nunit.reportsPaths="NUnitResults.xml" /d:sonar.cs.opencover.reportsPaths="opencover.xml"
MSBuild.exe ....sln /t:Rebuild
nunit3-console.exe --result=NUnitResults.xml path\to\tests.dll
OpenCover.Console.exe -output:opencover.xml -register:user -target:"nunit3-console.exe" -targetargs:"path\to\tests.dll --result=NUnitResults.xml"
SonarScanner.MSBuild.exe end /d:sonar.login="..."

[I think I'm probably unnecessarily running the nUnit tests twice, but I'll sort that out once I've resolved the following issue] [我想我可能不必要地运行了两次 nUnit 测试,但是一旦我解决了以下问题,我就会解决这个问题]

The problem is I'm not getting any code coverage details on SonarQube.问题是我没有在 SonarQube 上获得任何代码覆盖率详细信息。

Checking the SonarScanner log I can see the following output from OpenCover:检查 SonarScanner 日志,我可以看到 OpenCover 的以下输出:

Executing: nunit3-console.exe
NUnit Console Runner 3.10.0 (.NET 2.0)
Copyright (c) 2019 Charlie Poole, Rob Prouse
06 September 2019 09:24:53

Runtime Environment
   OS Version: Microsoft Windows NT 10.0.14393.0
  CLR Version: 4.0.30319.42000

Test Files
    path\to\UnitTests.dll


Run Settings
    DisposeRunners: True
    WorkDirectory: ...
    ImageRuntimeVersion: 4.0.30319
    ImageTargetFrameworkName: .NETFramework,Version=v4.6.1
    ImageRequiresX86: False
    ImageRequiresDefaultAppDomainAssemblyResolver: False
    NumberOfTestWorkers: 8

Test Run Summary
  Overall result: Passed
  Test Count: 35, Passed: 35, Failed: 0, Warnings: 0, Inconclusive: 0, Skipped: 0
  Start time: 2019-09-06 08:24:54Z
    End time: 2019-09-06 08:24:57Z
    Duration: 3.111 seconds

Results (nunit3) saved as NUnitResults.xml
Committing...
Visited Classes 12 of 12 (100)
Visited Methods 41 of 41 (100)
Visited Points 240 of 240 (100)
Visited Branches 41 of 41 (100)

==== Alternative Results (includes all methods including those without corresponding source) ====
Alternative Visited Classes 12 of 12 (100)
Alternative Visited Methods 41 of 53 (77.36)

Now checking the dir I can see I have NUnitResults.xml and opencover.xml created and populated.现在检查目录,我可以看到我创建并填充了NUnitResults.xmlopencover.xml (I don't really know how to read OpenCover results but it has data for my unit tests) (我真的不知道如何读取 OpenCover 结果,但它有用于我的单元测试的数据)

The SonarScanner.MSBuild.exe end log has this tidbit: SonarScanner.MSBuild.exe end日志有这个SonarScanner.MSBuild.exe end

INFO: Sensor C# Tests Coverage Report Import [csharp]信息:传感器 C# 测试覆盖率报告导入 [csharp]

INFO: Parsing the OpenCover report path.\\opencover.xml信息:解析 OpenCover 报告路径。\\opencover.xml

INFO: Adding this code coverage report to the cache for later reuse: path.\\opencover.xml信息:将此代码覆盖率报告添加到缓存以供以后重用:path.\\opencover.xml

WARN: The Code Coverage report doesn't contain any coverage data for the included files.警告:代码覆盖率报告不包含包含文件的任何覆盖率数据。 For troubleshooting hints, please refer to https://docs.sonarqube.org/x/CoBh有关故障排除提示,请参阅https://docs.sonarqube.org/x/CoBh

INFO: Sensor C# Tests Coverage Report Import [csharp] (done) |信息:传感器 C# 测试覆盖率报告导入 [csharp](完成)| time=14ms时间=14ms

INFO: Sensor C# Unit Test Results Import [csharp]信息:传感器 C# 单元测试结果导入 [csharp]

INFO: Parsing the NUnit Test Results file path.\\NUnitResults.xml信息:解析 NUnit 测试结果文件路径。\\NUnitResults.xml

INFO: Sensor C# Unit Test Results Import [csharp] (done) |信息:传感器 C# 单元测试结果导入 [csharp](完成)| time=34ms时间=34ms

Now the classes that the unit tests cover are included (I can see them in SonarQube marked as uncovered) so I guess either OpenCover isn't collecting the correct info or something is going wrong matching the covered files with the file in the solution.现在包含了单元测试涵盖的类(我可以在 SonarQube 中看到它们被标记为未覆盖),所以我想要么 OpenCover 没有收集正确的信息,要么在将覆盖的文件与解决方案中的文件匹配时出现问题。

nb Checking the project that should be covered by these tests, under Project > Build > Advanced the Debug Info is set to full for Debug configuration, which some resources seem to suggest is required. nb 检查这些测试应该涵盖的项目,在 Project > Build > Advanced 下,Debug Info 设置为full的 Debug 配置,这似乎需要一些资源。

Any idea what I'm missing/where I'm going wrong?知道我错过了什么/我哪里出错了吗?

UPDATE更新

Checking the bottom of opencode.xml it has the following:检查opencode.xml的底部,它有以下内容:

<Module skippedDueTo="MissingPdb" hash="DD-83-09-69-9F-A7-11-FF-F5-BC-43-7C-87-B2-54-99-0D-A5-D5-61">
  <ModulePath>C:\Windows\Microsoft.Net\assembly\GAC_MSIL\MyCompany.Common.Utility\v4.0_1.0.0.0__bf95fa7f15863c9f\MyCompany.Common.Utility.dll</ModulePath>
  <ModuleTime>2019-09-05T16:07:28.6111533Z</ModuleTime>
  <ModuleName>MyCompany.Common.Utility</ModuleName>
  <Classes />
</Module>

The original authors of this solution seemed to like shoving everything in the GAC, so I'm guessing OpenCover is looking in the GAC rather than locally for the dll, and the GAC doesn't hold pdbs.这个解决方案的原始作者似乎喜欢把所有东西都塞进 GAC 中,所以我猜 OpenCover 是在 GAC 中而不是在本地查找 dll,并且 GAC 不保存 pdbs。

I tried setting the OpenCover -targetdir setting as suggested in the manual:我尝试按照手册中的建议设置 OpenCover -targetdir设置:

The path to the target directory;目标目录的路径; if the target argument already contains a path then this argument can be used to provide an alternate path where PDB files may be found.如果目标参数已包含路径,则此参数可用于提供可在其中找到 PDB 文件的备用路径。

I also tried to use -searchdirs (Alternative locations to look for PDBs.)我还尝试使用-searchdirs (寻找 PDB 的替代位置。)

but neither of them resolved it.但他们都没有解决。

Can I tell OpenCover to use the bin\\Debug pdbs?我可以告诉 OpenCover 使用 bin\\Debug pdbs 吗?

https://github.com/OpenCover/opencover/wiki/Usage#notes-on-spaces-in-arguments https://github.com/OpenCover/opencover/wiki/Usage#notes-on-spaces-in-arguments

Using \\" in targetargs to represent quotes to nunit3-console在 targetargs 中使用 \\" 来表示 nunit3-console 的引号

SonarScanner.MSBuild.exe begin /k:"ProjectName" /d:sonar.host.url="..." /d:sonar.login="..." /d:sonar.cs.nunit.reportsPaths="NUnitResults.xml" /d:sonar.cs.opencover.reportsPaths="opencover.xml"
MSBuild.exe ....sln /t:Rebuild
OpenCover.Console.exe -output:opencover.xml -register:user -target:"nunit3-console.exe" -targetargs:"\"path\to\tests.dll\" --result=NUnitResults.xml"
SonarScanner.MSBuild.exe end /d:sonar.login="..."
Step1 : SonarScanner.MSBuild.exe begin /k:"Project-Name" /d:sonar.cs.opencover.reportsPaths=%cd%\TestCoverResult.xml /d:sonar.exclusions="**/*.css,**/*.js,**/*.cshtml" /d:sonar.verbose="true" /d:sonar.host.url="https://host-url.com/" /d:sonar.login="Login-Token"

Step2 :  "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\Bin\MSBuild.exe" /t:Rebuild  

Step3 : ".\packages\OpenCover.4.7.1221\tools\OpenCover.Console.exe" "-target:.\packages\NUnit.ConsoleRunner.3.12.0\tools\nunit3-console.exe"  "-targetargs:.\Test-Project\bin\Debug\Test-Project.dll" "-output:.\TestCoverResult.xml" -register:user

Step4 :  SonarScanner.MSBuild.exe end   

Explanation:解释:

1 : reportsPath : specifies the path of the code-coverage result file. 1: reportsPath :指定代码覆盖结果文件的路径。

2 : ".\\packages\\OpenCover.4.7.1221\\tools\\OpenCover.Console.exe" : it is system path of the "OpenCover.Console.exe" 2 : ".\\packages\\OpenCover.4.7.1221\\tools\\OpenCover.Console.exe" : 是“OpenCover.Console.exe”的系统路径

3 : ".\\packages\\NUnit.ConsoleRunner.3.12.0\\tools\\nunit3-console.exe" : it is system path of the "nunit3-console.exe" 3 : ".\\packages\\NUnit.ConsoleRunner.3.12.0\\tools\\nunit3-console.exe" : 它是“nunit3-console.exe”的系统路径

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

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