I am trying to get coverage stats for our databases within SQ. Scans for issues etc. work as expected however I cannot get stats for tests and Code Coverage.
Unit Tests are created in Visual Studio and we are running ADO Builds. Typical build steps are:
1: Prepare Analysis on SQ - tried with and without the following advanced parameter - /d:sonar.cs.vscoveragexml.reportsPaths=$(Common.TestResultsDirectory)/**/*.coveragexml
2: Build Solution - Visual Studio Build
3: Test Assemblies - Visual Studio Test - Code Coverage Enabled
4: Run Code Analysis
5: Publish Quality Gate Result
Run Code Analysis seems to locate the files as expected:
The following test results files were found: D:\BuildAgents\pwtfs04_db.1_work\139\s\TestResults\TFSSERVICE_PWTFS00004_2020-07-07_16_46_43.trx
The following code coverage attachments were found from the trx files: D:\BuildAgents\pwtfs04_db.1_work\139\s\TestResults\TFSSERVICE_PWTFS00004_2020-07-07_16_46_43\In\PWTFS00004\TFSSERVICE_PWTFS00004 2020-07-07 16_46_16.coverage
There a few things that can cause this, but to know for sure, I would recommend running the analysis with debug logging enabled. You should be able to do this by setting -Dsonar.verbose=true in the command that runs the SonarScanner. With that set, you’ll be able to see more detailed logs when coverage is collected, particularly, whether or not the coverage sensor is skipping coverage data, and why. (mistmatched filenames is a common culprit in that case).
Anywho, let me know if you find something in the Debug logs.
‘VisualStudioCoverage’ skipped because there is no related file in current project
‘C# Unit Test Results Import’ skipped because there is no related file in current project
That’s interesting. On its face, the error below means that the coverage sensor is looking in the directory provided, but isn’t finding a coverage xml file. Can you check the directory reported in the logs and make sure the coverage xml file is there?
So a bit more playing around, I added a c# file into the database solution and the sonar version changed from 4.8.0 to 4.11.0, and we now have the number of tests executed and skipped etc. in Sonarqube however we still do not have coverage.
It now looks like its processing the coveragexml file:
2020-07-15T10:14:42.3458150Z 11:14:42.333 INFO: Parsing the Visual Studio coverage XML report D:\BuildAgents\pwtfs04_db.1_work\139\s\TestResults\TFSSERVICE_PWTFS00004_2020-07-15_11_13_25\In\PWTFS00004\TFSSERVICE_PWTFS00004 2020-07-15 11_13_13.coveragexml
2020-07-15T10:14:42.3736599Z 11:14:42.348 INFO: Adding this code coverage report to the cache for later reuse: D:\BuildAgents\pwtfs04_db.1_work\139\s\TestResults\TFSSERVICE_PWTFS00004_2020-07-15_11_13_25\In\PWTFS00004\TFSSERVICE_PWTFS00004 2020-07-15 11_13_13.coveragexml
The scanner version and msbuild version look good. I forgot to check on this, but can you check what version of the C# plugin you have on your SonarQube server? That’s the last part where version can affect coverage and overall analysis.
Also, to drill down on the coverage, can we set debug logging for the analysis? You can get these by setting -Dsonar.verbose=true in SONAR_SCANNER_OPTS. Then we can check the messages for the C# coverage sensor. The debug logs will show us why the sensor is ignoring coverage information.