Examine Branch Test Coverage (Activity)

From Foss2Serve
Jump to: navigation, search


Branch Test Coverage


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

Learning Objectives After successfully completing this activity, the learner should be able to:
  • Examine the branch coverage of unit tests on a simple C++ class to discover and fix errors.
Process Skills Practiced



  • 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.


ACM Body of Knowledge
Area & Unit(s)

Software Engineering / SE Software Verification Validation

ACM Topic(s)

Unit Testing, Test Coverage, Branch Coverage

Level of Difficulty
Estimated Completion Time
  • 20-30 minutes to comprehend the branch coverage results, identify the error it exposes, and correct the solution.
  • 15-20 minutes to discuss.
Environment / Materials
  • Must have completed [Unit Test With GoogleTest Activity] installation process

Kevin Buffardi


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

CC license.png

Personal tools
Learning Resources
HFOSS Projects