Enable code coverage

You can build your project with code coverage enabled.

Code coverage uses a signal to tell the application to deliver its information back to the IDE. Depending on the design of the application, there are several possible risks that can result from running code coverage from the IDE:

  • It can modify or break the behavior of applications that are monitored by code coverage.
  • It can cause code to run that a test suite does not actually test.
  • It can result in data not actually being collected at all.

  1. In the Project Explorer view, right-click your project, and then click Properties. The properties dialog for your project appears.
  2. In the left pane, expand C/C++ Build, and then click Settings.
  3. On the Tool Settings tab, expand QCC Compiler or QCC Linker, and then click Build for Code Coverage.
  4. Click OK.
  5. In the Project Explorer view, right-click your project and select Clean Project.
  6. On the toolbar, click Build The Build button on the toolbar..

Enable code coverage for make projects

If you're using your own custom build environment, rather than the project build environment, you'll have to manually pass the coverage option to the compiler.
To enable code coverage for non-QNX projects:
If you're using qcc/gcc, compile and link with the following options:
-fprofile-arcs -ftest-coverage

For example, your Makefile might look something like the Makefile below, which belongs to the Code Coverage example project included with the IDE (although, this example includes additional comments):

DEBUG = -g 
CC = qcc 
LD = qcc 
CFLAGS += -Vgcc_ntox86 $(DEBUG) -c -Wc,-Wall -I. -O0 -Wc,-ftest-coverage 
-Wc,-fprofile-arcs 
LDFLAGS+= -Vgcc_ntox86 $(DEBUG) -ftest-coverage -fprofile-arcs  

# CC refers to the program for compiling C programs (the default is 
# qcc. Use 
# CXX as the program for compiling C++ programs. 

# CFLAGS are additional flags to give to the C compiler. Use CFLAGS  
# for the C++ compiler. 

# -c compiles or assemble the source files, but doesn't link, and the 
# -Wc captures the warning messages. The linking stage isn't done. 
# The ultimate output is in the form of an object file for each
# source file. 

# -Wall turns on all optional warnings that are desirable for normal  
# code. -I. adds the current directory to the list of directories to  
# search for header files. Directories named by -I are searched before  
# the standard system include directories. 

# -O0 is an optimization flag that indicates 'Do not optimize.' 

# LDFLAGS are additional flags to give to compilers when they invoke 
# the ld linker. 

# -ftest-coverage -Wc means that Code Coverage is enabled for your  
# project, and the data is used for test coverage analysis. 

# -fprofile-arcs adds code so that program flow arcs are instrumented.  
# During execution, the program records how many times each branch and 
# call is executed and how many times it is taken or returns, and it 
# saves this data to a file with the extension .gcda for each source  
# file.
# 
# For Code Coverage, you'll need the -fprofile-arcs -ftest-coverage  
# options in both the compile and link lines.

dir := $(shell pwd) 

BINS = rbt_client rbt_server 

# This next line is the rule for <cmd>all</cmd> that  
# incrementally builds your system by performing a <cmd>make</cmd>  
# of all the top-level targets the Makefile knows about. It does this by 
# expressing a dependency on the results of that system, which in turn  
# have their own rules and dependencies. 

all: $(BINS) 

# The following line shows a simple rule for cleaning your build  
# environment. It cleans your build environment by deleting all files  
# that are normally created by running make. 

# It has a Target named <cmd>clean</cmd> to left of the colon,  
# no dependencies (to the right of the colon), and two commands that are 
# indented by tabs on the lines that follow. 

clean: 
rm -f *.o *.img *.gcno *.gcda $(BINS) 

# The following lines are Dependency Rules, which are rules without any  
# command. If any file to the right of the colon changes, the target to  
# the left of the colon is no longer considered current (out of date). 
# Dependency Rules are often used to capture header file dependencies. 

rbt_server: rbt_server.o 

# Alternatively, to manually capture dependencies, several automated 
# dependency generators exist. 

rbt_server.o : rbt_server.c rbt_server.h 
$(CC) $(CFLAGS) $(dir)/$< 

rbt_client: rbt_client.o 

rbt_client.o: rbt_client.c rbt_server.h 
$(CC) $(CFLAGS) $(dir)/$< 

To enable Code Coverage for your project, you must use the options -fprofile-arcs -ftest-coverage when compiling and linking.

For example, in the Makefile, you'll have the following gcc options set for Code Coverage:

CFLAGS += -g -fprofile-arcs -ftest-coverage 
LDFLAGS+= -g -fprofile-arcs -ftest-coverage 

Start a coverage-enabled program

To start a program and measure the code coverage:
  1. Create a BlackBerry remote launch configuration as you normally would, but don't click OK yet.
  2. On the launcher, click the Tools tab.
  3. Click Add/Delete Tool. The Tools selection dialog appears.
  4. Select the Code Coverage tool.

    Screen showing adding the Code Coverage tool.

  5. Click OK.
  6. Click the Code Coverage tab, and fill in these fields:

    Screen showing the Code Coverage tool.

    Code Coverage data format
    Select gcc 4.3 or later to enable code coverage metrics collection if your application was compiled with gcc 4.3 or later.
    Comments for this Code Coverage session
    Your notes about the session, for your own personal use. The comments appear at the top of the generated reports.
    Code Coverage data scan interval (sec)
    Sets how often the Code Coverage tool polls for data. A low setting can cause continuous network traffic. The default setting of 5 seconds should be sufficient.
    Collect data for
    By default, all code coverage data built with code coverage in a project is included in the current Code Coverage session. To include referenced projects or to only collect data from certain sources, disable the option All Sources in this application compiled with code coverage, and then click Select to select the projects or files that you want to collect code coverage data for.
    Select
    Opens the Projects to include Code Coverage data from dialog so you can choose projects to include your coverage data (projects and files). Select any project from this list that you wish to gather code coverage data for. Note that projects must be built with code coverage enabled to capture data.

    Screen showing selecting projects for code coverage.

  7. Optional: Click Advanced to define a signal to enable the dynamic collection of code coverage data. The IDE will send a signal to suspend the application thread so that it can perform data collection.

    Screen showing defining a signal to enable the dynamic collection of code coverage data.

  8. Check Switch to this tool's perspective on launch if you want to automatically go to the QNX Code Coverage perspective when you run or debug.
  9. Click Apply.
  10. Click Run or Debug.

Last modified: 2013-12-21

comments powered by Disqus