Thanks:  0
Likes:  0
Dislikes:  0

# Thread: How do I test for all possible input values?

1. ## 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 &amp;&amp; D&gt; 3) {...}
case 2:
if (C==0 || D &lt; 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 else-block 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

2. ## 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.

3. ## 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.

4. ## 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 &gt; 3 (e.g 4 through 2,147483647)
A = 1, B = 1, C = 2, D =&lt; 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 &lt; 2
A = 1, B = 2, C = 0, D &gt;= 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.

5. ## 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 &gt;3
C==2 and D &lt;=3
}
B==2
{
C==0
D &lt;2
C &lt;&gt; 0 and D &gt;=2
}
}
A&lt;&gt;1

Test cases:

1. A==1, B==1, C==2 and D &gt;3
2. A==1, B==1, C==2 and D &lt;3
3. A==1, B==2, C==0 and D &gt;3
4. A==1, B==2, C&lt;&gt;0 and D &lt;2
5. A==1, B==2, C&lt;&gt;0 and D &gt;=2
6. A&lt;&gt;1, B==2, C&lt;&gt;0 and D &lt;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
•
Search Engine Optimisation provided by DragonByte SEO v2.0.36 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.