
Thanks: 0
Likes: 0
Dislikes: 0

Junior Member
How do I test for all possible input values?
This is a general unit testing development question, and a pretty basic one. I'm going to describe a method I have in my project for which I have written unit tests. The tests gets 100% coverage from the Visual Studio analyzer, but I'm not sure I'm done just because of that.
The method has 4 input parameters, lets call them A, B, C and D. There are a lot of if's, switches and eleses that depend on these parameters. The return type is a bool. So the method is structurally something like this
<font class="small">Code:</font><hr /><pre>public bool CanIDoSomething(int A, int B, int C, int D)
{
if (A ==1)
{
switch (B)
{
case 1:
if (C==2 && D> 3) {...}
case 2:
if (C==0  D < 2) {...}
}
}
else
{
return false;
}
}</pre><hr />
So far I've written 24 tests to cover all of the permutations in the if (A==1)block. My question is this:
How many more tests must I write to cover the else block of the same if?
One? 24? More than 24? The code NOW of course always returns false in this case, so Visual Studio Code Coverage is happy with one test, but this doesn't cover my back for any future changes, does it?
Example: I now have a test where I send in A=2 and assert for false. Somone might later change the elseblock to:
else { return A == 3; }. Now the logic has changed but all of the unit tests still pass.
I guess it's evident that I'm not very experienced with unit testing and that there must be some key basic principal that I'm missing [img]/images/graemlins/smile.gif[/img] Which one is it?
Nilzor

Member
Re: How do I test for all possible input values?
I'd say 1. For all A#1, it would go to the else block.

Junior Member
Re: How do I test for all possible input values?
Thank you for your reply, I'll probably go for 1 extra test, but I'm not convinced it's the only answer.

Member
Re: How do I test for all possible input values?
Hi Frode,
I think you might be missing something. This code won't/shouldn't compile because the only code path that will return a value is when A != 1.
You also need break statements in each case statement outside the brackets for each conditional.
So, let's assume we fix this to compile by putting in the break statements and a return true statement just outside the switch block e.g.
if (A == 1) {
switch(B) {
case 1:
...
break;
case 2:
...
break;
}
return true;
}
....
I have 8 tests:
A = 1, B = 1, C = 2, D > 3 (e.g 4 through 2,147483647)
A = 1, B = 1, C = 2, D =< 3 (e.g. 2147483648 through 3)
A = 1, B = 1, C != 2, D (any int value because we short circuit on C being false)
A = 1, B = 2, C = 0, D < 2
A = 1, B = 2, C = 0, D >= 2
A = 1, B = 2, C != 0, D (any int value because we short circuit on C being false)
A = 1, B any int other than 1 or 2
and of course
A != 1.
Would love to see the 24 tests you came up with.
Finally, if someone goes in and changes the else to "return A == 3;" then this method will still return false unless the arg value passed to the A param is 3 (or 1 which will cause control flow to enter the A == 1 block). Of course, that dev should probably never be allowed near a computer anymore.

Junior Member
Re: How do I test for all possible input values?
Hi with 6 test cases you can cover complete code:
Scenario:
A==1
{
B==1
{
C==2 and D >3
C==2 and D <=3
}
B==2
{
C==0
D <2
C <> 0 and D >=2
}
}
A<>1
Test cases:
1. A==1, B==1, C==2 and D >3
2. A==1, B==1, C==2 and D <3
3. A==1, B==2, C==0 and D >3
4. A==1, B==2, C<>0 and D <2
5. A==1, B==2, C<>0 and D >=2
6. A<>1, B==2, C<>0 and D <2
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules
