Gcov For Mac

What is CppUTest.

CppUTest is a C /C++ based unit xUnit test framework for unit testing and for test-driving your code. It is written in C++ but is used in C and C++ projects and frequently used in embedded systems but it works for any C/C++ project.

Display help about using gcov (on the standard output), and exit without doing any further processing. Output gcov file in an easy-to-parse JSON intermediate format which does not require source code for generation. The JSON file is compressed with gzip compression algorithm and the files have.gcov.json.gz extension.

  • Browse other questions tagged linker undefined-reference gcov or ask your own question. The Overflow Blog Podcast 293: Connecting apps, data, and the cloud with Apollo GraphQL CEO.
  • Using gcov for C/C Code Coverage¶ This is about how to run your C/C code using gcov to gather coverage metrics. These instructions have been tested on Mac OS X 10.9 (Mavericks). They may or may not work on other OS’s.
  • However, I found the way to get this to work. I have presented the steps that work with the sample project below. The steps work for Linux/Mac. Similar steps would work even on Windows once you have installed lcov. On Mac, you can install lcov using Mac Ports. Go to the Debug folder of the sample project.
  • In this post i have taken a sample C program and will be generating the code coverage stats for the same using gcov and lcov. Here is my sample C program link. Its pretty simple menu driven program that does simple mathematical operations like addition, subtraction, multiplication and division depending on users choice.

CppUTest’s core design principles are:

  • Simple in design and simple in use.
  • Portable to old and new platforms.
  • Build with Test-driven Development for Test-driven Developers.

Setting up CppUTest

There are several ways to setup CppUTest. One is to install via package management and the other is from source. The big difference is that from source you can use MakefileWorker.mk. MakefileWorker is not supported pre-packaged. MakefileWorker does not require you to know a lot about make and makefiles to get started.

An easy way to get your first test case running is to use James Grenning’s cpputest-starter-project for gcc or cpputest-starter-project for Visual Studio. James is the author of Test-Driven Development for Embedded C. You’ll find instructions, your first test case, and some other example code. James’ training resources use MakefileWorker, so you need to install from source.

Adding tests to untested C and C++ can be a big challenge. You might find Get your Legacy C into a Test Harness a useful recipe and resource. The page includes links to numerous articles of real legacy C challenges.

Pre-packaged install

Linux

There is a Debian and an Ubuntu package available for CppUTest. This is by far the easiest way to install it, via:

MacOSX

For Mac, a Homebrew package is available too. You can install via:

From source install

You can download the latest ‘automatically released’ version:

  • Latest version passing the buildThis version is automatically packages after a build has passed.

Alternatively, you can clone the github repository, read-only:

Or clone it via ssh (which requires a github account)

CppUTest can also be added to your git repo as a git submodule.

Now that you have CppUTest sources, you can build it with your favorite build tool (CMake or autoconf).

Building with autoconf requires you to (this requires you to have installed GNU autotools, apt-get/brew install automake autoconf libtool):

You can use make install if you want to install CppUTest system-wide

NOTE: Building from cpputest_build means you will not be able to use MakefileWorker.mk. To use MakefileWorker you need to build from the cpputest home directory.

Using CppUTest with MakefileWorker.mk and gcc

If you want to use CppUTest’s MakefileWorker, you cannot currently get CppUTest using the “Pre-packaged” options described above. Instead you can get CppUTest from source using the options already described.

Change to the top level directory of CppUTest (the directory containing include/ and src/ among other files)

You will want to add **export CPPUTEST_HOME=** somewhere like **.bashrc** or in your build script as a relative path.

Using CppUTest with Visual Studio

You can build CppUTest using cmake or in the Visual Studio IDE.

from Visual Studio IDE

Depending on your VS version double click either

  • CppUTest_VS201x.sln - for VS 2010 and later
  • CppUTest.sln - for pre VS 2010

