Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3
Info
iconfalse
titleTable of Contents

Table of Contents
maxLevel1

Requirements

  • Unit / integration tests must be isolated from the main code base in dedicated Visual Studio projects.
  • Test framework(s) must be compatible with Gallio.

Unit Tests and Code Coverage

There are two different ways to feed the SonarQube platform with information on unit tests execution and code coverage:

  1. Let the SonarQube platform drive the execution of the unit tests and the code coverage tool
  2. 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 Gallio 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.

Configuring Gallio

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 sonar.gallio.installDirectory property.
  • Define which tool to use to compute the code coverage through the sonar.gallio.coverage.tool property. Then, set the installation directory of this code coverage tool through the sonar.dotcover.installDirectory or sonar.opencover.installDirectory or sonar.partcover.installDirectory property. Note that there is not such a property for NCover as the installation path of NCover is automatically discovered by Gallio.

Letting the SonarQube platform drive the execution of the unit tests and the code coverage tool

This is the default mode.

The first and unique step is to tell SonarQube which projects are unit test projects. To do so, set the sonar.donet.visualstudio.testProjectPattern property.

Code Block
languagebash
# Semicolon-separated list of patterns to identify unit test projects
# Default value is *.Tests
sonar.donet.visualstudio.testProjectPattern=*.Tests;*.UnitTests

Then, you just have to run a SonarQube analysis and you'll get data on unit tests and code coverage. Indeed, the paths to the unit test assemblies are automatically retrieved from the Visual Studio ".csproj" files. And the execution of unit tests and the driving of the coverage tool is automatically performed by Gallio.

Reusing existing reports

To activate this mode, add the following line to your project configuration file:

Code Block
languagenone
sonar.gallio.mode=reuseReport

Then, you just need to provide SonarQube with the following reports: unit tests execution and code coverage.

Code Block
languagebash
# Paths are relative to the Solution base directory

# Path to the unit tests execution report
sonar.gallio.reports.path=reports/gallio-report.xml

# Path to the code coverage report
sonar.gallio.coverage.reports.path=reports/coverage-report.xml

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 using similar command lines:

Code Block
languagehtml/xml
# 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

Deactivating Unit Tests and Code Coverage

Add the following line to your analysis configuration file:

Code Block
languagenone
sonar.gallio.mode=skip

Integration Tests and Code Coverage

Because the principles are very similar to those for unit tests, please read Unit Tests and Code Coverage.

Letting the SonarQube platform drive the execution of the unit tests and the code coverage tool

To activate this mode, add the following line to your analysis configuration file:

Code Block
languagenone
sonar.gallio.it.mode=active

The first and unique step is to tell SonarQube which projects are integration test projects. To do so, set the sonar.donet.visualstudio.itProjectPattern property.

Code Block
languagebash
# Semicolon-separated list of patterns to identify integration test projects
sonar.donet.visualstudio.itProjectPattern=*.Integration.Tests;*.IntegrationTests

Then, you just have to run a SonarQube analysis and you'll get data on integration tests and code coverage. Indeed, the paths to the integration test assemblies are automatically retrieved from the Visual Studio ".csproj" files. And the execution of integration tests and the driving of the coverage tool is automatically performed by Gallio.

Reusing existing reports

To activate this mode, add the following line to your project configuration file:

Code Block
languagenone
sonar.gallio.it.mode=reuseReport

Then, you just need to provide SonarQube with the following reports: integration tests execution report and code coverage report.

Code Block
languagebash
# 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, execution data for integration tests are not currently stored as such. Thus, the C# plugin merges integration test data with unit test data.

Deactivating Integration Tests and Code Coverage

This is the default mode.

FAQ

My unit tests and integration tests are defined within the same project, how can I run my unit tests only?

Define the sonar.gallio.filter property (or the sonar.gallio.it.filter property for integration tests):

Code Block
languagebash
# To run test from the "unit" category only
sonar.gallio.filter=CategoryName:unit

Note that Gallio does not support MSTest categories. Thus it is useless to specify this property when MSTest is used.

How can I exclude some specific projects and/or namespaces from code coverage computation?

Define the sonar.gallio.coverage.excludes property. Note that this property is shared between unit and integration tests.

Code Block
languagebash
# List of components to exclude. Syntax is different from one tool to another.

