There are two different ways to feed the SonarQube platform with information on unit tests execution and code coverage:
- Letting Let the SonarQube platform drive the execution of the unit tests and the code coverage tool
- Reusing Reuse reports that have been generated by external tools prior to the SonarQube analysis
These two ways are handled by Gallio. So you first have to install itGallio on the machine(s) running the SonarQube analysis. You also have to install the code coverage tool of your choice that will be driven by Gallio. Supported tools are dotCover, NCover, OpenCover and PartCover.
Log in as a System administrator and go to Settings > Configuration > General Settings > .NET Gallio.
- Set the path to the Gallio installation directory through the
- Define which tool to use to compute the code coverage through the
sonar.gallio.coverage.toolproperty. Then, set the installation directory of this code coverage tool through the
sonar.partcover.installDirectoryproperty. Note that there is not such a property for NCover as the installation path of NCover is automatically discovered by Gallio.
The following command lines are launched while letting the SonarQube platform drive the execution of the unit tests and the code coverage tool. The reuseReport mode has been tested with those command lines. That's why , it is highly recommend to generate your reports by running using similar command lines:
# Based on the following solution sample: https://github.com/SonarCommunity/sonar-dotnet/tree/master/sonar/dotnet/sonar-dotnet-plugin/src/test/resources/solution/Example # dotCover C:\Program Files\JetBrains\dotCover\v2.2\Bin\dotCover.exe a /TargetExecutable=C:\Program Files\Gallio\bin\Gallio.Echo.exe /TargetWorkingDir=C:\Example\target\sonar "/TargetArguments=\"/r:Local\" \"/report-directory:C:\Example\target\sonar\" \"/report-name-format:gallio-report\" \"/report-type:Xml\" \"C:\Example\Example.Core.Tests\bin\Debug\Example.Core.Tests.dll\"" /Filters=+:module=Example.Application;class=*;function=*;+:module=Example.Core;class=*;function=*; /ReportType=TeamCityXML /Output=C:\Example\target\sonar\coverage-report.xml # NCover C:\Program Files\Gallio\bin\Gallio.Echo.exe /r:NCover3 /report-directory:C:\Example\target\sonar /report-name-format:gallio-report /report-type:Xml C:\Example\Example.Core.Tests\bin\Debug\Example.Core.Tests.dll /runner-property:NCoverCoverageFile=C:\Example\target\sonar\coverage-report.xml /runner-property:NCoverArguments=//ias Example.Application;Example.Core # OpenCover C:\Program Files\OpenCover\OpenCover.Console.exe -register:user -target:C:\Program Files\Gallio\bin\Gallio.Echo.exe -targetdir:C:\Example\target\sonar "-targetargs:\"/r:IsolatedAppDomain\" \"/report-directory:C:\Example\target\sonar\" \"/report-name-format:gallio-report\" \"/report-type:Xml\" \"C:\Example\Example.Core.Tests\bin\Debug\Example.Core.Tests.dll\"" "-filter:+[Example.Application]* +[Example.Core]* " -mergebyhash -output:C:\Example\target\sonar\coverage-report.xml # PartCover C:\Program Files\PartCover\PartCover .NET 4.0\PartCover.exe --target C:\Program Files\Gallio\bin\Gallio.Echo.exe --target-work-dir C:\Example\target\sonar --target-args \"/r:IsolatedAppDomain\" \"/report-directory:C:\Example\target\sonar\" \"/report-name-format:gallio-report\" \"/report-type:Xml\" \"C:\Example\Example.Core.Tests\bin\Debug\Example.Core.Tests.dll\" --include [Example.Application]* --include [Example.Core]* --output C:\Example\target\sonar\coverage-report.xml
Integration Tests and Code Coverage
As Because the principles are very similar to the those for unit tests. , please read the Unit Tests and Code Coveragebefore.
Letting the SonarQube platform drive the execution of the unit tests and the code coverage tool
# Paths are relative to the Solution base directory # Path to the integration tests execution report sonar.gallio.it.reports.path=reports/it-gallio-report.xml # Path to the code coverage report sonar.gallio.it.coverage.reports.path=reports/it-coverage-report.xml
Due to SONAR-4101, as execution data for integration tests are not currently stored as such. Thus, the C# plugin merged those merges integration test data with unit test onesdata.
Deactivating Integration Tests and Code Coverage
This might happen if you have generated the reports then moved the files of your solution. Indeed, in the XML coverage files, the paths to the C# source files must be the same as the paths specified in the ".csproj" files, otherwise the coverage data are simply not taken into account.
When using OpenCover with Gallio, I get 0% code coverage
, but I do have test classes that are covered by tests
Most probably, you should use the following property to run the tests in an isolated process:
The "sku" attribute is not something understandable by Gallio. .. If you remove replace the above XML fragment by with the one below, the issue is solved:
<startup> <supportedRuntime version="v4.0.30319"/> </startup>
The NCover Complete Edition allows you to run tests from multiple projects and merge the coverage data, the NCover Classic Edition does not.
sonar.gallio.timeoutMinutes property (default value is 30).
How can I run Gallio and PartCover on
Gallio and PartCover work perfectly out-of-the-box on x86 Windows, but not on 64-bit versions. To make them work:
[INFO] [23:30:05.643] A fatal exception occurred while running tests. Possible causes include invalid test runner parameters and stack overflows. [INFO] [23:30:05.727] Gallio.Model.ModelException: An exception occurred while invoking a test driver. ---> Gallio.Model.ModelException: Gallio.Model.ModelException: Could not load test assembly from 'C:\whatever\MyTestAssembly.Test.dll'. ---> System.BadImageFormatException: Could not load file...
that it might be because some of your assemblies have been compiled with a x86 profile (32 bits) while Gallio is configured to run in an "Any CPU" mode, hence 64bits on your system.
This problem might be solved using corflags tool to force Gallio to run in a 32 bits mode as shown below:
The problem might be solved if you modify your installation of Gallio. Add the following XML fragment in files Gallio.Host.x86.exe.config and/or Gallio.Host.x64.exe.config:
<startup useLegacyV2RuntimeActivationPolicy="true"> <supportedRuntime version="v4.0"/> </startup>