Measure and check code coverage
With Coco, you can measure and the code coverage of tests. The Coco plugin supports the setup of a project for code coverage and the display of the coverage inside Qt Creator.
To use the plugin, you must download and install Coco version 6.0 or later.
Note: Enable the Coco plugin to use it.
Configuring the plugin
There is a settings page at Preferences > Coco with which you can set the directory in which Coco is installed. But in most cases, the default settings need not to be changed.
Measure code coverage
With the Coco plugin, it is possible to set up code coverage easily for Qt Creator projects that are built with QMake or CMake.
The general idea is that you take an existing build configuration, like "Debug", clone it with a new name (like "DebugCoverage") and then use the plugin to configure it for the use with Coco. Switching back and forth between coverage and normal builds on the same build configuration is not recommended.
Components of the plugin
With the plugin enabled, C/C++ projects get
- A project settings menu at Projects > Project settings > Coco Code Coverage with which you can enable and configure code coverage.
- In the Build Settings, an additional pseudo build step that shows whether code coverage was enabled. There is also a button to directly disable or enable code coverage from this build step.
If code coverage is enabled, the plugin generates a settings file that is read by the build tool before the other configuration files and which changes the build process so that the Coco compiler wrappers are used instead of the original compiler. The settings file is always located in the root directory of the project sources. It also contains the coverage flags and possible overrides and can be checked in into version control to preserve the settings.
The Project settings page
The pages for QMake and CMake projects do not differ very much. They contain:
- A checkbox to enable and disable code coverage.
- A field to enter code coverage options. There are no settings that are needed to enable code coverage. Below the field are buttons:
- Exclude file... to exclude a file from instrumentation more easily.
- Exclude directory... to exclude a directory from instrumentation more easily.
- Override to open another entry field in which you can enter additional commands at the end of the settings file. It is meant for special cases in which the usual configuration flags are not enough.
- A button Revert to reload the coverage settings from the current settings file.
- A button Save or Save & Re-configure to write the settings to the settings file and reconfigure the project, if necessary.
- A list with the project build settings that were changed by the plugin.
QMake projects
The settings file is cocoplugin.prf
. It is a QMake "feature file".
For a command line build, qmake
must be run with the additional options CONFIG+=cocoplugin COCOPATH=<Coco directory>
. It is also necessary to set the environment variable QMAKEFEATURES
to the directory in which cocoplugin.prf
is located.
CMake projects
The settings file is cocoplugin.cmake
. It is a CMake cache preload script. Apart from this file, the "compiler files" cocoplugin-gcc.cmake
, cocoplugin-clang.cmake
and cocoplugin-visualstudio.cmake
are created in the same directory. They are needed for a command line build.
In a command-line build, run CMake in the form "cmake <other options> -C<project dir>/cocoplugin-gcc.cmake
" (if you are compiling with GCC). The file cocoplugin-gcc.cmake
includes then cocoplugin.cmake
.
If you use a compiler different from GCC, clang or Visual Studio, one of the compiler files must be modified for the new compiler.
Check code coverage
With the help of Coco CoverageBrowser, you can analyze the test coverage by loading an instrumentation database (a .csmes
file), which was generated by Coco CoverageScanner.
Configure Coco
- Go to Analyze > Squish Coco.
- In CSMes, select the instrumentation database to load.
- Select Open to start CoverageBrowser.
- In CoverageBrowser, go to File > Load Execution Report and select the
.csexe
file for the coverage scan. - To keep the execution report, clear Delete execution report after loading.
Open the analyzed files in Qt Creator. You can see the results of the analysis after the code in Edit mode. You can change the fonts and colors used for different types of results.
Changing Fonts and Colors
To change the default fonts and colors, go to Preferences > Text Editor > Font & Colors. Create your own color scheme and select new fonts and colors for the following results:
- Code Coverage Added Code
- Partially Covered Code
- Uncovered Code
- Fully Covered Code
- Manually Validated Code
- Code Coverage Dead Code
- Code Coverage Execution Count too Low
- Implicitly Not Covered Code
- Implicitly Covered Code
- Implicit Manual Coverage Validation
See also Enable and disable plugins, How To: Analyze, Analyzers, Font & Colors, and Analyzing Code.
Copyright © The Qt Company Ltd. and other contributors. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. 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.