tags:

views:

330

answers:

3

When writing test cases which are supposed to have 100% branch coverage, is it ok to have one of your cases that covers two branches and another case that only covers one.

note: we are assuming there are only three branches in the code.

edit: 3 branches means three basic if statments that are all seperate to each other within a body of code. e.g.

input (x, y)
if (x<0)
    something
if (x==y)
    something
if (x > y)
    something
output (x)

I have one test case that covers the first branch and one test case that covers the other two branches

A: 

100% branch coverage? Does the person asking for something like that have any real world test coverage experience? In my experience, for reasonably complex projects, obtaining 75-80% code coverage and around 60-70% branch coverage is the best one can hope for. These numbers are usually the raw, pre-analys, numbers. They go up (~92-95% code and 80-85% branch) after the snippets impossible to reach are eliminated, like Asserts, default switch cases, 'defense in depth' code paths and such. As for your question: the less test cases you have the better. Don't forget that tests take time not only to develop, but to run and analyze failures too. After you waited your first time you wait 4 days for the whole test suite to finish, you quickly learn the value of reducing your number of test cases to the minimum that gives confidence in the coverage.

Remus Rusanu
So basically, the less cases the better, I understood everything you said was a valid point but it doesn't apply to this situation as it's not 'real world'. It's a a theoretical question that was thrown my way. I can't answer with "100% coverage!!! you're crazy" -_-
My answer was a strictly practical one. In the Real World time to develop and run tests does matter and practice quickly shows that the exponential rate explosion of the test matrix cannot be outrun. If the discussion is academic, then someone can win the argument with 'but it does not cover all the possible cases', you say 'thank you for your valuable input' and move on...
Remus Rusanu
+1  A: 

Personally, I focus on the behaviour of the code. As such, there are three different possible ways for the code to be executed as such there should be three tests.

Think of it this way - if one of the two branches breaks, you might be unaware because the test still passes (as the other branch still works) but production code fails. Not ideal.

Yes it does take more time, but in some cases it is worth it... 100% for everything? Maybe not to that level of extremes.

Ben Hall
Good point, thanks.
Then again, couldn't the test fails if one of the branches fails? and the test coudl record which branch failed?
It could, but your test is containing additional logic which might hide the true intention of what it is covering.
Ben Hall
A: 

A test is a question that we have about the product. The idea of one test per (written) branch might be useful or silly.

I have some questions about the example you give

input (x, y)
if (x<0)
something
if (x==y)
something
if (x > y)
something
output (x)

What happens if x is greater than zero? Are you supposed to fall through? What's supposed to happen if x is less than y? Something? Nothing?

Here's the thing: code (and branch) (and condition) coverage are nice ideas. But what does it mean to "cover" a line or a branch or a condition? Is it to make sure that the program can work--that is, execute a given line/branch/condition without crashing? Or is to to make sure that that program will work?

---Michael B.

Michael Bolton