Examine Branch Test Coverage (Activity)
Title |
Branch Test Coverage |
Overview |
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. |
Directions:
Instructor:
- 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
Students:
- 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 withinField.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 upField.cpp.gcov
in your editor. Find that function and look at the gcov analysis of it. You should see some lines that look like:
What does the branch number correspond to? What do you think "taken" percentage refers to?
branch 0 taken 99%
STOP HERE AND WAIT FOR DISCUSSION
- 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 yourField.cpp.gcov
to complete this assignment.
Deliverables:
Revised code in a C++ program to resolve an error.
Assessment:
Students should look through branch coverage results to see where the logic does not work correctly for all possible cases and correct the problem.
Comments:
Additional Information:
Knowledge Area/Knowledge Unit |
Software Engineering / SE Software Verification Validation |
Topic |
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. |
Materials/Environment |
Need to have completed [Unit Test With GoogleTest Activity] installation process |
Author |
Kevin Buffardi |
Source |
Kevin Buffardi |
License |
Licensed CC BY-SA 4.0 |
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License