Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to unit test a class with one entry point?

So I'm looking for some advice on how test a class I've written.

It intentionally consists of one public method Run() which accepts varaibles and sets of a long running process. This method consists of numerous calls to private methods that perform specific tasks but shouldn't be public accessible.

My query is, in this case, it seems logical to unit tests all of my private methods. However lots of people suggest that testing private methods is a bad practice.

So my question is should I just go ahead and test the private methods or has anybody been in a similar situation and can suggest an alternative method? There is absolutely no way I can reliably test all of my private methods through the one public method.

Any help appreciated and apologies if my question isn't the clearest. The application is written in C# if thats of any help.

Thanks

like image 658
mat-mcloughlin Avatar asked Dec 07 '10 11:12

mat-mcloughlin


1 Answers

Personally I take a pragmatic point of view for this sort of thing - that it's okay to make unit tests "white box" tests (i.e. you can take implementation details into account and test them) but don't go so far down that route that they end up being hugely brittle.

I'd make the private methods internal and test them via InternalsVisibleTo. Also run some "black box" tests via just the Run method... it sounds like these are really integration tests more than unit tests.

Another thing to consider is whether this really should be one class. Are the tasks separable? Would they make sense as individual classes themselves, with the current class just coordinating them?

like image 129
Jon Skeet Avatar answered Sep 27 '22 16:09

Jon Skeet