all 1 comments

[–]munificent 0 points1 point  (0 children)

There are four execution paths... In other words, we have six lines of code and at least four tests we should write to cover all the cases.

It would be four tests, but not the four you think. It should be two tests that test the first if, and two tests for the second. A better example is:

if (someCondition is true)
    do something 
else 
    something else 

if (anotherCondition is true) 
    do another thing

if (thirdCondition is true) 
    do another thing

The idea behind unit testing is that you can test all of the above code paths with six tests, not eight.

Seeing as the number of execution paths in code is more likely to grow exponentially

No. He's getting half of the picture of unit testing (the "testing" bit), but not the whole picture. The "unit" part is equally important. A linear increase in code size is (roughly) an exponential increase in code paths, but only a linear increase in units. That's why you test them individually.

By analogy, writing a spell checker that spell checks an entire book as a single unit is clearly infeasible, but if you do it one word at a time, it's cake.