Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Suppose we're unit testing a class with xUnit-like framework - so a test class is created for class-under-test. Now, how much should be tested by individual test methods?

Should there be one-to-one mapping between methods-under-test and test methods:

class TestMath {

    testSqrt() {
        assertEquals(Math.sqrt(4), 2);

        assertException(Math.sqrt(-2));
    }
}

Or is it better to have one test method per "scenario":

class TestMath {

    testSqrtOfPositiveNum() {
        assertEquals(Math.sqrt(4), 2);
    }

    testSqrtOfNegativeNum() {
        assertException(Math.sqrt(-2));
    }
}

Which is better and what are pros and cons of both methods?

share|improve this question

closed as not constructive by Jim G., Martijn Pieters, Dynamic, MichaelT, MainMa Mar 20 '13 at 22:17

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.If this question can be reworded to fit the rules in the help center, please edit the question.

3 Answers 3

The second approach is better. If one of the tests fail, you will instantly know what the problem is. If your test methods check many conditions, if one of them fails, you will have to dig through it to figure out what the problem is.

share|improve this answer

Personally I prefer the scenario approach, it more closely resembles how the code will be used and is less likely to break just do to some change in the way the underlying code works.

share|improve this answer
2  
Also, keep in mind that putting multiple, dissimilar tests in one method (first approach) will not fully exercise your classes - as soon as one of them fails, the rest of the assertions in that test method do not even run. –  GalacticCowboy Mar 20 '13 at 20:51
    
It depends on test framework. Most behave this way, though. –  el.pescado Mar 20 '13 at 20:55
    
@el.pescado no with you first example if the test failed the -2 case is never executed, it will in your second example –  ratchet freak Mar 20 '13 at 22:14

The general rule is have one assert per test.

As far as the level to which you should test. I like to extract core logic into logic engines that can be tested using low level method level unit tests. But also have higher level tests that run at a scenario level. This way the high level tests speak the functionality of the whole system and the low level tests make sure that all the paths are tested correctly. When something breaks you can see where it is that it has broken but also how the break will manifest at a higher level.

In your case I would definitely split it out into two tests so that you can see exactly what has broken rather than just knowing that something has broken.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.