Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - VS 2010 Unit Testing for private methods

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

like image 754
MammothOne Avatar asked Aug 04 '11 15:08

MammothOne


2 Answers

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.

like image 71
vc 74 Avatar answered Nov 19 '22 18:11

vc 74


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.

like image 1
Dmitry Avatar answered Nov 19 '22 19:11

Dmitry