When writing tests for some new functionality, our goal should be to write the minimum number of tests with the highest likelihood of finding defects. To do this, we need a repeatable, consistent approach to defining our tests that will result in a similar outcome and quality of tests across the development team.
Thankfully, lightweight tools and strategies already exist to provide this structured approach. A good starting point is to combine these three different analysis techniques: 1) basis testing, 2) data-flow testing, and 3) error guessing.
Executing this analysis — in this order — provides insight into what needs to be tested and an optimized test plan for your unit tests. Steve McConnell goes into great detail on these three test analysis approaches and other testing considerations and strategies in chapter 22 of his book “Code Complete.”
In this three-part series, I will go into more detail about each of these approaches. I will also include an example so you have a basic understanding of the concepts and a starting point for creating a disciplined approach to automated test design.
Let’s start with basis testing.
Basis testing is the simplest method of testing. The goal of basis testing is to test each statement in your program at least once using a minimal set of test cases. The analysis has two steps:
- Calculate the minimum number of paths through the program
- Develop the minimum number of test cases that will exercise every path
This form of testing will catch a surprising number of errors, even if the approach is quite simple. The code below is an example analysis for a method program that is used to calculate employee bonuses. Four paths are identified. Once we have identified the paths, we can start to create our test matrix by creating an entry for each path identified.
Below is our test matrix with the four tests entered representing the four paths identified. The matrix includes additional information related to the conditions we will need to test: 1) whether the employee is full-time, 2) what their salary is, and 3) whether they are eligible for a bonus.
Once we have completed our structured basis testing analysis, we move on to data-flow testing analysis. I will cover that in my next post.