Using branch coverage to drive test cases #148
Replies: 1 comment
-
Another interesting observation is that different branches of the code will use different subsets of the data points passed into the function. For example looking at this logic: if a = 1 then
if b = 2 then
"foo"
else
"bar"
else
if c = 3 then
"baz"
else
"bat" This logic has 4 branches, 2 of those uses
So at this point we might say that it shouldn't matter what you put in the empty cells so we'll just let the system replace it with any random value. But does that provide full coverage? Let's assume that we are using this to ensure that we came up with this logic based on an existing system but we made a mistake in the process and missed a condition: if a = 1 then
if b = 2 then
"foo"
else
-- we missed this before
if c = 4 then
"crash"
else
"bar"
else
if c = 3 then
"baz"
else
"bat" Will our test cases catch this? Not unless we happen to accidentally pass in 4 as the random number for This is because our interpretation of an empty cell was wrong. It doesn't mean "any value will do", it means "every possible value should yield the same result". Technically we cannot generate all the possible inputs, but the tooling could apply property-based testing to approximate it. With that we would get better coverage than full branch coverage. Branch coverage makes sure that every decision that the logic makes is right and this extension is trying to ensure that there are no decisions that the logic is missing. |
Beta Was this translation helpful? Give feedback.
-
Problem Statement
Making sure that your business logic is correct is one of the main focus areas of the Morphir tooling. At the lowest level we have compile -time checks which ensure that your logic handles every possible value that can be passed into it. We also have to make sure that it's doing what's expected. We already have support for defining test cases, saving them in a JSON and then running them against the logic either interactively on the UI or as part of the CI. But how do you know if you have enough test cases to know that the logic is always right?
Analysis
To be absolutely sure you would have to have a test case for every possible input which is not feasible in most cases since the number of possible inputs is large or even infinite (assuming arbitrary precision). But even functions with an infinite number of possible inputs make a finite number of decisions in the logic, so it is possible to check every possible combination of decisions a piece of logic could make. This is where the concept of Cyclomatic Complexity and Branch Coverage comes in:
These metrics are very useful by themselves and we can build even more on top of the core functionality that underpins them: identifying all the branches of a piece of business logic. Let me lay all those out in user story form.
User Stories
Beta Was this translation helpful? Give feedback.
All reactions