One thing that bothers me while I'm writing unit tests of my code is if tests of methods should include validation of the outcome of other methods? Of course, public methods. Let's see a - rather trivial - example:

public int Solve(string forumla)
    if (forumla.Contains("+")) 
        return SolveAddition(formula);
    if (forumla.Contains("-"))
        return SolveSubstraction(formula);
    throw new ArgumentException();

public int SolveAddition(string formula)
    var strings = formula.Split('+');
    if (strings.Length != 2)
        throw new ArgumentException();
    int firstFactor = int.Parse(strings[0]);
    int secondFactor = int.Parse(strings[0]);
    return firstFactor + secondFactor;

In this case, should tests of Solve methods only test control flow, or each possible scenario of SolveAddition? Let's assume, that SolveAddition and SolveSubstraction have their own tests.

Using other words, did test scenarios for Solve should contains only:

  • 2+2
  • 2-2
  • 2:2

Or also include:

  • 2+2+2+2
  • 2-2-2-2
  • potato - hamster

Best regards


No, a unit test should test the thing it's targetting and nothing else. You're allowed to assume that any other method that this method calls does the right thing, because they must have their own unit tests.

If one of those helper methods were wrong, you'd get an alert when that other test is run, and since you're supposed to run the entire test suite and only push when the entire suite succeeds, testing the same thing a second time is just a waste of time.


For any given piece of code, there should be at most one place that is responsible for exhaustively testing that code.

There can be any number that incidentally test it; you just don't want to waste effort running up an exponentially large mountain.

For your example, the minimum set of test cases is a list of invalid forumulas, plus a list of valid formulas with expected answers. Those lists don't get any shorter overall if you split out addition and subtraction cases.

So the right place to test all three functions would be through 'Solve'.

That way, when you decide you need to handle '2 + 3 - 1', you just move it from one list to the other.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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