Take the 2-minute tour ×

Let's say I have a class which has a private void method. And I want to test this method by a some seed parameters for each specific iteration.

For instance, I want to make StartTrhoughGridCells method below testable for its iterations so that I can check statte changes in parameterObject.

In such cases, how can I make a each iteration testable in a private method that has a loop inside?

public class Traveller

// ... Some memebers, properties

private void StartTrhoughGridCells(object parameterObject)
   // Do some state changes on parameterObject

share|improve this question
could you make it not void and just output the state changes int an int[n,numberofproperties] where n is the iteration number, and each column holds the state that was returned? –  soandos May 6 '11 at 11:05

1 Answer 1

Unit testing is black box testing, so conceptually, testing specific private methods should never be a goal in itself. The unit tests don't care about private methods and they don't know about their existence. Now, when it comes to code coverage you'd want to hit all of the code, but that's not quite the same...

When writing unit tests you only care about observable effects. Presumably the StartTrhoughGridCells method exists to produce some kind of observable effect, so you'll need to test that. However, from the question I can't tell what that observable effect would be, so you'll have to figure that out for yourself.

In other words you'd need to hit the method through whatever public API you have. If that's too hard to do, it's a sign that your API is too coarse-grained. In that case, consider whether some of the private methods can't be refactored to composable Strategies or similar.

share|improve this answer

Your Answer