Say yes to suggested conversions. Select the menu item corresponding to run without debugging. CppUTest should build (probably with warnings). When the build completes the test runner runs. You should see over 1000 tests passing and no test failures. The build also produced a static library (cpputest/lib) holding CppUTest you can link your tests to.

To use CppUTest, define an environment variable CPPUTEST_HOME that points to the home directory of CppUTest. You will find a working example and some more help in cpputest-starter-project for Visual Studio.

How to create a coverage report

You can use autoconf to create a coverage report for CppUTests own tests. If you have lcov installed, a browsable html report will be generated as well. After the steps outlined in the previous paragraph, do the following:

This will generate a file called gcov_report.txt with the coverage report in plain text format. It will also generate an HTML file called gcov_report.txt.html. If you have lcov installed, you will be able to browse the lcov report by opening ./cpputest_build/test_coverage/index.html . The lcov report is by far the easiest way to inspect CppUTest’s own test coverage.

For

Alternatively, you can use CMake if that is the build tool you fancy (this requires you have install CMake, apt-get install cmake):

For Windows users, the above will work with cygwin. There are also several MS VC++ projects available.

Where to find more information

  • If you have any questions, check out the Google Groups
  • The source is at the main github page
  • You can report bugs or features at the issues page
  • You can follow CppUTest on twitter

Quick introduction (some code!)

To write your first test, all you need is a new cpp file with a TEST_GROUP and a TEST, like:

This test will fail.

You can add new tests to the test group by just writing more tests in the file, like this:

You do need to create a main where you run all the unit tests. Such a main will look like this:

For more information, We’d recommend reading the manual or, even better, check some existing tests such as SimpleStringTest or (a bit more complicated) MemoryLeakDetectorTest or the mocking tests or just check out the Cheat Sheet

Related projects

For Eclipse users, also check:

  • CppUTest Eclipse Test Runner This will allow you to run your tests JUnit style with red and green bars, and rerun arbitrary selections of tests.Prerequisites:
    • CppUTest off master
    • Eclipse Juno, Kepler, or later
    • Eclipse C/C++ Unit Plugin (if not already present in your version of Eclipse; install directly from Eclipse Help -> Install New Software…).

Authors and Contributors

CppUTest has had many contributions from its users. We can’t remember all, but we appreciate it a lot! Much of the original code was written by Michael Feathers (based on CppUnit Lite). The current main maintainers are @jwgrenning and @basvodde

NOTE:

Gcov for c++

Between mid October 2019 and mid February 2020 everyone in the Army was migrated to use their PIV Authentication certificate for Email access. You no longer use the Email certificate for Enterprise Email or any CAC enabled websites

Mac users who choose to upgrade (or already have upgraded) to Mac OS Catalina (10.15.x) will need to uninstall all 3rd Party CAC enablers per https://militarycac.com/macuninstall.htm AND reenable the built in smart card ability (very bottom of macuninstall link above)

If you purchased your Mac with OS Catalina (10.15.x) already installed, you can skip the uninstall part above and follow the instructions below.

Mac

6 'high level' steps needed, follow down the page to make this a painless systematic process

1.Is your CAC reader 'Mac friendly'?
2.Can your Mac 'see' the reader?
3.Verify which version of Mac OS you have
4.Figure out which CAC (ID card) you have
5.Install the DoD certificates
5a.Additional DoD certificate installation instructions for Firefox users
6.Decide which CAC enabler you want to use (except for 10.12-.15 & 11)

Step 1: Is your CAC reader Mac friendly?

Visit the USB Readers page to verify the CAC reader you have is Mac friendly.

Visit the USB-C Readers page to verify the CAC reader you have is Mac friendly.

'Some, not all' CAC readers may need to have a driver installed to make it work.

NOTE: Readers such as: SCR-331 & SCR-3500A may need a firmware update (NO OTHER Readers need firmware updates).

Information about these specific readers are in Step 2

Step 2: Can your Mac 'see' the reader?

