<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Do not test private methods</title>
	<atom:link href="http://lostechies.com/chadmyers/2008/11/21/do-not-test-private-methods/feed/" rel="self" type="application/rss+xml" />
	<link>http://lostechies.com/chadmyers/2008/11/21/do-not-test-private-methods/</link>
	<description>Software development, testing, and techie life</description>
	<lastBuildDate>Thu, 08 Mar 2012 22:19:00 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4.2</generator>
	<item>
		<title>By: Anonymous</title>
		<link>http://lostechies.com/chadmyers/2008/11/21/do-not-test-private-methods/#comment-1378</link>
		<dc:creator>Anonymous</dc:creator>
		<pubDate>Tue, 30 Aug 2011 12:18:00 +0000</pubDate>
		<guid isPermaLink="false">/blogs/chad_myers/archive/2008/11/21/do-not-test-private-methods.aspx#comment-1378</guid>
		<description>i truthfully enjoy your own writing kind, very remarkable,
don’t give up as well as keep writing due to the fact that it simply just worth to follow it. looking forward to see a whole lot more of your current well written articles, enjoy your day
&lt;a href=&quot;http://www.theseoportal.com&quot; rel=&quot;nofollow&quot;&gt;SEO Services Company&lt;/a&gt;</description>
		<content:encoded><![CDATA[<p>i truthfully enjoy your own writing kind, very remarkable,<br />
don’t give up as well as keep writing due to the fact that it simply just worth to follow it. looking forward to see a whole lot more of your current well written articles, enjoy your day<br />
<a href="http://www.theseoportal.com" rel="nofollow">SEO Services Company</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: chadmyers</title>
		<link>http://lostechies.com/chadmyers/2008/11/21/do-not-test-private-methods/#comment-787</link>
		<dc:creator>chadmyers</dc:creator>
		<pubDate>Fri, 09 Jan 2009 03:29:58 +0000</pubDate>
		<guid isPermaLink="false">/blogs/chad_myers/archive/2008/11/21/do-not-test-private-methods.aspx#comment-787</guid>
		<description>@Doug: Let me turn the question around on you:  Why must that task necessarily be private?

Frankly, I don&#039;t frequently find the need to make hardly anything private. Classes are small, singularly responsible, methods are small, and generally only a few methods per class. If I do need a private method, it&#039;s usually a small utility method of some sorts.

So your private methods are either a.) Used heavily by the public members in the class or b.) not used.

If a.) then they&#039;ll get tested when you test the public methods and b.) why are they there in the first place, let alone being tested?

And if your private methods are complex enough you really feel the need to test them separately, then you probably have a design smell or problem somewhere else you need to investigate.

This is the kind of stuff I like using TDD for, because it helps remind me when I&#039;m making mistakes. When I have lots of stuff in a private method, TDD gently, but firmly, reminds me where I went wrong and how to correct it.</description>
		<content:encoded><![CDATA[<p>@Doug: Let me turn the question around on you:  Why must that task necessarily be private?</p>
<p>Frankly, I don&#8217;t frequently find the need to make hardly anything private. Classes are small, singularly responsible, methods are small, and generally only a few methods per class. If I do need a private method, it&#8217;s usually a small utility method of some sorts.</p>
<p>So your private methods are either a.) Used heavily by the public members in the class or b.) not used.</p>
<p>If a.) then they&#8217;ll get tested when you test the public methods and b.) why are they there in the first place, let alone being tested?</p>
<p>And if your private methods are complex enough you really feel the need to test them separately, then you probably have a design smell or problem somewhere else you need to investigate.</p>
<p>This is the kind of stuff I like using TDD for, because it helps remind me when I&#8217;m making mistakes. When I have lots of stuff in a private method, TDD gently, but firmly, reminds me where I went wrong and how to correct it.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Doug Kent</title>
		<link>http://lostechies.com/chadmyers/2008/11/21/do-not-test-private-methods/#comment-786</link>
		<dc:creator>Doug Kent</dc:creator>
		<pubDate>Thu, 08 Jan 2009 22:28:29 +0000</pubDate>
		<guid isPermaLink="false">/blogs/chad_myers/archive/2008/11/21/do-not-test-private-methods.aspx#comment-786</guid>
		<description>I&#039;m not convinced that one should not test private methods.  The argument for that seems to be of the form that one should not test them because one should not test them, or, private methods cannot possibly represent a valid or respectable unit of testable code, because they can&#039;t.