# dotCover filter format
# Exclusions can be defined at any levels: project, class, function
# Example: Excludes class Example.Core.MoneyBag and function Multiply defined in class Example.Core.Money
sonar.gallio.coverage.excludes=-:module=Example.Core;class=Example.Core.MoneyBag;function=*;-:module=Example.Core;class=Example.Core.Money;function=Multiply;

# NCover
# Exclusions can be defined at project level only
# Comma-separated list of projects to exclude
# Example: Excludes all components from projects Example.Core and Example.Model
sonar.gallio.coverage.excludes=Example.Core,Example.Model

# OpenCover
# Exclusions can be defined at project level only
# Comma-separated list of projects to exclude
# Example: Excludes all components from projects Example.Core and Example.Model
sonar.gallio.coverage.excludes=Example.Core,Example.Model

# PartCover
# Exclusions can be defined at project level only
# Comma-separated list of projects to exclude. Syntax: [project]*
# Example: Excludes all components from projects Example.Core and Example.Model 
sonar.gallio.coverage.excludes=[Example.Core]*,[Example.Model]*

SonarQube cannot retrieve the unit test assemblies

See Running an Analysis on a C# Solution, "SonarQube cannot retrieve the assemblies" section.

Code Block
languagebash
# If, for each test project, the test assembly is moved to a "bin/package" directory inside the project directory
sonar.dotnet.test.assemblies=bin/package/$(AssemblyName).$(OutputType)
 
# If all the assemblies are copied into a "package" directory located in the solution directory
sonar.dotnet.test.assemblies=$(SolutionDir)/package/$(AssemblyName).$(OutputType)

0% code coverage in reuseReport mode

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:

Code Block
languagebash
sonar.gallio.runner=IsolatedProcess

As describe in the Gallio documentation, possible values are IsolatedAppDomain (PartCover default runner type), IsolatedProcess and Local. Does not apply to NCover.

Gallio execution fails with following error message

Code Block
languagenone
Host process exited with code: -2146232576
A fatal exception occurred while running tests. Possible causes include invalid test runner parameters and stack overflows

This error happens when one of the dotnet config files (app.config, web.config, ...) of your Visual Studio solution specifies a runtime version not supported by the instance of Gallio. This is often the case when the following XML fragment appears in an app.config file:

Code Block
<startup>
   <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>

The "sku" attribute is not something understandable by Gallio. If you replace the above XML fragment with the one below, the issue is solved:

Code Block
<startup>
    <supportedRuntime version="v4.0.30319"/>
</startup>

NCover, the analysis succeeded but only one test project is taken into account

The NCover Complete Edition allows you to run tests from multiple projects and merge the coverage data, the NCover Classic Edition does not.

The workaround is to activate the Gallio safe mode (sonar.gallio.safe.mode=true). Gallio will then be launched for each test project and the generated XML reports will be merged.

How to set Gallio's timeout?

Set the sonar.gallio.timeoutMinutes property (default value is 30).

How can I run Gallio and PartCover on 64-bit Windows?

Gallio and PartCover work perfectly out-of-the-box on x86 Windows, but not on 64-bit versions. To make them work:

  1. Download the latest Gallio x64 installer and install it in a folder, but not under "Program Files".
  2. Download the latest PartCover installer and install it in a folder, but not under "Program Files".
  3. Run corflags.exe on Gallio and PartCover executables:

    Code Block
    languagebash
    # Go to the PartCover folder
    corflags /32bit+ /force PartCover.exe
    corflags /32bit+ /force PartCover.Browser.exe
    
    # Go to the Gallio bin folder
    corflags /32bit+ /force Gallio.Echo.exe
    

See http://www.planetgeek.ch/2009/10/15/get-partcover-running-on-x64-windows/

Gallio execution fails on a Windows 64-bit system

If in the logs you get something such as:

Code Block
[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...

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:

Code Block
corFlags.exe Gallio.Echo.exe /32BIT+ /Force

Some tests fail unexpectedly using a legacy .net2 library

If you get this kind of error:

Code Block
System.IO.FileLoadException: Mixed mode assembly is built against version 'v2.0.50727' of the runtime and cannot be loaded in the 4.0 runtime without additional configuration information.

It is likely that you are facing the issue described here

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:

Code Block
<startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0"/>
</startup>