Click here to Skip to main content
15,913,610 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: I like unit testing and debugging private methods so u$ sucks today Pin
Marc Clifton6-Oct-14 15:18
mvaMarc Clifton6-Oct-14 15:18 
GeneralRe: I like unit testing and debugging private methods so u$ sucks today Pin
_Maxxx_6-Oct-14 15:47
professional_Maxxx_6-Oct-14 15:47 
GeneralRe: I like unit testing and debugging private methods so u$ sucks today Pin
Marc Clifton6-Oct-14 16:21
mvaMarc Clifton6-Oct-14 16:21 
GeneralRe: I like unit testing and debugging private methods so u$ sucks today Pin
_Maxxx_6-Oct-14 18:09
professional_Maxxx_6-Oct-14 18:09 
GeneralRe: I like unit testing and debugging private methods so u$ sucks today Pin
Super Lloyd6-Oct-14 18:07
Super Lloyd6-Oct-14 18:07 
GeneralRe: I like unit testing and debugging private methods so u$ sucks today Pin
Marc Clifton7-Oct-14 2:26
mvaMarc Clifton7-Oct-14 2:26 
GeneralRe: I like unit testing and debugging private methods so u$ sucks today Pin
Nicholas Marty6-Oct-14 23:59
professionalNicholas Marty6-Oct-14 23:59 
GeneralRe: I like unit testing and debugging private methods so u$ sucks today Pin
peterchen7-Oct-14 0:32
peterchen7-Oct-14 0:32 
Hmm.... grcchh... I have to disagree - not necessarily with the result, but with the reasoning.

Now there are a lot of assumptions going into how the public interface is implemented. Yet, at the general level, the core responsibility of a unit test is to verify observable behavior of a functional unit.

This seems pretty straightforward to me, since

  • Other components relying on it will do so through the public interface.
  • The public interface resembles more or less closely the specification of the functional unit.
  • A significant element of the observable bahavior that requires testing is preserving invariants - something your privates usually don't do


So the question is not should we test public or private, but is testing the public interface sufficient?.

I can imagine quite some scenarios where this is not:

  • You are not exposing sufficient state to test for invariants.
    In some cases that's simply missing from the interface (e.g. a SetCapacity without GetCapacity), but I concede there are cases where this state wouldn't need to be public except for tests.
  • Performance guarantees like "amortized O(1)" are hard to test for.
    I've worked around this in some cases by e.g. exposing "cache stats" or "performance stats" in the public interface (which tend to end up in the final app diagnostic anyway). But again, I concede there's not always a reasonable solution.
  • You've built a complex interface ("the car") entirely of private elements ("the engine" etc.) that are not individually testable.
    As much as I detest the very idea of Test Driven Design (*shudder*), I would concede that automated testability is a relevant stake in the architecture, so - unless there's unjustifiable burden on other stakes - "the engine" should be testable as well.
  • While having a public interface, the sub component is private in its entirety, thus the interface is not available form the outside for testing.





My own conclusion is: the public interface is the primary interface to be tested. If tests require access to privates, at least consider if the public interface is complete and/or a sub component should be isolated for testability. Then, remember my personal motto of "Test what is easy to test, you won't catch everything anyway". After that, there's little privates to be tested left, anyway, which is sufficiently rare to make test-only functionality accessable within the public signature.

RantPHP PinPopular
Marc Clifton6-Oct-14 9:48
mvaMarc Clifton6-Oct-14 9:48 
GeneralRe: PHP Pin
CDP18026-Oct-14 9:54
CDP18026-Oct-14 9:54 
GeneralRe: PHP Pin
Afzaal Ahmad Zeeshan6-Oct-14 9:58
professionalAfzaal Ahmad Zeeshan6-Oct-14 9:58 
GeneralRe: PHP Pin
Marc Clifton6-Oct-14 10:13
mvaMarc Clifton6-Oct-14 10:13 
GeneralRe: PHP Pin
Jeremy Falcon6-Oct-14 11:24
professionalJeremy Falcon6-Oct-14 11:24 
GeneralRe: PHP Pin
Chris Maunder6-Oct-14 10:07
cofounderChris Maunder6-Oct-14 10:07 
GeneralRe: PHP Pin
Marc Clifton6-Oct-14 10:16
mvaMarc Clifton6-Oct-14 10:16 
GeneralRe: PHP PinPopular
Roger Wright6-Oct-14 10:13
professionalRoger Wright6-Oct-14 10:13 
GeneralRe: PHP Pin
CDP18026-Oct-14 10:16
CDP18026-Oct-14 10:16 
GeneralRe: PHP Pin
Jeremy Falcon6-Oct-14 10:32
professionalJeremy Falcon6-Oct-14 10:32 
GeneralRe: PHP Pin
Jeremy Falcon6-Oct-14 10:24
professionalJeremy Falcon6-Oct-14 10:24 
GeneralRe: PHP Pin
Slacker0076-Oct-14 10:29
professionalSlacker0076-Oct-14 10:29 
GeneralRe: PHP Pin
Jeremy Falcon6-Oct-14 10:31
professionalJeremy Falcon6-Oct-14 10:31 
GeneralRe: PHP Pin
Slacker0076-Oct-14 10:36
professionalSlacker0076-Oct-14 10:36 
GeneralRe: PHP Pin
Jeremy Falcon6-Oct-14 10:48
professionalJeremy Falcon6-Oct-14 10:48 
GeneralRe: PHP Pin
Marc Clifton6-Oct-14 10:33
mvaMarc Clifton6-Oct-14 10:33 
GeneralRe: PHP Pin
Jeremy Falcon6-Oct-14 10:38
professionalJeremy Falcon6-Oct-14 10:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.