unittest and private methods

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Yin

    unittest and private methods

    I am an avid user of unittest and write tests for everything. Every so
    often, I write a class that has some private methods that I only want
    to be called from within the class. I usually write unittests for
    these private methods using the mangled form of the call from a test
    script file. It's not very elegant, and I wanted to get some feedback
    on how others write unit tests for private methods.

    I suppose that I could just not use private methods, and have them all
    be public, but it's a lot cleaner for me to hide all the other
    extraneous methods rather than expose them to the public.

    Thanks,
    Yin
  • Ben Finney

    #2
    Re: unittest and private methods

    On 2 Feb 2004 21:58:13 -0800, Yin wrote:[color=blue]
    > I am an avid user of unittest and write tests for everything. Every so
    > often, I write a class that has some private methods that I only want
    > to be called from within the class. I usually write unittests for
    > these private methods using the mangled form of the call from a test
    > script file. It's not very elegant, and I wanted to get some feedback
    > on how others write unit tests for private methods.[/color]

    If a method is private, why is it being tested from outside? Unit
    testing is supposed to test that the module *behaves* as expected. If a
    method is private, it's not part of the external behaviour of the
    module.

    Possibly you want to re-think what it is you're testing. Write a public
    method that *does something visible*, and test that. If you find that
    there's nothing the private method is doing that you want to test from
    outside, then why are you trying to test it?

    --
    \ "Laurie got offended that I used the word 'puke.' But to me, |
    `\ that's what her dinner tasted like." -- Jack Handey |
    _o__) |
    Ben Finney <http://bignose.squidly .org/>

    Comment

    • John Roth

      #3
      Re: unittest and private methods

      "Yin" <yin_12180@yaho o.com> wrote in message
      news:88caaeda.0 402022158.3a792 3ac@posting.goo gle.com...[color=blue]
      > I am an avid user of unittest and write tests for everything. Every so
      > often, I write a class that has some private methods that I only want
      > to be called from within the class. I usually write unittests for
      > these private methods using the mangled form of the call from a test
      > script file. It's not very elegant, and I wanted to get some feedback
      > on how others write unit tests for private methods.
      >
      > I suppose that I could just not use private methods, and have them all
      > be public, but it's a lot cleaner for me to hide all the other
      > extraneous methods rather than expose them to the public.[/color]

      Technically, the __ syntax that does name mangling is supposed
      to be for private methods, and the _ syntax is supposed to be for
      protected methods and attributes. However, I find that the
      distinction is not worth all that much, and use the single underscore
      for both. Possibly that's because I don't use subclassing all that
      much anyway so I don't find 'protected' to be a useful distinction.

      John Roth[color=blue]
      >
      > Thanks,
      > Yin[/color]


      Comment

      • Yin

        #4
        Re: unittest and private methods

        Ben Finney <bignose-hates-spam@and-benfinney-does-too.id.au> wrote in message news:<slrnc1ui5 e.151.bignose-hates-spam@rose.local domain.fake>...[color=blue]
        > On 2 Feb 2004 21:58:13 -0800, Yin wrote:[color=green]
        > > I am an avid user of unittest and write tests for everything. Every so
        > > often, I write a class that has some private methods that I only want
        > > to be called from within the class. I usually write unittests for
        > > these private methods using the mangled form of the call from a test
        > > script file. It's not very elegant, and I wanted to get some feedback
        > > on how others write unit tests for private methods.[/color]
        >
        > If a method is private, why is it being tested from outside? Unit
        > testing is supposed to test that the module *behaves* as expected. If a
        > method is private, it's not part of the external behaviour of the
        > module.
        >
        > Possibly you want to re-think what it is you're testing. Write a public
        > method that *does something visible*, and test that. If you find that
        > there's nothing the private method is doing that you want to test from
        > outside, then why are you trying to test it?[/color]

        That's a good point. The primary reason is that I'd like to be able to
        pinpoint errors quicker through unittesting. If I just provided a
        call to an exposed function, and an error occurred, it would still
        take some work to go in the class and pinpoint where exactly the
        problem is. By writing some unittests for the class internals, I may
        save myself some time in the future.

        Thanks for your thoughts!
        Yin

        Comment

        Working...