Creating an instrumented project
This chapter contains recipes for setting up a software project for instrumentation. The methods depend on the languages and the development environments. Here we show how to create a new software project so that it is prepared for instrumentation. This is another simple way to get acquainted with Coco.
A description of a larger project can be found in Code coverage for a Qt application.
C++ on Microsoft Visual Studio using the Visual Studio Coco Wizard
The add-in for Microsoft® Visual Studio® (see The Visual Studio Coco Wizard) supports working with Coco.
C# on Microsoft Visual Studio
Start Microsoft Visual Studio and create a new C# application:
- Click File > New > Project to open the new project wizard.
- Choose the Console App template for C# Windows.
- Enter the project name
squishcoco_sample
, and then click the Next button. - When the wizard's second page appears, click the Finish button.
To activate the instrumentation, use the project properties:
- In Solution Explorer, select Properties in the context menu.
- Select Build.
- In Conditional compilation symbols, enter
COVERAGESCANNER_COVERAGE_ON
.
The code coverage analysis is enabled when the symbol COVERAGESCANNER_COVERAGE_ON
is defined from the command line.
Build the squish_coco
project. This will cause the executable squishcoco_sample.exe
to be built and the code coverage instrumentation file squishcoco_sample.exe.csmes
to be generated. Double-click squishcoco_sample.exe.csmes
to inspect this file in CoverageBrowser.
Right now there are no code coverage statistics to be seen in CoverageBrowser because the application has not yet been executed. Click Program.cs
in the source list to display the main function. All the instrumented lines are shown grayed out to indicate that nothing has been executed.
Now execute squishcoco_sample.exe
by double-clicking it. This will result in a file called squishcoco_sample.exe.csexe
being generated. This file contains a code coverage snapshot which can be imported into CoverageBrowser:
- Click File > Load Execution Report.
- Select the File item and enter the path of the
squishcoco_sample.exe.csexe
file. - Click the Import button.
This will cause the code coverage statistics to be updated. Now, in the source code window, the main function's return statement will be colored green to indicate that this line has been executed.
Command line tools
Open a console window (MS-DOS Prompt or Command Prompt window on Windows) and make sure that the Microsoft Visual Studio compiler (cl.exe
) or GCC is installed on your system.
Create a simple hello.c
source file that contains the following code:
#include <stdio.h> int main(int argc, char *argv[]) { printf("hello\n"); return 0; }
Compile this program using the CoverageScanner compiler wrapper instead of the native compiler. This is done by prepending cs
to the command line compiler's name:
With Microsoft Visual Studio | With GCC |
---|---|
$ cscl.exe hello.c /Fehello.exe | $ csgcc hello.c -o hello.exe |
The executable hello.exe
and the file hello.exe.csmes
, which contains the code coverage instrumentation, will be generated.
Execute hello.exe
to generate hello.exe.csexe
. It contains a code coverage snapshot which can be imported into hello.exe.csmes
using cmcsexeimport
:
$ cmcsexeimport --title="Hello execution" -m hello.exe.csmes -e hello.exe.csexe
Once imported, the hello.exe.csexe
file is no longer needed and can be deleted. Now hello.exe.csmes
contains a single execution record. We can generate an HTML report with cmreport
:
$ cmreport --title="Hello application" -m hello.exe.csmes --html=hello.html
Here is a summary of the command line options that were used in this command:
--title="Hello application"
: Set a title for the report. It appears on all generated HTML pages.-m hello.exe.csmes
: Select the instrumentation database.--html=hello.html
: Specify the name of the HTML file that the report should be written to.
Coco v7.0.0 ©2023 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.