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.

This question already has an answer here:

I'm new in unit testing. For last two days I'm thinking how to test non-atomic methods with unit tests. When i need to write tests for some simple, atomic method situation in pretty easy - i'm taking all scenarios for this method that i can imagine, and reforge each of it in unit test.

But what if i have methods that are calling other methods? Should I test each scenarios for tested method AND each methods it is calling?

Maybe some example will be easier to understand:

    public int makeSomeOperation(int a, int b)
        if (a < 0)
            throw new ArgumentException("a must be greater that 0");
        if (b == 0)
            throw new ArgumentException("b cannot be 0");
        return a/b;

    public bool SomeNonatomicMethod(int number)
        if (number * number % 2 == 1)
            return false;
        int secondNumber = getNumberFromMordor();
        int result = makeSomeOperation(number, secondNumber);
        if (result == "123")
            return true;
        return false;

In this case, makeSomeOperation is easy to test - one test for each exception, one to check in returning result is equals with expectations.

But what about SomeNonatomicMethod? Did I need to write not only test for each case in this method (so: power of number is odd, result is equal 123, and result is diffrent), but also each possible flow of things that may happen in getNumberFromMordor and makeSomeOperation?

Things that may happen in makeSomeOperation are already tested, so are they should be tested second time?

share|improve this question

marked as duplicate by Kilian Foth, gnat, GlenH7, Alex Feinman, ChrisF Mar 26 '14 at 21:30

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

I believe the term you are looking for is "referentially transparent," rather than "atomic." –  Dan Mar 25 '14 at 16:39

2 Answers 2

up vote 12 down vote accepted

If getNumberFromMordor and makeSomeOperation are public, they should be tested by themselves, so your unit test should not doublecheck that getNumberFromMordor works correctly.

Only test what SomeNonatomicMethod does. Don't re-test public methods it relies on.

If getNumberFromMordor is private, you should test SomeNonatomicMethod against the possibility that getNumberFromMordor fails to work properly.

If SomeNonatomicMethod itself does so many things that you end up with dozens of assertions, it is a signal that it doesn't follow the single responsibility principle and should be broken down.

share|improve this answer

Usually you would not test the "makeSomeOperation" method. However, this is not a universal rule. Why? Because your composite method may not supply an adequate combination of parameters to execute all of "makeSomeOperation"'s code.

So, what you're really looking for is code coverage. When you write unit tests, and you've got a good code coverage tool, once you get to somewhere between 80% and 90% code coverage for a large library, you're doing pretty good. Ideally you'd get 100%, but that's almost impossible to maintain on a code base of any size. Maybe if the code you were writing was running in a medical device or something you'd try to maintain 100%, but for we mere mortals, we try to target and maintain 80% to 90% code coverage.

share|improve this answer

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