Are you explicitly unit testing a private method when you use your knowledge of the private method to choose test cases -


it seems though general consensus of testing community not test private methods. instead, should test private methods testing public methods invoke them. however, doesn't feel right me. let's take method example:

/**  * returns base name of output generator class. if class named  * reno_outputgenerator_html, return "html".  *  * @return string  */ protected function getname() {     $class = get_class($this);     $matches = array();      if (preg_match('/^reno_outputgenerator_(.+)$', $class, $matches))     {         return $matches[1];     }     else     {         throw new reno_outputgenerator_exception('class name must follow format of reno_outputgenerator_<name>.');     } } 

this particular function used in couple of places in class. i'd test both branches of if statement in function, mean each public function i'd have test 2 situations plus whatever else public method does.

this feels weird me. if i'm testing see if getname() throws exception when specific condition met, means have know implementation details of private method. if have know that, why shouldn't extend class, make method public, , test way?

(btw: if you're wondering why such weird method exists, used automagically figure out directory class's template files stored in).

the way understand unit testing, kind of testing want do. have looked @ unit testing white-box testing; if there's branch point in code, means need 2 unit tests address it. think worst case ever wound single method 32 permutations.

the challenge unit-testing if don't explore edge cases examining code , figuring out different paths, wind missing 1 or more cases , possibly introducing subtle bugs application.

so, no, don't see you're proposing weird. method can stay internal, , can add test case - need 1 exception, right?

alternatively, refactor functionality separate object takes generator object , returns name (based on algorithm above). justify separating tests, because you'd have name-extractor object, , output generator implementations. i'm still not sure save lot, because you'd still have test output generators make sure using name extractor correctly, separate functional , testing concerns.


Comments

Popular posts from this blog

unicode - Are email addresses allowed to contain non-alphanumeric characters? -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

c++ - Convert big endian to little endian when reading from a binary file -