SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Guru
    Join Date
    Oct 2006
    Location
    Queensland, Australia
    Posts
    852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unit Testing Question

    Apologies for the generic title, but I couldn't really sum this up in a few words.

    When writing a Unit Test, the goal is obviously to test the public interface of a class, to ensure it behaves as expected. I have at least one test (method) for each method of a class, but I find when testing a particular method, I usually have to rely on another method of the same class to verify the correct behaviour of the method I'm testing. I generally try to avoid doing this, but often I don't have a choice as many of the classes properties are protected.

    What's the general stance on this? Should you avoid using other methods of the same class to verify the correct behaviour of the method your testing, or is it really not a problem?

    Here's an example of what I mean just in case the wording is a little confusing...

    PHP Code:
    function testRegisterMethod ()
    {
        
    $obj = new Example;
        
    $obj->register('test''example data');
        
    $this->assertTrue$obj->isRegistered('test') );

    As you can see in the example above, although I'm trying to test the register() method, I'm really testing both it, and the isRegistered() method. So a failure (or even a pass) of this test, really doesn't indicate what has exactly failed or succeeded.

  2. #2
    SitePoint Addict Mal Curtis's Avatar
    Join Date
    Jul 2009
    Location
    New Zealand
    Posts
    327
    Mentioned
    14 Post(s)
    Tagged
    0 Thread(s)
    If you check to see if the isRegistered works before you register then you can see if change is happening due to the register method.

    ie.
    if isregistered asserts true before the register -> problem
    If isregistered asserts false STILL after the register -> problem.

    Unfortunately that is only useful in simplish situations!

  3. #3
    SitePoint Guru
    Join Date
    Oct 2006
    Location
    Queensland, Australia
    Posts
    852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah that's true. Thanks for the tip. Speaking more generally though, is this generally accepted practice to rely on other methods of the same class for verifying behaviour of a particular method?

  4. #4
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    I would say so, you're testing this particular scenario, if either of these methods fail, then the scenario fails.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  5. #5
    SitePoint Guru
    Join Date
    Oct 2006
    Location
    Queensland, Australia
    Posts
    852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well yeah, that's what I was thinking, but wasn't sure if this was the norm for unit tests.

  6. #6
    SitePoint Addict Mal Curtis's Avatar
    Join Date
    Jul 2009
    Location
    New Zealand
    Posts
    327
    Mentioned
    14 Post(s)
    Tagged
    0 Thread(s)
    It should be fine - also if it were a more intensive setup / more complex you'd have other unit tests setup for either an individual method or that method amongst others. If it works in one, then a test which it's also in doesn't you can generally assume it's due to other methods.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •