<?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"
	>
<channel>
	<title>Comments on: Testing Is a Legitimate Use Case</title>
	<atom:link href="http://blogs.teamb.com/craigstuntz/2009/01/12/37919/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.teamb.com/craigstuntz/2009/01/12/37919/</link>
	<description>C# • Delphi • Entity Framework • Functional Programming • InterBase • MVC • .NET • Web</description>
	<pubDate>Wed, 17 Mar 2010 23:08:04 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.2</generator>
		<item>
		<title>By: Craig Stuntz</title>
		<link>http://blogs.teamb.com/craigstuntz/2009/01/12/37919/#comment-4400</link>
		<dc:creator>Craig Stuntz</dc:creator>
		<pubDate>Wed, 28 Jan 2009 12:58:09 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.teamb.com/craigstuntz/?p=37919#comment-4400</guid>
		<description>&lt;i&gt;That’s not true. Of course you can have non-RT methods where you know you have non-executing code paths.&lt;/i&gt;

Huh? That is irrelevant to what I actually wrote.

&lt;i&gt;My experience of it was that is was pretty pointless for true TDD - after all, there is no code for it to generate tests for, until after you’ve already written the tests, right?&lt;/i&gt;

Have you tried its assertions?</description>
		<content:encoded><![CDATA[<p><i>That’s not true. Of course you can have non-RT methods where you know you have non-executing code paths.</i></p>
<p>Huh? That is irrelevant to what I actually wrote.</p>
<p><i>My experience of it was that is was pretty pointless for true TDD - after all, there is no code for it to generate tests for, until after you’ve already written the tests, right?</i></p>
<p>Have you tried its assertions?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jim Cooper</title>
		<link>http://blogs.teamb.com/craigstuntz/2009/01/12/37919/#comment-4399</link>
		<dc:creator>Jim Cooper</dc:creator>
		<pubDate>Wed, 28 Jan 2009 04:15:24 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.teamb.com/craigstuntz/?p=37919#comment-4399</guid>
		<description>&#62; It is really useful for TDD, though, since it can 
&#62; find cases you might have missed, with nothing 
&#62; more than a prototype.

This statement seems to imply something unusual about your understanding of TDD to me. 

My experience of it was that is was pretty pointless for true TDD - after all, there is no code for it to generate tests for, until after you've already written the tests, right?

You could run it against your code later, as some sort of sanity check, but I didn't find that a useful exercise.

For existing non-unit tested code (which is approximately all the code ever written) it comes into its own, although I frequently found a need to delete irrelevant tests in my trials.</description>
		<content:encoded><![CDATA[<p>&gt; It is really useful for TDD, though, since it can<br />
&gt; find cases you might have missed, with nothing<br />
&gt; more than a prototype.</p>
<p>This statement seems to imply something unusual about your understanding of TDD to me. </p>
<p>My experience of it was that is was pretty pointless for true TDD - after all, there is no code for it to generate tests for, until after you&#8217;ve already written the tests, right?</p>
<p>You could run it against your code later, as some sort of sanity check, but I didn&#8217;t find that a useful exercise.</p>
<p>For existing non-unit tested code (which is approximately all the code ever written) it comes into its own, although I frequently found a need to delete irrelevant tests in my trials.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jim Cooper</title>
		<link>http://blogs.teamb.com/craigstuntz/2009/01/12/37919/#comment-4398</link>
		<dc:creator>Jim Cooper</dc:creator>
		<pubDate>Wed, 28 Jan 2009 03:20:27 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.teamb.com/craigstuntz/?p=37919#comment-4398</guid>
		<description>&#62; Once more: Unless the methods under test are 
&#62; truly referentially transparent, you can’t know 
&#62; this.

That's not true. Of course you can have non-RT methods where you know you have non-executing code paths.</description>
		<content:encoded><![CDATA[<p>&gt; Once more: Unless the methods under test are<br />
&gt; truly referentially transparent, you can’t know<br />
&gt; this.</p>
<p>That&#8217;s not true. Of course you can have non-RT methods where you know you have non-executing code paths.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Craig Stuntz&#8217;s Weblog : SQLDA missing or incorrect version, or incorrect number/type of variables</title>
		<link>http://blogs.teamb.com/craigstuntz/2009/01/12/37919/#comment-4351</link>
		<dc:creator>Craig Stuntz&#8217;s Weblog : SQLDA missing or incorrect version, or incorrect number/type of variables</dc:creator>
		<pubDate>Thu, 22 Jan 2009 05:23:22 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.teamb.com/craigstuntz/?p=37919#comment-4351</guid>
		<description>[...] the applications and provides testing and diagnostic information at runtime. I said before that testing is a valid use case, and this is one example. Another, report-related example is that whenever our applications [...]</description>
		<content:encoded><![CDATA[<p>[...] the applications and provides testing and diagnostic information at runtime. I said before that testing is a valid use case, and this is one example. Another, report-related example is that whenever our applications [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Craig Stuntz</title>
		<link>http://blogs.teamb.com/craigstuntz/2009/01/12/37919/#comment-4350</link>
		<dc:creator>Craig Stuntz</dc:creator>
		<pubDate>Thu, 22 Jan 2009 04:53:03 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.teamb.com/craigstuntz/?p=37919#comment-4350</guid>
		<description>You'll note that I linked Pex in the post.

But Pex works on methods, not classes. It won't solve the problem of state. It &lt;i&gt;is&lt;/i&gt; really useful for TDD, though, since it can find cases you might have missed, with nothing more than a prototype.

&lt;i&gt;It still seems to me that you are worrying about the number of input values to methods.&lt;/i&gt;

No. I'm concerned about being able to hit the "interesting" cases. Exhaustive testing is just a really, really inefficent way of doing that.

&lt;i&gt;It is possible that you will not be able to exercise all the code paths in the private methods by calling the public method. But that actually doesn’t matter, since any code calling that method can’t hit those code paths either.&lt;/i&gt;

Once more: Unless the methods under test are &lt;i&gt;truly referentially transparent&lt;/i&gt;, you can't know this.</description>
		<content:encoded><![CDATA[<p>You&#8217;ll note that I linked Pex in the post.</p>
<p>But Pex works on methods, not classes. It won&#8217;t solve the problem of state. It <i>is</i> really useful for TDD, though, since it can find cases you might have missed, with nothing more than a prototype.</p>
<p><i>It still seems to me that you are worrying about the number of input values to methods.</i></p>
<p>No. I&#8217;m concerned about being able to hit the "interesting" cases. Exhaustive testing is just a really, really inefficent way of doing that.</p>
<p><i>It is possible that you will not be able to exercise all the code paths in the private methods by calling the public method. But that actually doesn’t matter, since any code calling that method can’t hit those code paths either.</i></p>
<p>Once more: Unless the methods under test are <i>truly referentially transparent</i>, you can&#8217;t know this.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jim Cooper</title>
		<link>http://blogs.teamb.com/craigstuntz/2009/01/12/37919/#comment-4333</link>
		<dc:creator>Jim Cooper</dc:creator>
		<pubDate>Tue, 20 Jan 2009 13:33:20 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.teamb.com/craigstuntz/?p=37919#comment-4333</guid>
		<description>I do understand your argument Craig, I just don't think it's right :-)

&#62; The argument is, how do you ensure that you
&#62; have hit the useful test cases? Without 
&#62; thehidden input of state, this is really easy 
&#62; to do. When state is an input to the function, 
&#62; it is very difficult to do.

It still seems to me that you are worrying about the number of input values to methods. You are then getting concerned about combinatorial explosions of the number of test cases that might be required if internal (not visible to unit test) state is used in those methods.

But input values are not how you work out what tests are required. You only need to be concerned about code paths, both the obvious ones that coverage tools can measure (eg cyclomatic complexity), and less obvious ones like exceptions (and non-exceptions like the integer multiplication example, possibly).

Now, if a public method uses two private methods, you only need to supply the public method with sufficient input values to exercise all the code paths in those 3 methods. That's got to be orders of magnitude less tests in almost all cases :-)

It is possible that you will not be able to exercise all the code paths in the private methods by calling the public method. But that actually doesn't matter, since any code calling that method can't hit those code paths either. If you extend the same reasoning throughout the whole public interface of a class, you can see that it may not be necessary to get 100% test coverage.

This is one reason why private methods rarely need direct testing. 

My experience suggests the heuristic of only testing public methods works pretty much alway the time. Any complex private stuff that needed testing I've always had to extract elsewhere, as far as I can remember.

Needing to unit test a private method directly is a code smell, IMO.

Generally speaking, I find a human brain is required to determine the required unit tests, but there is an interesting tool under development at MS:

http://research.microsoft.com/en-us/projects/Pex/

Since I prefer to do TDD, it's not that useful to me currently (all new code, fortunately), but it might be quite handy for legacy code.</description>
		<content:encoded><![CDATA[<p>I do understand your argument Craig, I just don&#8217;t think it&#8217;s right <img src='http://blogs.teamb.com/craigstuntz/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>&gt; The argument is, how do you ensure that you<br />
&gt; have hit the useful test cases? Without<br />
&gt; thehidden input of state, this is really easy<br />
&gt; to do. When state is an input to the function,<br />
&gt; it is very difficult to do.</p>
<p>It still seems to me that you are worrying about the number of input values to methods. You are then getting concerned about combinatorial explosions of the number of test cases that might be required if internal (not visible to unit test) state is used in those methods.</p>
<p>But input values are not how you work out what tests are required. You only need to be concerned about code paths, both the obvious ones that coverage tools can measure (eg cyclomatic complexity), and less obvious ones like exceptions (and non-exceptions like the integer multiplication example, possibly).</p>
<p>Now, if a public method uses two private methods, you only need to supply the public method with sufficient input values to exercise all the code paths in those 3 methods. That&#8217;s got to be orders of magnitude less tests in almost all cases <img src='http://blogs.teamb.com/craigstuntz/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>It is possible that you will not be able to exercise all the code paths in the private methods by calling the public method. But that actually doesn&#8217;t matter, since any code calling that method can&#8217;t hit those code paths either. If you extend the same reasoning throughout the whole public interface of a class, you can see that it may not be necessary to get 100% test coverage.</p>
<p>This is one reason why private methods rarely need direct testing. </p>
<p>My experience suggests the heuristic of only testing public methods works pretty much alway the time. Any complex private stuff that needed testing I&#8217;ve always had to extract elsewhere, as far as I can remember.</p>
<p>Needing to unit test a private method directly is a code smell, IMO.</p>
<p>Generally speaking, I find a human brain is required to determine the required unit tests, but there is an interesting tool under development at MS:</p>
<p><a href="http://research.microsoft.com/en-us/projects/Pex/" rel="nofollow">http://research.microsoft.com/en-us/projects/Pex/</a></p>
<p>Since I prefer to do TDD, it&#8217;s not that useful to me currently (all new code, fortunately), but it might be quite handy for legacy code.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Craig Stuntz</title>
		<link>http://blogs.teamb.com/craigstuntz/2009/01/12/37919/#comment-4323</link>
		<dc:creator>Craig Stuntz</dc:creator>
		<pubDate>Mon, 19 Jan 2009 11:55:50 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.teamb.com/craigstuntz/?p=37919#comment-4323</guid>
		<description>Of course I don't think that 2^32 tests would be required. Which is why I wrote this in the post:

&lt;i&gt;That’s a lot, and perhaps we would not want to test every possible case...&lt;/i&gt;

If it seems that I've based my argument on the possible number of inputs, then either I was not clear, or you miss the point. The argument is, how do you ensure that you have hit the useful test cases? Without the hidden input of state, this is really easy to do. When state is an input to the function, it is very difficult to do.

If the public function under test is guaranteed to be a referentially transparent function, then I agree that testing the public function is all that is required, no matter what it may do internally. But it is both difficult and rare to make that guarantee in Delphi or C# code.</description>
		<content:encoded><![CDATA[<p>Of course I don&#8217;t think that 2^32 tests would be required. Which is why I wrote this in the post:</p>
<p><i>That’s a lot, and perhaps we would not want to test every possible case&#8230;</i></p>
<p>If it seems that I&#8217;ve based my argument on the possible number of inputs, then either I was not clear, or you miss the point. The argument is, how do you ensure that you have hit the useful test cases? Without the hidden input of state, this is really easy to do. When state is an input to the function, it is very difficult to do.</p>
<p>If the public function under test is guaranteed to be a referentially transparent function, then I agree that testing the public function is all that is required, no matter what it may do internally. But it is both difficult and rare to make that guarantee in Delphi or C# code.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jim Cooper</title>
		<link>http://blogs.teamb.com/craigstuntz/2009/01/12/37919/#comment-4322</link>
		<dc:creator>Jim Cooper</dc:creator>
		<pubDate>Mon, 19 Jan 2009 02:48:05 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.teamb.com/craigstuntz/?p=37919#comment-4322</guid>
		<description>&#62; Ah, so you do agree that Foo is not "too 
&#62; simple to test." 

Actually, no I don't. :-)

In the real world, I wouldn't normally test it, as, yes, I think it is too simple to test (in most situations, anyway). It is certainly not worth making it public solely so you could test it. 

The example tests I gave were just to demonstrate that 2^32 tests are not required to completely exercise the code. I think 2 or 3 does it, don't you? 

Basing your argument on the possible number of inputs was a red herring, IMO.</description>
		<content:encoded><![CDATA[<p>&gt; Ah, so you do agree that Foo is not "too<br />
&gt; simple to test." </p>
<p>Actually, no I don&#8217;t. <img src='http://blogs.teamb.com/craigstuntz/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>In the real world, I wouldn&#8217;t normally test it, as, yes, I think it is too simple to test (in most situations, anyway). It is certainly not worth making it public solely so you could test it. </p>
<p>The example tests I gave were just to demonstrate that 2^32 tests are not required to completely exercise the code. I think 2 or 3 does it, don&#8217;t you? </p>
<p>Basing your argument on the possible number of inputs was a red herring, IMO.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Craig Stuntz</title>
		<link>http://blogs.teamb.com/craigstuntz/2009/01/12/37919/#comment-4309</link>
		<dc:creator>Craig Stuntz</dc:creator>
		<pubDate>Fri, 16 Jan 2009 15:14:04 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.teamb.com/craigstuntz/?p=37919#comment-4309</guid>
		<description>Ah, so you do agree that Foo is not "too simple to test." Me, too. Which gets us right back to the beginning. :)</description>
		<content:encoded><![CDATA[<p>Ah, so you do agree that Foo is not "too simple to test." Me, too. Which gets us right back to the beginning. <img src='http://blogs.teamb.com/craigstuntz/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jim Cooper</title>
		<link>http://blogs.teamb.com/craigstuntz/2009/01/12/37919/#comment-4308</link>
		<dc:creator>Jim Cooper</dc:creator>
		<pubDate>Fri, 16 Jan 2009 08:43:22 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.teamb.com/craigstuntz/?p=37919#comment-4308</guid>
		<description>&#62; I don’t agree that private methods would be "too dangerous to use" whenever "state is
&#62; an argument."

Neither do I - and that's exactly what I said. 

&#62; Finally, the specification I referred to was the one in your comment, namely that Foo "can 
&#62; also throw an exception for sufficiently large a." This is demonstrably incorrect.

I see. Well, as you yourself implied, the code **can** throw an exception. You need the right compiler options, but it still can. 

So if you were testing Foo you would write at least one test:

        [Test]
        public void Foo()
        {
            Assert.That(Foo(1), Is.EqualTo(2));
        }

So you're covered if someone changes Foo to multiply by 3 or something.

If you wanted the code to throw an exception (you might not, of course) you might also test for that - the boundary conditions, if you will :

        [Test]
        [ExpectedException(typeof(OverflowException))]
        public void FooMaxInt()
        {
            Foo(int.MaxValue);
        }

        [Test]
        [ExpectedException(typeof(OverflowException))]
        public void FooMinInt()
        {
            Foo(int.MinValue);
        }

At this point you could discover you do not have the compiler switch right, so the tests fail. You turn the switch on and the tests pass. TDD in action.

&#62; I feel like we’re going in circles here, and getting away from the original point, namely 
&#62; that testing is by itself a valid use case.

I'm a big proponent of unit testing, but I didn't agree with your 2^32 numbers and the subsequent argument. I still don't.

That you should write testable code I completely agree with, hence my use of TDD whenever possible. That you might need to refactor to be able to test something more easily is of course true. Testability is one measure of design quality.

However, I do not (in general) equate unit tests with use cases. Unit testing is for classes; use cases should not be that detailed (you'd have to refactor your use cases all the time, it'd just be silly). 

In principle, I wouldn't make unit testing a use case either, I don't think. In my mind it is just good, professional programming practice. You should just do it whenever it is appropriate. I see no need to explicitly say when it should be done.

Higher level tests (UI, integration, performance etc) are required to test use cases or user stories, IMO (as they are higher level constructs than classes). And I wouldn't get so recursive as to make that sort of testing a use case :-) 

I would get someone else to do that type of testing, too (seriously).</description>
		<content:encoded><![CDATA[<p>&gt; I don’t agree that private methods would be "too dangerous to use" whenever "state is<br />
&gt; an argument."</p>
<p>Neither do I - and that&#8217;s exactly what I said. </p>
<p>&gt; Finally, the specification I referred to was the one in your comment, namely that Foo "can<br />
&gt; also throw an exception for sufficiently large a." This is demonstrably incorrect.</p>
<p>I see. Well, as you yourself implied, the code **can** throw an exception. You need the right compiler options, but it still can. </p>
<p>So if you were testing Foo you would write at least one test:</p>
<p>        [Test]<br />
        public void Foo()<br />
        {<br />
            Assert.That(Foo(1), Is.EqualTo(2));<br />
        }</p>
<p>So you&#8217;re covered if someone changes Foo to multiply by 3 or something.</p>
<p>If you wanted the code to throw an exception (you might not, of course) you might also test for that - the boundary conditions, if you will :</p>
<p>        [Test]<br />
        [ExpectedException(typeof(OverflowException))]<br />
        public void FooMaxInt()<br />
        {<br />
            Foo(int.MaxValue);<br />
        }</p>
<p>        [Test]<br />
        [ExpectedException(typeof(OverflowException))]<br />
        public void FooMinInt()<br />
        {<br />
            Foo(int.MinValue);<br />
        }</p>
<p>At this point you could discover you do not have the compiler switch right, so the tests fail. You turn the switch on and the tests pass. TDD in action.</p>
<p>&gt; I feel like we’re going in circles here, and getting away from the original point, namely<br />
&gt; that testing is by itself a valid use case.</p>
<p>I&#8217;m a big proponent of unit testing, but I didn&#8217;t agree with your 2^32 numbers and the subsequent argument. I still don&#8217;t.</p>
<p>That you should write testable code I completely agree with, hence my use of TDD whenever possible. That you might need to refactor to be able to test something more easily is of course true. Testability is one measure of design quality.</p>
<p>However, I do not (in general) equate unit tests with use cases. Unit testing is for classes; use cases should not be that detailed (you&#8217;d have to refactor your use cases all the time, it&#8217;d just be silly). </p>
<p>In principle, I wouldn&#8217;t make unit testing a use case either, I don&#8217;t think. In my mind it is just good, professional programming practice. You should just do it whenever it is appropriate. I see no need to explicitly say when it should be done.</p>
<p>Higher level tests (UI, integration, performance etc) are required to test use cases or user stories, IMO (as they are higher level constructs than classes). And I wouldn&#8217;t get so recursive as to make that sort of testing a use case <img src='http://blogs.teamb.com/craigstuntz/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>I would get someone else to do that type of testing, too (seriously).</p>
]]></content:encoded>
	</item>
</channel>
</rss>
