Another important step in specification-based tests is analyzing boundaries. Boundaries are a common place to have bugs in it.
But what is a boundary? Well, have you implemented some code that checked any interval, like for example dates between X (start date) and Y (end date)? When you were implementing you questioned whether are they both inclusive, or just X is inclusive and Y is exclusive, or X is exclusive and Y is inclusive, or they are both exclusive.
That’s an example of a boundary, which is a place where you can easily make a mistake by just putting a < where it should be a ≤.
Is easy to happen and sometimes is hard to see it. And sometimes we developers forget to clarify the proper behavior in this situation.
Example:
Let’s see in practice with the code that we were exploring in this series (the instruction 8XY4 add with carry from the CHIP-8 emulator):
// 8XY4
// ADD
int registerxIndex = ((instruction & 0x0F00) >> 8);
int registeryIndex = ((instruction & 0x00F0) >> 4);
int firstToSum = registers[registerxIndex];
int secondRegisterToSum = registers[registeryIndex];
int registersSum = (firstToSum + secondRegisterToSum);
registers[registerxIndex] = registersSum & 0xFF;
if (registersSum > 255) registers[registers.length - 1] = 1;
else registers[registers.length - 1] = 0;
pc += 2;
We can spot a clear case of boundary to explore, that is this condition:
if (registersSum > 255) registers[registers.length - 1] = 1;
else registers[registers.length - 1] = 0;
The rule is that if is bigger than 255 set the carry flag. But what happens when the value is exactly 255?
In this case, the code is fine (no bugs here). But it is a must to test this, and if you have any doubts about the behavior go ahead and clarify it.
To explore more this boundary we can follow these rules to test it:
- Test the exact boundary value:
- in this scenario, since we are looking at a sum, we can make that the sum of X and Y will be exactly 255.
- The boundary minus one:
- in this scenario, since we are looking at a sum, we can make that the sum of X and Y will be exactly 254.
- The boundary plus one:
- in this scenario, since we are looking at a sum, we can make that the sum of X and Y will be exactly 256.
For each boundary that you find in your code, follow the set of rules. Just by doing this, we found 3 important scenarios to explore. This will help you find more bugs or places to get clarified in your code.
Want to learn more about this topic?
If you didn’t read read the other articles of this series:
- Specification-Based Testing: A Developer’s Secret Weapon
- Specification-based Testing: Understand the requirements
- Specification-based Testing: Explore the program
- Specification-Based Testing: Exploring partitions
In the following days, I’ll dive into each of those steps in more detail, follow me, and do not miss my next blog posts of this series when it comes out.
In my next blog post, we are going to devise all the test cases based on the specification-based test steps. To see in practice how it works.
Stay tuned to learn more! Don't miss out!
Willian Moya (@WillianFMoya) / X (twitter.com)
Willian Ferreira Moya | LinkedIn
Follow me here on dev.to!
Top comments (0)