Examine Branch Test Coverage (Activity)
Branch Test Coverage
Participants examine branch test coverage to discover potential errors in their code.
GoogleTest needed to have been installed (as instructed in Unit Test With GoogleTest Activity), and intermediate C++ and git expertise is needed.
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.cppif 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
-boption is short for:
--branch-probabilities. Within the results you should find:
Taken at least once:
- This summarizes the whole file, but we want to concentrate specifically on
revealAdjacent()so open up
Field.cpp.gcovin 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
branchcovbranch 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.gcovto 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.
|Knowledge Area/Knowledge Unit||
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
Licensed CC BY-SA 4.0
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License