Re: Method of Unit Testing Private Methods

From: Eric Mutta (anon21h_at_yahoo.co.uk)
Date: 02/06/05

  • Next message: bjarne: "Re: Which new language to learn?"
    Date: 6 Feb 2005 12:06:13 -0800
    
    

    The Weeg wrote:
    > I'm relatively new to the concept of unit testing, and I've quickly
    run
    > into the problem of how to properly unit test private methods of a
    > class. [...]

    I follow the philosophy that, if it's private, then its none of my
    business whether it works correctly and consider the class to be
    functionally correct, if unit tests on its public members run
    successfully. In other words, I infer the correctness of private
    members, via the correctness of the public members that use those
    private members.

    Then when I *do* need access to the private members for some reason
    (mostly happens with private data members), I use conditional
    compilation to alter the visibility of the member:

      #If DEBUG Then
         Public Foo As Bar
      #Else
         Private Foo As Bar
      #End If

    or as follows for procedures:

      #If DEBUG Then
         Public Sub Foo()
      #Else
         Private Sub Foo()
      #End If

         'procedure body goes here

         End Sub

    This way, I can access the member in debug builds which run the unit
    tests, and keep it hidden in release builds meant for general use.
    Needless to say, I only use this method for the rare cases where I
    *must* have access to the private member in order to write any
    meaningful test. If overused, the conditional compilation code makes it
    all look, er, ugly.

    Further, I personally wouldn't use the method of inheritance or
    interfaces just to facilitate testing of private members. It smacks of
    OOP abuse, but YMMV.

    Cheers,
    Eric Mutta :-)


  • Next message: bjarne: "Re: Which new language to learn?"

    Relevant Pages

    • Re: Magazine article
      ... websites. ... intended for private circulation among members of the ... and supplying information privately to family members. ... family members aren't family historians, ...
      (soc.genealogy.britain)
    • Re: Closing parenthesis in functions definition followed by its call
      ... "properties of the constructor"? ... Java's notion of static members, ... This pattern of public, private, and privileged members is possible ... The pattern in question is the one for emulating private instance ...
      (comp.lang.javascript)
    • Re: (Probably) easy question about inheritance
      ... Typically a matrix uses a two-dimensional array, so it would be private ... I would like to access three data members: ... public inheritance is the same thing as inheritance in Java) ...
      (comp.lang.java.programmer)
    • Re: modular programming in Forth
      ... Foo \ Depends on Foo ... To prevent private members from being used outside the module. ... I feel this is a valid reason for the public/private distinction, ...
      (comp.lang.forth)
    • Re: private methods and OOD using XP
      ... > behind the public interface responsibilities defined in a Class Diagram. ... I believe access modifiers like private and public do ... one should unit test what is ... Now this is akin to saying Unit testing *is* Black box testing. ...
      (comp.object)