C# Coverage

C# instrumentation is similar to C/C++ instrumentation, but with some changes that are caused by the nature of the C# compiler.

Installation

C# support is automatically installed together with C/C++ coverage. There is however one difference that must be taken into account when Coco is used together with Microsoft Visual Studio.

For Visual Studio, the Coco installer replaces the C# compiler csc.exe with a copy of CoverageScanner – the original csc.exe gets renamed. This is the only way to make sure that CoverageScanner gets called when a C# file is called. But this also means that when Visual Studio is updated and there is a new compiler version, csc.exe is no longer a copy of CoverageScanner and code coverage does not work. In this case, a re-install of Coco solves the problem. (It is not necessary to uninstall Coco first.)

Note: If C# coverage no longer works, try first re-installing Coco.

Instrumentation of C# code

Microsoft Visual Studio

There are two ways to activate the instrumentation of a C# project.

Mono C# XBuild

To instrument a project that is generated with Mono C# XBuild, run the following shell commands:

export COVERAGESCANNER_ARGS=--cs-on
msbuild \
/p:UseEnv=true \
/p:UseHostCompilerIfAvailable=true \
/p:CscToolPath="${SQUISHCOCO}/wrapper" \
myproject.sln /t:ReBuild

(The environment variable SQUISHCOCO contains the path to the CoverageScanner executable.)

Advanced setup

  • To modify the behavior of the instrumented program at runtime, use the C# Library.
  • To modify the instrumentation only in a part of a C# source file, use C# Regions.

Support for unit test coverage

The general process for integration with testing frameworks is described in the chapter about Use of the Coco library.

See here for the xUnit testing framework.

Coco v7.2.0 ©2024 The Qt Company Ltd.
Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.