Plug the CAC reader into an open USB port before proceeding, give it a few moments to install

Step 2a: Click the Apple Icon in the upper left corner of the desktop, select 'About This Mac'

Step 2b: Click 'System Report...' (button)

Step 2c: Verify the CAC reader shows in Hardware, USB, under USB Device Tree. Different readers will show differently, most readers have no problem in this step. See Step 2c1 for specific reader issues.

Step 2c1: Verify firmware version on your SCR-331 or GSR-202, 202V, 203 CAC, or SCR-3500a reader. If you have a reader other than these 5, Proceed directly to step 3

Step 2c1a-SCR-331 reader

If your reader does not look like this, go to the next step.

In the 'Hardware' drop down, click 'USB.' On the right side of the screen under 'USB Device Tree' the window will display all hardware plugged into the USB ports on your Mac. Look for “SCRx31 USB Smart Card Reader.” If the Smart Card reader is present, look at 'Version' in the lower right corner of this box: If you have a number below 5.25, you need to update your firmware to 5.25. If you are already at 5.25, your reader is installed on your system, and no further hardware changes are required. You can now Quit System Profiler and continue to Step 3.

Step 2c1b-SCR-3500A reader

If you have the SCR3500A P/N:905430-1 CAC reader,you may need to install this driver, as the one that installs automatically will not work on most Macs. Hold the control key [on your keyboard] when clicking the .pkg file [with your mouse], select [the word] Open

Step 3: Verify which version of MacOS do you have?

(You need to know this information for step 6)

Step 3a: Click the Apple Icon in the upper left corner of your desktop and select 'About This Mac'

Step 3b: Look below Mac OS X for: Example: Version 10.X.X.

Step 4: Figure out which CAC (ID Card) you have

(You need to know this information for step 6)

Look at the top back of your ID card for these card types. If you have any version other than the six shown below, you need to visit an ID card office and have it replaced. All CACs [other than these six] were supposed to be replaced prior to 1 October 2012.

Find out how to flip card over video

Step 5: Install the DoD certificates (for Safari and Chrome Users)

Go to Keychain Access

Click: Go (top of screen), Utilities, double click Keychain Access.app

(You can also type: keychain access using Spotlight (this is my preferred method))

Select login (under Keychains),and All Items (under Category).

Download the 5 files via links below (you may need to <ctrl> click, select Download Linked File As... on each link) Save to your downloads folder

Please know... IF You have any DoD certificates already located in your keychain access, you will need to delete them prior to running the AllCerts.p7b file below.

https://militarycac.com/maccerts/AllCerts.p7b,

https://militarycac.com/maccerts/RootCert2.cer,

https://militarycac.com/maccerts/RootCert3.cer,

https://militarycac.com/maccerts/RootCert4.cer, and

Double click each of the files to install certificates into the login section of keychain

Select the Kind column, verify the arrow is pointing up, scroll down to certificate, look for all of the following certificates:

DOD EMAIL CA-33 through DOD EMAIL CA-34,

DOD EMAIL CA-39 through DOD EMAIL CA-44,

DOD EMAIL CA-49 through DOD EMAIL CA-52,

DOD EMAIL CA-59,

DOD ID CA-33 through DOD ID CA-34,

DOD ID CA-39 through DOD ID CA-44,

DOD ID CA-49 through DOD ID CA-52,

DOD ID CA-59

DOD ID SW CA-35 through DOD ID SW CA-38,

DOD ID SW CA-45 through DOD ID SW CA-48,

DoD Root CA 2 through DoD Root CA 5,

DOD SW CA-53 through DOD SW CA-58, and

DOD SW CA-60 through DOD SW CA-61

NOTE: If you are missing any of the above certificates, you have 2 choices,

1. Delete all of them, and re-run the 5 files above, or

2. Download the allcerts.zip file and install each of the certificates you are missing individually.

Errors:

Error 100001 Solution

Error 100013 Solution

