
Nilzor
Newbie
Reged: 07/23/10
Posts: 2


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
Code:
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; } }
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 Which one is it?
Nilzor
Edited by Nilzor (07/23/10 04:47 AM)

FakeSoftwareTester
Member
Reged: 06/29/10
Posts: 51


I'd say 1. For all A#1, it would go to the else block.
  Fake Software Tester
(Not a secret identity, but a chosen mask!!!)
http://fakesoftwaretester.blogspot.com

Nilzor
Newbie
Reged: 07/23/10
Posts: 2


Thank you for your reply, I'll probably go for 1 extra test, but I'm not convinced it's the only answer.

TestingMentor
Member
Reged: 12/28/06
Posts: 235
Loc: Seattle, Washington


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.
  Bj 
I.M. Testy blog
Testing Mentor

gkgana123
Junior Member
Reged: 08/25/05
Posts: 10
Loc: bangalore


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


0 registered and 4 anonymous users are browsing this forum.
Moderator: AJ, Jeanj
Print Topic

Forum Permissions
You cannot start new topics
You cannot reply to topics
HTML is disabled
UBBCode is enabled

Rating:
Topic views: 3641




Powered by UBB.threads™ 6.5.5
