Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

6
  • 7
    I've never found that argument for testing private methods compelling. You refactor code into methods all the time for reasons that have nothing to do with the public API of a class, and it's a very nice thing to be able to test those methods independently, without involving any other code. That is the reason you refactored, right? To get a piece of independent functionality. That functionality should be testable independently as well. Commented Dec 12, 2013 at 16:47
  • 1
    @RobertHarvey answer expanded with a rant addressing this. "Private methods are beneficial to unit testing..." Commented Dec 13, 2013 at 7:54
  • I see what you're saying about private methods and code coverage, but I wasn't really advocating making those methods public so that you can test them. I was advocating having a way to test them independently even though they are private. You can still have your public tests that touch the private method, if you want to. And I can have my private tests. Commented Dec 13, 2013 at 16:06
  • @RobertHarvey I see. Guess it boils down to coding style. At the amount of private methods I typically produce, and at the rate I refactor these, having tests for them is simply a luxury I can't afford. Non-private API is another matter, I tend to be quite reluctant and slow at modifying it Commented Dec 13, 2013 at 16:14
  • Perhaps you're better at writing methods that work the first time than I am. For me, I need to test the method to make sure that it works first before I can move on, and having to test it indirectly by firing off a series of other methods would be clumsy and awkward. Commented Dec 13, 2013 at 16:16