Re-reading Code Complete 2 recently, I came across the concept of Structured Basis Testing which despite its foreboding name is in fact an easy to understand and straight forward method that you can use to calculate the minimum number of unit test required to exercise every path through a function or procedure.
Structured Basis testing differs from Code Coverage testing or Logic Coverage because Structured Basis testing gives you the minimum number of test cases you need to exercise every path whereas when using Code Coverage testing or Logic Coverage testing you could end up with many more test cases than you would need to cover the same logic with Structured Basis Testing.
You calculate the minimum number of tests required by following these rules:
- Start with 1 for the straight path through the function or procedure
- Add 1 for each keyword or decision construct such as if, while, for, do, and, or,
- Add 1 for each case in a case statement.
In this first example, there are 2 unit tests required to exercise every path through the function. Number 1 is the straight path through the function, i.e a record is found by the select statement. Number 2 is the path taken when a record is not found.
In the second example there are 5 unit tests required to test every path through this procedure. Number 1 is again the straight path through the procedure. Numbers 2 – 5 are each of the cases in the case statement.
In this final example, there are four unit tests required to test every path through this procedure. The first is the straight path through the routine, the bulk collect will not raise an exception if no records are found so there are no divergent paths at this point, the next unit test, number 2 exercises the for loop and then numbers 3 and 4 exercise the IF statement paths.
In this article I have explained what Structured Basis Testing is, how it differs from other code coverage testing and how to calculate the minimal number of unit tests required to exercise every path in your function or procedure.
The idea for this post comes from the discussion on Structured Basis Testing in Code Complete 2 Pages 505 – 506
One thought on “Structured Basis Testing”