5

VS 2010 allows for private method unit testing. Is that a good idea? I have always heard that unit testing scenarios were for the public methods only. Should I bother with the private methods and properties?

Thanks Leo

4 Answers 4

5

In his infinite wisdom, Jon Skeet once wrote (in C# in depth) 'I’m happy to test whatever I can in the simplest manner possible'

IMHO, if you think a private method deserves unit testing then test it.

Sign up to request clarification or add additional context in comments.

1 Comment

I agree. Specifically, I find that a lot of private static methods of mine are good candidates for unit testing private members. It's especially useful if I have narrow-focused methods that I really don't want to haul off to some new "helper" class.
1

Two reasons to not test private methods:

1) Brittle tests. Private method is an implementation detail that you might want to change it in future without breaking tests.

2) Duplication. Code in private methods should be covered by the tests that exercise object using its public interface. If this is the case than you would simply be testing the same thing twice.

1 Comment

surely you want to see what breaks, why and how if you are changing a private method?
0

Although it would be nice to test all of your private methods/properties too, what really counts are your public methods/properties (i.e. what you expose to the outside world). Ultimately, your private methods/properties will be used somehow by your public methods/properties.

If you want to test your private methods/properties on their own, then test them. But since they will not be used directly (and instead, indirectly), I don't feel it is absolutely necessary.

Comments

0

As it has been said, test it if you feel the need and you are able to, but be aware that a unit test of a private method is more likely to be broken if someone modifies it, which can become a great pain in the ass in a big project with a lot of unit tests, my advice would be, test only those methods whose functionality are very clear and more unlikely to be changed.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.