You may notice some of the certificates will have a red circle with a white X . This means your computer does not trust those certificates

You need to manually trust the DoD Root CA 2, 3, 4, & 5 certificates

Double click each of the DoD Root CA certificates, select the triangle next to Trust, in the When using this certificate: select Always Trust, repeat until all 4 do not have the red circle with a white X.

You may be prompted to enter computer password when you close the window

Once you select Always Trust, your icon will have a light blue circle with a white + on it.

The 'bad certs' that have caused problems for Windows users may show up in the keychain access section on some Macs. These need to be deleted / moved to trash.

Gcov

The DoD Root CA 2 & 3 you are removing has a light blue frame, leave the yellow frame version. The icons may or may not have a red circle with the white x

or DoD Interoperability Root CA 1 or CA 2 certificate
DoD Root CA 2 or 3 (light blue frame ONLY) certificate
or Federal Bridge CA 2016 or 2013 certificate
or Federal Common Policy CAcertificate
or or SHA-1 Federal Root CA G2 certificate
or US DoD CCEB Interoperability Root CA 1 certificate

If you have tried accessing CAC enabled sites prior to following these instructions, please go through this page before proceeding

Clearing the keychain (opens a new page)

Please come back to this page to continue installation instructions.

Step 5a: DoD certificate installation instructions for Firefox users

NOTE: Firefox will not work on Catalina (10.15.x), or last 4 versions of Mac OS if using the native Apple smartcard ability

Download AllCerts.zip, [remember where you save it].

double click the allcerts.zip file (it'll automatically extract into a new folder)

Option 1 to install the certificates (semi automated):

From inside the AllCerts extracted folder, select all of the certificates

<control> click (or Right click) the selected certificates, select Open With, Other...

In the Enable (selection box), change to All Applications

Select Firefox, then Open

You will see several dozen browser tabs open up, let it open as many as it wants..

Gcov For Macbook Air

You will eventually start seeing either of the 2 messages shown next

If the certificate is not already in Firefox, a window will pop up stating 'You have been asked to trust a new Certificate Authority (CA).'

Check all three boxes to allow the certificate to: identify websites, identify email users, and identify software developers

or

'Alert This certificate is already installed as a certificate authority.' Click OK

Once you've added all of the certificates...
• Click Firefox (word) (upper left of your screen)
• Preferences
• Advanced (tab)
• Press Network under the Advanced Tab
• In the Cached Web Content section, click Clear Now (button).
• Quit Firefox and restart it

Option 2 to install the certificates (very tedious manual):

Click Firefox (word) (upper left of your screen)

Preferences

Advanced (tab on left side of screen)

Certificates (tab)

View Certificates (button)

Authorities (tab)

Import (button)

Browse to the DoD certificates (AllCerts) extracted folder you downloaded and extracted above.

Gcov For Mac

Note: You have to do this step for every single certificate

Note2: If the certificate is already in Firefox, a window will pop up stating: 'Alert This certificate is already installed as a certificate authority (CA).' Click OK

Note3: If the certificate is not already in Firefox, a window will pop up stating 'You have been asked to trust a new Certificate Authority (CA).'

Check all three boxes to allow the certificate to: identify websites, identify email users, and identify software developers

Once you've added all of the certificates...
• Click Firefox (word) (upper left of your screen)
• Preferences
• Advanced (tab)
• Press Network under the Advanced Tab
• In the Cached Web Content section, click Clear Now (button).
• Quit Firefox and restart it

Step 6: Decide which CAC enabler you can / want to use

Only for Mac El Capitan (10.11.x or older)

Install Lcov Mac

After installing the CAC enabler, restart the computer and go to a CAC enabled website

NOTE: Mac OS Sierra (10.12.x), High Sierra (10.13.x), Mojave (10.14.x) or Catalina (10.15.x) computers no longer need a CAC Enabler.

Try to access the CAC enabled site you need to access now

Gcov Format

Mac support provided by: Michael Danberry