Examine Branch Test Coverage (Activity)

From Foss2Serve
Revision as of 17:46, 7 August 2016 by Dee.weikle (Talk | contribs)
Jump to: navigation, search

Branch Test Coverage


Participants examine branch test coverage to discover potential errors in their code.

Prerequisite Knowledge

GoogleTest needed to have been installed (as instructed in Unit Test With GoogleTest Activity), and intermediate C++ and git expertise is needed.

Learning Objectives

Participants should examine the branch coverage of unit tests on a simple C++ class to discover and fix errors.



  • Pause when most students have reached the intermission (marked below) and explain the gcov results to demonstrate how branch coverage exposes more than line coverage


  • This exercise continues from Introduction to Test Driven Development
  • Fork and clone the upstream repository of Minefield.
  • Get the updates from the (upstream) Minefield and put it into a new branch called branchcov by using the following commands:

git remote add upstream https://github.com/ChicoState/minefield.git
git checkout -b branchcov upstream/master
This creates the new branch by pulling from the upstream master branch and checks out the new branch locally

  • Look over the unit tests for the revealAdjacent() function and see what cases have been handled. Review the function within Field.cpp if you want to see how the solution is implemented.
  • Next, make the project and run the tests using make test. Do the tests pass? How confident are you in the solution?
  • Let's look at how comprehensively we've tested the function. Run the command: gcov -b Field.cpp. Note that the -b option is short for: --branch-probabilities. Within the results you should find:

File 'Field.cpp'
Lines executed:
Branches executed:
Taken at least once:
No calls
Field.cpp:creating 'Field.cpp.gcov'

  • This summarizes the whole file, but we want to concentrate specifically on revealAdjacent() so open up Field.cpp.gcov in your editor. Find that function and look at the gcov analysis of it. You should see some lines that look like:

branch 0 taken 99%
What does the branch number correspond to? What do you think "taken" percentage refers to?


  • After we have refactored the code, you have some updates to retrieve so go to your branchcov branch and fetch from upstream
  • Now that you have a better understanding of how to read the results of gcov's branch coverage, continue testing until you have tested all the branches for revealAdjacent(). For this exercise, you may disregard the branches in the constructor. Upload your Field.cpp.gcov to complete this assignment.


Revised code in a C++ program to resolve an error.


Students should look through branch coverage results to see where the logic does not work correctly for all possible cases and correct the problem.


Additional Information:

Knowledge Area/Knowledge Unit

Software Engineering


Unit Testing, Test Coverage, Branch Coverage

Level of Difficulty

Intermediate expertise of C++ and imperative programming

Estimated Time to Completion

Comprehending the branch coverage results, identifying the error it exposes, and correcting the solution should take between 20-30 minutes. Discussion should take 15-20 minutes for a total of up to 50 minutes.


Need to have completed [Unit Test With GoogleTest Activity] installation process


Kevin Buffardi


Kevin Buffardi


Licensed CC BY-SA 4.0

This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License

CC license.png

Personal tools
Learning Resources
HFOSS Projects