C++test Cpptestcc Advanced Tutorial: Including Static Libraries Coverage

MichaelCMichaelC Posts: 28 admin

This is a second part to the following
forum post: https://forums.parasoft.com/discussion/4423/c-test-latest-coverage-collecting-features#latest

This post assumes that you understand how the basic of instrumenting source code for coverage collection via cpptestcc and how to use/build C++test Coverage Runtime libraries. We are going to discuss how we can use these features to collect coverage on a project that calls on static libraries and how we can collect aggregated coverage in a single report.

In older editions of C++test this was extremely difficult to accomplish. With the addition of our latest toolset, this has become significantly easier to collect and report aggregated coverage.

The following tutorial uses the following:

Ubuntu 18.04
GCC 7.4.0 64-bit compiler
C++test 10.4.2 Professional Edition Standalone

The example workspace that I referred to attached to this post.

Before working through this tutorial verify that your toolchain and OS are supported with C++test 10.4.X or later.

C++test 10.4.2 Supported Environment:

I would recommend creating a separate project configuration so you can quickly switch between building your project for coverage collection and the project configuration for developing your project.

To make a new project configuration follow the steps below:

  1. Right-click on your desired project and select Properties
  2. Go to C/C++ Build
  3. Click on Manage Configurations...
  4. Fill up the name of your new Project Configuration along with a description to help you differentiate from other project configurations.
  5. In the copy settings from select Existing Configuration and choose the desired Project Configuration

Below is a screenshot what this configuration should look like

Select Settings -> Tool Settings and select either Cross GCC Compiler (used for C projects) or Cross G++ Compiler (used for GCC) and add cpptestcc and the appropriate flags before the compiler options.

We will now add the static libraries directories to linker phase. We can add these options by going to Cross G++ Linker.

You will also need to manually add the path to the C++test Coverage Runtime library in the main Executable project linker options. In this example, I will be using the cpptest.o that is built by default in the C++test Coverage Library directory.

The command line execution should look something like the following.

Static Library 1 Build with cpptestcc instrumentation:

cpptestcc -line-coverage -compiler gcc_7-64 -- g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"lib1.d" -MT"lib1.d" -o "lib1.o" "../lib1.cpp"

ar -r "libStaticLib1.a" ./lib1.o

Static Library 2 Build with cpptestcc instrumentation:

cpptestcc -line-coverage -compiler gcc_7-64 -- g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"lib2.d" -MT"lib2.d" -o "lib2.o" "../lib2.cpp"

ar -r "libStaticLib2.a" ./lib2.o

Main Executable Project Build Command:

cpptestcc -line-coverage -compiler gcc_7-64 -- g++ -I"/home/mchalfant/Downloads/L3Demo/StaticLib2" -I"/home/mchalfant/Downloads/L3Demo/StaticLib1" -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"exec.d" -MT"exec.d" -o "exec.o" "../exec.cpp"

g++ -L"/home/mchalfant/Downloads/L3Demo/StaticLib2/Coverage" -L"/home/mchalfant/Downloads/L3Demo/StaticLib1/Coverage" -o "Executable" ./exec.o /home/mchalfant/C++testProf/10.4/engine/coverage/runtime/build/cpptest.o -lStaticLib2 -lStaticLib1

Once you have all of your project properties build your Project by selecting Project -> Build All to instrument your static libraries and the main executable project for coverage collection and archiving purposes. You should now see a .cpptest directory in each of the projects below are screenshots of the expected results.

Now we can Run Unit Tests on the main Executable project. As you can see from the screenshot before this doesn't provide coverage on any of the Static Libraries codebases even those were are using functions from those libraries.

We will need to archive the results of our Unit Test as we did within the last tutorial. However, some configuration is required to collect the coverage from multiple projects.

  1. Duplicate the default Run Unit Tests test configuration. Builtin -> Unit Testing -> Run Unit Tests
  2. Select the user-defined test configuration.
  3. Go to Execution -> Runtime
  4. Select Archive test and coverage results (experimental)
  5. Change the archive location to ${workspace_loc}/archive
  6. Rename the Test configuration to identify which Test Configuration will archive results.

It should look similar to the following

We want this to use the workspace location rather than the default project location as we used in the previous tutorial so that we archive the location of multiple projects' coverage into one location.

Now that we have the test configuration set up we can run this configuration on our Unit Tests in our Main Executable Project. You should see the following results.

The workspace that I am using currently has the Unit Test Results for both of my Static Libraries archived. I can load the archived coverage results from each of my Static Library projects and the recently archived Unit Tests results into my IDE.

Before loading our aggregated coverage results and findings to C++test we should verify that the archive directory that we have generated contains archive information from our tests. The archive should look similar to the following:

  1. Right-click on the Builtin -> Utilities -> Load Archived Results
  2. Select Duplicate.
  3. The Test configuration will now appear within the User-defined folder
  4. Go to Execution -> Runtime and enable the option detailed coverage report for
  5. Select Line Coverage as the value next to the option
  6. Change the location of the Archive to ${workspace_loc}/archive as this is the location where have placed our archived Coverage findings.

Select all of the projects within the workspace so that your coverage report will contain line coverage from the Main Executable project along with the associated static libraries.

Your Coverage view should look like the following:

To publish the report you can either click on proceed or the report and publish button at the top-right of the Test Configuration View.

Finally here is our report

We can see the aggregated coverage for all project reported along with a detailed Line Coverage Report to go into further depth on which lines were executed.

This concludes the main purpose of this tutorial. Below is provides some information about using Application Monitoring and Unit Testing to increase the overall coverage findings within your codebase.

OPTIONAL: Application Monitoring with Unit Testing Aggregated Coverage

In this section, we'll be using application monitoring on the main Executable project to get more overall coverage within the workspace.

IDE: First, we will need to create a new Run Configuration to that we can easily the run Executable from the IDE. You will need to be in the C/C++ perspective to configure your Run Configurations.

  • This can be done by going to Run -> Run Configurations -> C/C++ application. Right-click on the C/C++ Application
  • Can the name of you Run Configuration so you can quickly identify the Run Configuration
  • In the C/C++ application, select the location of the executable that you would like to run.
  • Select the appropriate project that the executable is associated with.

It should look somewhat similar to the following.

Command Line: you can quickly run the executable via command line to get the necessary cpptest_results.clog file as seen below.

Now that we have the .clog file created we can import the coverage into the C++test IDE. We can use the builtin test configuration Load Application Coverage to point to the correct .cpptest directory location and the correct .clog file location.

You should be able to see 100% coverage on within the Executable Project

If you import the findings from all archived results you should also receive 100% coverage on all projects

Detailed Line Coverage Report with both Application Monitoring and Unit Tests Coverage collected.

This concludes the tutorial. Happy Testing!

Sign In or Register to comment.