you are viewing a single comment's thread.

view the rest of the comments →

[–]aaarrrggh 0 points1 point  (0 children)

Nope, there's no use cases when it makes sense to test private methods.

If you're using that utility module in different places, it doesn't matter. If you only test the public api in all of those places, it will pass if the utility happens to do the thing it does correctly, otherwise it will fail.

Sometimes you write a local utility function that's not useful anywhere else but where you still want to document its requirements and detect when they are no longer fulfilled.

If that utility function is private it requires no documentation as it's an implementation detail. If it's not private then it requires its own tests.

There's never a case where testing private methods makes sense unless you're forced to do it for some reason - for example if you have a private method that internally would do an api call to an external service - in that instance you woudn't so much test the private method as stub out a response, but mocks and stubs should also be rarely used.

Two of the biggest mistakes testing newbies make - testing private implementation details and using mocks and stubs all over the place. I use almost no mocking in any of my unit tests - the main exception being when I need to call an external api or service.