Unit testing multiple conditions in an IF statement

Posted by bwalk2895 on Programmers See other posts from Programmers or by bwalk2895
Published on 2012-07-06T19:17:42Z Indexed on 2012/07/06 21:24 UTC
Read the original article Hit count: 188

Filed under:

I have a chunk of code that looks something like this:

function bool PassesBusinessRules()
{
    bool meetsBusinessRules = false;

    if (PassesBusinessRule1 
         && PassesBusinessRule2
         && PassesBusinessRule3)
    {
         meetsBusinessRules= true;
    }

    return meetsBusinessRules;
}

I believe there should be four unit tests for this particular function. Three to test each of the conditions in the if statement and ensure it returns false. And another test that makes sure the function returns true.

Question: Should there actually be ten unit tests instead? Nine that checks each of the possible failure paths. IE:

  • False False False
  • False False True
  • False True False

And so on for each possible combination.

I think that is overkill, but some of the other members on my team do not. The way I look at it is if BusinessRule1 fails then it should always return false, it doesn't matter if it was checked first or last.

© Programmers or respective owner

Related posts about unit-testing