Other than being not swayed by this, it seems absurd to me.  What is it about being &quot;private&quot; that necessarily makes a piece of code not worthy of a unit test being dedicated to it?  Of course a private method can accomplish a very useful and well encapsulated task pertinent only to a single class.  Why must that task necessarily be public?</description>
		<content:encoded><![CDATA[<p>I&#8217;m not convinced that one should not test private methods.  The argument for that seems to be of the form that one should not test them because one should not test them, or, private methods cannot possibly represent a valid or respectable unit of testable code, because they can&#8217;t.</p>
<p>Other than being not swayed by this, it seems absurd to me.  What is it about being &#8220;private&#8221; that necessarily makes a piece of code not worthy of a unit test being dedicated to it?  Of course a private method can accomplish a very useful and well encapsulated task pertinent only to a single class.  Why must that task necessarily be public?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mark Needham</title>
		<link>http://lostechies.com/chadmyers/2008/11/21/do-not-test-private-methods/#comment-785</link>
		<dc:creator>Mark Needham</dc:creator>
		<pubDate>Sat, 29 Nov 2008 01:55:22 +0000</pubDate>
		<guid isPermaLink="false">/blogs/chad_myers/archive/2008/11/21/do-not-test-private-methods.aspx#comment-785</guid>
		<description>You&#039;re right that you shouldn&#039;t test private methods although I&#039;ve sometimes found that when I&#039;ve made a class way too big and difficult to test that it can be effective to make a private method public temporarily and test it in there before I work out where I want to move it to. 

I&#039;m not sure whether or not that&#039;s a bad approach - certainly if I leave it in that state it&#039;s not good at all, but it helps to keep the momentum and I can then move the method to the right place when I&#039;ve finished working on the original class.

Mark</description>
		<content:encoded><![CDATA[<p>You&#8217;re right that you shouldn&#8217;t test private methods although I&#8217;ve sometimes found that when I&#8217;ve made a class way too big and difficult to test that it can be effective to make a private method public temporarily and test it in there before I work out where I want to move it to. </p>
<p>I&#8217;m not sure whether or not that&#8217;s a bad approach &#8211; certainly if I leave it in that state it&#8217;s not good at all, but it helps to keep the momentum and I can then move the method to the right place when I&#8217;ve finished working on the original class.</p>
<p>Mark</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: chadmyers</title>
		<link>http://lostechies.com/chadmyers/2008/11/21/do-not-test-private-methods/#comment-784</link>
		<dc:creator>chadmyers</dc:creator>
		<pubDate>Wed, 26 Nov 2008 14:14:40 +0000</pubDate>
		<guid isPermaLink="false">/blogs/chad_myers/archive/2008/11/21/do-not-test-private-methods.aspx#comment-784</guid>
		<description>@guy:

(1) &quot;not testing a class&quot;  -- I don&#039;t follow. Please elaborate.  In C#, it is not possible to write code without a class (for the most part, there are tricks), so I don&#039;t know how you could not test a class.  And I do this and it is TDD, so I don&#039;t know what you mean.

(2) &quot;testing derived classes only&quot; - Meaning: Test the public API first unless it gets repetitive and you start violating DRY, then consider testing the protected method directly</description>
		<content:encoded><![CDATA[<p>@guy:</p>
<p>(1) &#8220;not testing a class&#8221;  &#8212; I don&#8217;t follow. Please elaborate.  In C#, it is not possible to write code without a class (for the most part, there are tricks), so I don&#8217;t know how you could not test a class.  And I do this and it is TDD, so I don&#8217;t know what you mean.</p>
<p>(2) &#8220;testing derived classes only&#8221; &#8211; Meaning: Test the public API first unless it gets repetitive and you start violating DRY, then consider testing the protected method directly</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: guy kolbis</title>
		<link>http://lostechies.com/chadmyers/2008/11/21/do-not-test-private-methods/#comment-783</link>
		<dc:creator>guy kolbis</dc:creator>
		<pubDate>Wed, 26 Nov 2008 06:18:58 +0000</pubDate>
		<guid isPermaLink="false">/blogs/chad_myers/archive/2008/11/21/do-not-test-private-methods.aspx#comment-783</guid>
		<description>Hi Chad, 
I enjoy reading your comments, however I have some issue with some, first please read this:

http://blogs.microsoft.co.il/blogs/kolbis/archive/2008/11/08/my-opinion-on-mocking.aspx

This is my opinion on mocking.

B.W
1) not testing a class? this is not TDD...
2) testing derive classses only?

Thanks</description>
		<content:encoded><![CDATA[<p>Hi Chad,<br />
I enjoy reading your comments, however I have some issue with some, first please read this:</p>
<p><a href="http://blogs.microsoft.co.il/blogs/kolbis/archive/2008/11/08/my-opinion-on-mocking.aspx" rel="nofollow">http://blogs.microsoft.co.il/blogs/kolbis/archive/2008/11/08/my-opinion-on-mocking.aspx</a></p>
<p>This is my opinion on mocking.</p>
<p>B.W<br />
1) not testing a class? this is not TDD&#8230;<br />
2) testing derive classses only?</p>
<p>Thanks</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: chadmyers</title>
		<link>http://lostechies.com/chadmyers/2008/11/21/do-not-test-private-methods/#comment-782</link>
		<dc:creator>chadmyers</dc:creator>
		<pubDate>Wed, 26 Nov 2008 00:05:44 +0000</pubDate>
		<guid isPermaLink="false">/blogs/chad_myers/archive/2008/11/21/do-not-test-private-methods.aspx#comment-782</guid>
		<description>@mbratton

Protected methods are protected methods for a reason (i.e. you&#039;re exposing them to client code -- whether your own client code or the wide, wide world&#039;s client code).

So, I would give the following advice:

If it is 100% your code on both sides (framework/infrastructure code and client code) and the code in this project or other projects under your control (i.e. not code that will be used by a wide audience), I would concentrate on testing the classes that derive from the class-with-protected-members and you will essentially be testing the protected method(s) in question.

Having said that, if you find your tests repeating themselves for each sub-class, perhaps it would be better to have a test subclass that exposes the protected functionality and test that instead (to avoid duplication/repetition).

If this is a public framework where anyone and everyone is going to derive from your class and use this protected method, I would use the test subclass or a PartialMock (most mocking frameworks can do this -- see Rhino Mocks, Moq, TypeMock, etc).

</description>
		<content:encoded><![CDATA[<p>@mbratton</p>
<p>Protected methods are protected methods for a reason (i.e. you&#8217;re exposing them to client code &#8212; whether your own client code or the wide, wide world&#8217;s client code).</p>
<p>So, I would give the following advice:</p>
<p>If it is 100% your code on both sides (framework/infrastructure code and client code) and the code in this project or other projects under your control (i.e. not code that will be used by a wide audience), I would concentrate on testing the classes that derive from the class-with-protected-members and you will essentially be testing the protected method(s) in question.</p>
<p>Having said that, if you find your tests repeating themselves for each sub-class, perhaps it would be better to have a test subclass that exposes the protected functionality and test that instead (to avoid duplication/repetition).</p>
<p>If this is a public framework where anyone and everyone is going to derive from your class and use this protected method, I would use the test subclass or a PartialMock (most mocking frameworks can do this &#8212; see Rhino Mocks, Moq, TypeMock, etc).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: mbratton</title>
		<link>http://lostechies.com/chadmyers/2008/11/21/do-not-test-private-methods/#comment-781</link>
		<dc:creator>mbratton</dc:creator>
		<pubDate>Mon, 24 Nov 2008 17:03:00 +0000</pubDate>
		<guid isPermaLink="false">/blogs/chad_myers/archive/2008/11/21/do-not-test-private-methods.aspx#comment-781</guid>
		<description>What about protected methods?</description>
		<content:encoded><![CDATA[<p>What about protected methods?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: chadmyers</title>
		<link>http://lostechies.com/chadmyers/2008/11/21/do-not-test-private-methods/#comment-780</link>
		<dc:creator>chadmyers</dc:creator>
		<pubDate>Mon, 24 Nov 2008 14:24:43 +0000</pubDate>
		<guid isPermaLink="false">/blogs/chad_myers/archive/2008/11/21/do-not-test-private-methods.aspx#comment-780</guid>
		<description>@guy:

Microsoft&#039;s opinion is in favor of testing private methods. In fact, they seem to encourage it with, like you said, Private Accessors and various How To articles on MSDN.

I wouldn&#039;t say that this is just my opinion, it is generally a common-held opinion in the unit testing and, specifically, TDD circles. I won&#039;t take credit for this thought, but I will say that I do hold this opinion (e.g &quot;Do not test private methods&quot;).

I have written several large-scale enterprise apps and have not ever needed to test a private method.  I&#039;m saying this to brag or anything, I&#039;m saying it to prove a point that it IS possible and SHOULD be a virtue worth striving for.

Having said all this, I am not prepared to say that testing private methods is ALWAYS wrong, but it&#039;s &quot;ALMOST ALWAYS&quot; wrong. There may be a few circumstances where it is beneficial or worth the risk of brittle tests to do this.

This is why I called it a code &quot;stench&quot;.  It&#039;s worse than a smell, but it&#039;s not universally BAD.

RE: Internal methods

I feel mostly the same about this one, but it&#039;s not quite as dangerous as private methods, so I would tone down a the warning a little.  Generally, I manage to find a way to not have any internal methods also.

Internal methods usually denote direct coupling between concrete classes versus coupling through abstractions (public interfaces).  This creates a more brittle framework and prevents client code from overriding functionality if it needs to.

By using the Dependency Inversion Principle and Interface Segregation Principle, along with an IoC container, I seem to never need the &#039;internal&#039; keyword and very rarely need the &#039;private&#039; keyword, let alone tests for the same.</description>
		<content:encoded><![CDATA[<p>@guy:</p>
<p>Microsoft&#8217;s opinion is in favor of testing private methods. In fact, they seem to encourage it with, like you said, Private Accessors and various How To articles on MSDN.</p>
<p>I wouldn&#8217;t say that this is just my opinion, it is generally a common-held opinion in the unit testing and, specifically, TDD circles. I won&#8217;t take credit for this thought, but I will say that I do hold this opinion (e.g &#8220;Do not test private methods&#8221;).</p>
<p>I have written several large-scale enterprise apps and have not ever needed to test a private method.  I&#8217;m saying this to brag or anything, I&#8217;m saying it to prove a point that it IS possible and SHOULD be a virtue worth striving for.</p>
<p>Having said all this, I am not prepared to say that testing private methods is ALWAYS wrong, but it&#8217;s &#8220;ALMOST ALWAYS&#8221; wrong. There may be a few circumstances where it is beneficial or worth the risk of brittle tests to do this.</p>
<p>This is why I called it a code &#8220;stench&#8221;.  It&#8217;s worse than a smell, but it&#8217;s not universally BAD.</p>
<p>RE: Internal methods</p>
<p>I feel mostly the same about this one, but it&#8217;s not quite as dangerous as private methods, so I would tone down a the warning a little.  Generally, I manage to find a way to not have any internal methods also.</p>
<p>Internal methods usually denote direct coupling between concrete classes versus coupling through abstractions (public interfaces).  This creates a more brittle framework and prevents client code from overriding functionality if it needs to.</p>
<p>By using the Dependency Inversion Principle and Interface Segregation Principle, along with an IoC container, I seem to never need the &#8216;internal&#8217; keyword and very rarely need the &#8216;private&#8217; keyword, let alone tests for the same.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: bogardj</title>
		<link>http://lostechies.com/chadmyers/2008/11/21/do-not-test-private-methods/#comment-779</link>
		<dc:creator>bogardj</dc:creator>
		<pubDate>Mon, 24 Nov 2008 12:14:44 +0000</pubDate>
		<guid isPermaLink="false">/blogs/chad_myers/archive/2008/11/21/do-not-test-private-methods.aspx#comment-779</guid>
		<description>@guy

the general rule of thumb is only test public visible behavior.  If you feel you need to test something private, then your class probably has too much responsibility.

The other piece to note is that private methods are generally only testable through reflection, which uses strings, which is brittle.  InternalsVisibleTo attribute is a means around this issue, which I had to use quite a bit when part of our shipped product was a public API.</description>
		<content:encoded><![CDATA[<p>@guy</p>
<p>the general rule of thumb is only test public visible behavior.  If you feel you need to test something private, then your class probably has too much responsibility.</p>
<p>The other piece to note is that private methods are generally only testable through reflection, which uses strings, which is brittle.  InternalsVisibleTo attribute is a means around this issue, which I had to use quite a bit when part of our shipped product was a public API.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
