PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Sun Sep 23, 2018 5:50 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Testing Private Methods
PostPosted: Sat Aug 06, 2005 6:23 pm 
Offline
DevNet Master
User avatar

Joined: Mon Oct 25, 2004 9:29 pm
Posts: 3698
Location: New Jersey, US
I did a quick google search and noticed that there's a long standing debate on this. I have several questions:

* What are your views on testing private methods and,
* How would you test methods explicitly declared private in PHP5?
* Maybe you should be a bit more discerning before making things private?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 06, 2005 6:37 pm 
Offline
DevNet Resident
User avatar

Joined: Fri Aug 16, 2002 8:57 am
Posts: 1834
Location: Watertown, MA
Personally, I wouldn't test private (or protected), methods. Thats an implementation detail. Which I don't think should be enforced via testing. You want to test that the class's public interface meets its spec. So, the private methods should be tested indirectly via testing the public method's interface.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 06, 2005 9:58 pm 
Offline
DevNet Master

Joined: Thu Jan 30, 2003 9:26 pm
Posts: 2893
Location: Glasgow, Scotland
I haven't heard the argument for but it sounds like a very bad idea.

When you refactor, the interface might remain constant while the internals are shuffled around. Testing private methods would interfere with that (ie the test case would have to be edited to keep pace rather than acting as a constant touch stone to guide the refactoring).

I think testing should focus on interfaces. That helps to improve design - encapsulation is what OOP is all about. Testing private methods has got to be evil.

I'm trying to think why anyone would want to. A programmer might have missed the smell of a new responsibility and a need to spawn another class. If methods are rather bloated or there are lots of them you might find it harder to get a fine enough grain against the interface alone. Testing private methods could maybe help to debug something which isn't obvious otherwise. I don't know really. I expect they'll pay for their sins though.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 06, 2005 10:29 pm 
Offline
DevNet Master
User avatar

Joined: Mon Oct 25, 2004 9:29 pm
Posts: 3698
Location: New Jersey, US
Thank's for your replies. Here's the google search I did: http://www.google.com/search?q=unit%20t ... e%20method and for some strange reason the first two results are anti-private method testing and the second two results are pro-private method testing. I think I'll go no private method testing. :)


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 06, 2005 11:05 pm 
Offline
DevNet Master

Joined: Thu Jan 30, 2003 9:26 pm
Posts: 2893
Location: Glasgow, Scotland
I had a quick look at one of the links which recommends testing private methods.

The example he gives mentions a sorting algorithm going awry but the failing interface assertions aren't giving enough fine detail to find the problem quickly. That's exactly what I was getting at above: if the algorithm is complex enough for this to be an issue, it probably should be refactored into a new object, with its own fine-grained tests.

Testing smells can be a sign that the design needs a rethink.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: Google [Bot] and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group