如何向 SonarQube 添加单元测试覆盖率?使用 Azure-DevOps 管道
How to add unit test coverage to SonarQube? Using Azure-DevOps Pipeline
我正在尝试将代码覆盖率添加到 sonarqube,包含并执行了测试,但覆盖率百分比为 0.0。
在我的管道中,我得到了 "Prepare analysis on SonarQube" 并且在我放置的附加属性中很好地配置了项目密钥 "sonar.cs.vscoveragexml.reportsPaths=**/*.coveragexml" 但没有任何改变,我也尝试过没有这个。
另一个步骤是 "VsTest - testAssemblies",我在其中选择了我的单元测试并选择了启用代码覆盖率框以及 "Run Code Analysis" 和 "Publish Quality Gate Result" 的另外两个步骤,这两个步骤与默认步骤一样,我试图增加超时但没有任何改变。
"VsTest - testAssemblies" 中的输出日志是
Results File: X:\VSTSWork\s\TestResults\PRO_2019-05-09_20_07_48.trx
Attachments:
X:\VSTSWork\s\TestResults\cdb1f30e-3958-4155-81e4-caa54693dfe9\PRO 2019-05-09 20_06_39.coverage
Total tests: 25. Passed: 25. Failed: 0. Skipped: 0.
Test Run Successful.
并在 "Run Code Analysis"
20:08:05.303 Fetching code coverage report information from TFS...
20:08:05.308 Attempting to locate a test results (.trx) file...
20:08:05.364 Looking for TRX files in: X:\VSTSWork\TestResults, X:\VSTSWork\s\TestResults
20:08:05.366 The following test results files were found: X:\VSTSWork\s\TestResults\PRO_2019-05-09_20_07_48.trx
20:08:05.433 The following code coverage attachments were found from the trx files: X:\VSTSWork\s\TestResults\PRO_2019-05-09_20_07_48\In\PRO\PRO 2019-05-09 20_06_39.coverage
WARNING: File 'C:\Users\pro\.nuget\packages\runtime.win-x64.microsoft.netcore.dotnethostpolicy.1.0\runtimes\win-x64\native\hostpolicy.dll' is not located under the root directory 'X:\VSTSWork\s' and will not be analyzed.
WARNING: File 'C:\Users\pro\.nuget\packages\runtime.win-x64.microsoft.netcore.dotnethostresolver.1.0\runtimes\win-x64\native\hostfxr.dll' is not located under the root directory 'X:\VSTSWork\s' and will not be analyzed.
WARNING: File 'C:\Users\pro\.nuget\packages\xunit.runner.visualstudio.3.1\build\netcoreapp1.0\xunit.runner.visualstudio.dotnetcore.testadapter.dll' is not located under the root directory 'X:\VSTSWork\s' and will not be analyzed.
WARNING: File 'C:\Users\pro\.nuget\packages\xunit.runner.visualstudio.3.1\build\netcoreapp1.0\xunit.runner.reporters.netcoreapp10.dll' is not located under the root directory 'X:\VSTSWork\s' and will not be analyzed.
WARNING: File 'C:\Users\pro\.nuget\packages\xunit.runner.visualstudio.3.1\build\netcoreapp1.0\xunit.runner.utility.netcoreapp10.dll' is not located under the root directory 'X:\VSTSWork\s' and will not be analyzed.
Calling the SonarQube Scanner...
.
..
...
INFO: Sensor C# Tests Coverage Report Import [csharp]
INFO: Parsing the Visual Studio coverage XML report X:\VSTSWork\.\s\TestResults\PRO_2019-05-09_20_07_48\In\PRO\PRO 2019-05-09 20_06_39.coveragexml
INFO: Adding this code coverage report to the cache for later reuse: X:\VSTSWork\.\s\TestResults\PRO_2019-05-09_20_07_48\In\PRO\PRO 2019-05-09 20_06_39.coveragexml
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
INFO: Sensor C# Tests Coverage Report Import [csharp] (done) | time=3701ms
INFO: Sensor C# Unit Test Results Import [csharp]
INFO: Parsing the Visual Studio Test Results file X:\VSTSWork\s\TestResults\PRO_2019-05-09_20_07_48.trx
INFO: Sensor C# Unit Test Results Import [csharp] (done) | time=64ms
INFO: Sensor Zero Coverage Sensor
INFO: Sensor Zero Coverage Sensor (done) | time=363ms
INFO: Sensor CPD Block Indexer
INFO: Sensor CPD Block Indexer (done) | time=0ms
INFO: 49 files had no CPD blocks
INFO: Calculating CPD for 128 files
INFO: CPD calculation finished
INFO: Analysis report generated in 7191ms, dir size=810 KB
INFO: Analysis reports compressed in 1207ms, zip size=501 KB
INFO: Analysis report uploaded in 89ms
终于,我找到了问题所在,
当我添加时它为我解决了:
<DebugType>Full</DebugType>
在.csproj
。奇怪的是,其他项目报告了一些没有这个 属性 的代码覆盖率,所以我认为我不需要它。
因此,如果报告的覆盖率看起来是假的,请尝试在 csproj
中添加此 属性。
我在
中找到了这个
我正在尝试将代码覆盖率添加到 sonarqube,包含并执行了测试,但覆盖率百分比为 0.0。
在我的管道中,我得到了 "Prepare analysis on SonarQube" 并且在我放置的附加属性中很好地配置了项目密钥 "sonar.cs.vscoveragexml.reportsPaths=**/*.coveragexml" 但没有任何改变,我也尝试过没有这个。
另一个步骤是 "VsTest - testAssemblies",我在其中选择了我的单元测试并选择了启用代码覆盖率框以及 "Run Code Analysis" 和 "Publish Quality Gate Result" 的另外两个步骤,这两个步骤与默认步骤一样,我试图增加超时但没有任何改变。
"VsTest - testAssemblies" 中的输出日志是
Results File: X:\VSTSWork\s\TestResults\PRO_2019-05-09_20_07_48.trx
Attachments:
X:\VSTSWork\s\TestResults\cdb1f30e-3958-4155-81e4-caa54693dfe9\PRO 2019-05-09 20_06_39.coverage
Total tests: 25. Passed: 25. Failed: 0. Skipped: 0.
Test Run Successful.
并在 "Run Code Analysis"
20:08:05.303 Fetching code coverage report information from TFS...
20:08:05.308 Attempting to locate a test results (.trx) file...
20:08:05.364 Looking for TRX files in: X:\VSTSWork\TestResults, X:\VSTSWork\s\TestResults
20:08:05.366 The following test results files were found: X:\VSTSWork\s\TestResults\PRO_2019-05-09_20_07_48.trx
20:08:05.433 The following code coverage attachments were found from the trx files: X:\VSTSWork\s\TestResults\PRO_2019-05-09_20_07_48\In\PRO\PRO 2019-05-09 20_06_39.coverage
WARNING: File 'C:\Users\pro\.nuget\packages\runtime.win-x64.microsoft.netcore.dotnethostpolicy.1.0\runtimes\win-x64\native\hostpolicy.dll' is not located under the root directory 'X:\VSTSWork\s' and will not be analyzed.
WARNING: File 'C:\Users\pro\.nuget\packages\runtime.win-x64.microsoft.netcore.dotnethostresolver.1.0\runtimes\win-x64\native\hostfxr.dll' is not located under the root directory 'X:\VSTSWork\s' and will not be analyzed.
WARNING: File 'C:\Users\pro\.nuget\packages\xunit.runner.visualstudio.3.1\build\netcoreapp1.0\xunit.runner.visualstudio.dotnetcore.testadapter.dll' is not located under the root directory 'X:\VSTSWork\s' and will not be analyzed.
WARNING: File 'C:\Users\pro\.nuget\packages\xunit.runner.visualstudio.3.1\build\netcoreapp1.0\xunit.runner.reporters.netcoreapp10.dll' is not located under the root directory 'X:\VSTSWork\s' and will not be analyzed.
WARNING: File 'C:\Users\pro\.nuget\packages\xunit.runner.visualstudio.3.1\build\netcoreapp1.0\xunit.runner.utility.netcoreapp10.dll' is not located under the root directory 'X:\VSTSWork\s' and will not be analyzed.
Calling the SonarQube Scanner...
.
..
...
INFO: Sensor C# Tests Coverage Report Import [csharp]
INFO: Parsing the Visual Studio coverage XML report X:\VSTSWork\.\s\TestResults\PRO_2019-05-09_20_07_48\In\PRO\PRO 2019-05-09 20_06_39.coveragexml
INFO: Adding this code coverage report to the cache for later reuse: X:\VSTSWork\.\s\TestResults\PRO_2019-05-09_20_07_48\In\PRO\PRO 2019-05-09 20_06_39.coveragexml
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
INFO: Sensor C# Tests Coverage Report Import [csharp] (done) | time=3701ms
INFO: Sensor C# Unit Test Results Import [csharp]
INFO: Parsing the Visual Studio Test Results file X:\VSTSWork\s\TestResults\PRO_2019-05-09_20_07_48.trx
INFO: Sensor C# Unit Test Results Import [csharp] (done) | time=64ms
INFO: Sensor Zero Coverage Sensor
INFO: Sensor Zero Coverage Sensor (done) | time=363ms
INFO: Sensor CPD Block Indexer
INFO: Sensor CPD Block Indexer (done) | time=0ms
INFO: 49 files had no CPD blocks
INFO: Calculating CPD for 128 files
INFO: CPD calculation finished
INFO: Analysis report generated in 7191ms, dir size=810 KB
INFO: Analysis reports compressed in 1207ms, zip size=501 KB
INFO: Analysis report uploaded in 89ms
终于,我找到了问题所在,
当我添加时它为我解决了:
<DebugType>Full</DebugType>
在.csproj
。奇怪的是,其他项目报告了一些没有这个 属性 的代码覆盖率,所以我认为我不需要它。
因此,如果报告的覆盖率看起来是假的,请尝试在 csproj
中添加此 属性。
我在