<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>out &#62;&#62; m_Conscientia; &#187; Unit Testing</title>
	<atom:link href="http://blog.hypercomplex.co.uk/index.php/tag/unit-testing/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.hypercomplex.co.uk</link>
	<description>a multidimensional braindump</description>
	<lastBuildDate>Mon, 02 Aug 2010 22:30:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>CHESS on Channel9</title>
		<link>http://blog.hypercomplex.co.uk/index.php/2009/12/chess-on-channel9/</link>
		<comments>http://blog.hypercomplex.co.uk/index.php/2009/12/chess-on-channel9/#comments</comments>
		<pubDate>Wed, 09 Dec 2009 22:43:43 +0000</pubDate>
		<dc:creator>Alex Peck</dc:creator>
				<category><![CDATA[Software Engineering]]></category>
		<category><![CDATA[concurrency]]></category>
		<category><![CDATA[heisenbug]]></category>
		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">http://blog.hypercomplex.co.uk/?p=673</guid>
		<description><![CDATA[CHESS is a test tool for finding and reproducing Heisenbugs. Put simply, it takes the non out of non deterministic when testing concurrent code. Toward the beginning there is quite a nice illustration of why you might not find Heisenbugs by stress testing. Further in there is a more detailed explanation of how CHESS works, [...]]]></description>
			<content:encoded><![CDATA[<p>CHESS is a test tool for finding and reproducing Heisenbugs. Put simply, it takes the non out of non deterministic when testing concurrent code.</p>
<p><object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="512" height="384"><param name="source" value="http://channel9.msdn.com/App_Themes/default/vp09_11_30.xap" /><param name="initParams" value="deferredLoad=true,duration=0,m=mms://mschnlnine.wmod.llnwd.net/a1809/d1/ch9/0/0/1/7/4/4/InsideCHESS_s_ch9.wmv,autostart=false,autohide=true,showembed=true, postid=447100" /><param name="background" value="#00FFFFFF" /><a href="http://go.microsoft.com/fwlink/?LinkID=124807" style="text-decoration: none;"><br />
<img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style: none"/><br />
</a><br />
</object><br />
Toward the beginning there is quite a nice illustration of why you might not find Heisenbugs by stress testing. Further in there is a more detailed explanation of how CHESS works, and how the thread interleaving state space is reduced to a manageable level.</p>
<p><object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="512" height="384"><param name="source" value="http://channel9.msdn.com/App_Themes/default/vp09_11_30.xap" /><param name="initParams" value="deferredLoad=true,duration=0,m=http://mschnlnine.vo.llnwd.net/d1/ch9/4/4/5/1/5/4/gettingstartedwithchessinvs2008_2MB_ch9.wmv,autostart=false,autohide=true,showembed=true, postid=451544" /><param name="background" value="#00FFFFFF" /><a href="http://go.microsoft.com/fwlink/?LinkID=124807" style="text-decoration: none;"><br />
<img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style: none"/><br />
</a><br />
</object><br />
In this installment we see how to use the managed version of CHESS within a Visual Studio concurrency unit test.</p>
<p><object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="512" height="384"><param name="source" value="http://channel9.msdn.com/App_Themes/default/vp09_11_30.xap" /><param name="initParams" value="deferredLoad=true,duration=0,m=http://mschnlnine.vo.llnwd.net/d1/ch9/2/1/1/3/7/4/dataracedetectionwithchess_2MB_ch9.wmv,autostart=false,autohide=true,showembed=true, postid=473112" /><param name="background" value="#00FFFFFF" /><a href="http://go.microsoft.com/fwlink/?LinkID=124807" style="text-decoration: none;"><br />
<img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style: none"/><br />
</a><br />
</object><br />
Finally, here we get an overview of the tooling which helps visualise different thread interleavings.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.hypercomplex.co.uk/index.php/2009/12/chess-on-channel9/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
<enclosure url="http://mschnlnine.vo.llnwd.net/d1/ch9/4/4/5/1/5/4/gettingstartedwithchessinvs2008_2MB_ch9.wmv" length="64586733" type="video/x-ms-wmv" />
<enclosure url="http://mschnlnine.vo.llnwd.net/d1/ch9/2/1/1/3/7/4/dataracedetectionwithchess_2MB_ch9.wmv" length="125988025" type="video/x-ms-wmv" />
		</item>
		<item>
		<title>100% code coverage</title>
		<link>http://blog.hypercomplex.co.uk/index.php/2009/09/100pc-code-coverage/</link>
		<comments>http://blog.hypercomplex.co.uk/index.php/2009/09/100pc-code-coverage/#comments</comments>
		<pubDate>Fri, 25 Sep 2009 21:31:45 +0000</pubDate>
		<dc:creator>Alex Peck</dc:creator>
				<category><![CDATA[Software Engineering]]></category>
		<category><![CDATA[code coverage]]></category>
		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">http://blog.hypercomplex.co.uk/?p=526</guid>
		<description><![CDATA[Unit tests can&#8217;t expose bugs in code they don&#8217;t execute. Therefore, unit tests should have a high degree of code coverage. If you test first, inject all dependencies and use mocks, 100% block coverage isn&#8217;t an onerous a goal. But what does block coverage really tell you? The answer is not much. It&#8217;s easy to [...]]]></description>
			<content:encoded><![CDATA[<p>Unit tests can&#8217;t expose bugs in code they don&#8217;t execute. Therefore, unit tests should have a high degree of code coverage. If you test first, <a href="http://blog.hypercomplex.co.uk/index.php/2009/06/dependency-injection-testability/">inject all dependencies</a> and use mocks, 100% block coverage isn&#8217;t an onerous a goal. But what does block coverage really tell you?</p>
<p>The answer is not much. It&#8217;s easy to spot which code you didn&#8217;t execute. For code that did execute, if neither the behaviour nor resultant state is verified, all you really know is that there were no unexpected errors. In other words, this code coverage is merely <a href="http://jasonrudolph.com/blog/2008/06/17/testing-anti-patterns-incidental-coverage/">incidental</a>, and no actual testing took place.</p>
<p>Furthermore, even with 100% coverage you have no indication of the code you should have written, but didn&#8217;t. For example, consider this simple one liner:</p>

<div class="wp_codebox"><table><tr id="p5262"><td class="code" id="p526code2"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">float</span> Divide<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">float</span> dividend, <span style="color: #6666cc; font-weight: bold;">float</span> divisor<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
   <span style="color: #0600FF; font-weight: bold;">return</span> dividend <span style="color: #008000;">/</span> divisor<span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> TestDivide<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
   <span style="color: #6666cc; font-weight: bold;">float</span> quotient <span style="color: #008000;">=</span> Divide<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">10</span>, <span style="color: #FF0000;">2</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
   Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">AreEqual</span><span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">float</span><span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">5</span>, quotient<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>The test case gives us 100% block coverage. However, we will get different behaviour if we use a divisor of 0. This different behaviour may very well constitute a bug. Interestingly, we won&#8217;t find this bug even if we use a more robust coverage metric (e.g path coverage) because it is a boundary problem.</p>
<p><!-- http://panela.blog-city.com/pycon2008_video_of_managing_complexity_up.htm --></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.hypercomplex.co.uk/index.php/2009/09/100pc-code-coverage/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>When dependency injection meets IDisposable</title>
		<link>http://blog.hypercomplex.co.uk/index.php/2009/06/when-dependency-injection-meets-idisposable/</link>
		<comments>http://blog.hypercomplex.co.uk/index.php/2009/06/when-dependency-injection-meets-idisposable/#comments</comments>
		<pubDate>Sun, 21 Jun 2009 21:50:58 +0000</pubDate>
		<dc:creator>Alex Peck</dc:creator>
				<category><![CDATA[Software Engineering]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Dependency Injection]]></category>
		<category><![CDATA[IDisposable]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">http://blog.hypercomplex.co.uk/?p=500</guid>
		<description><![CDATA[What follows is a solution which should really have been obvious to me the first time I faced the problem: how do you decouple logic and instantiation of collaborating objects when faced with an IDisposable class? Consider this: public class NiceClass &#123; private DependentClass injectedDependency; &#160; public NiceClass&#40;DependentClass injectedDependency&#41; &#123; this.injectedDependency = injectedDependency; &#125; &#160; [...]]]></description>
			<content:encoded><![CDATA[<p>What follows is a solution which should really have been obvious to me the first time I faced the problem: how do you decouple logic and instantiation of collaborating objects when faced with an IDisposable class? Consider this:</p>

<div class="wp_codebox"><table><tr id="p5006"><td class="code" id="p500code6"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> NiceClass
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> DependentClass injectedDependency<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> NiceClass<span style="color: #008000;">&#40;</span>DependentClass injectedDependency<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">injectedDependency</span> <span style="color: #008000;">=</span> injectedDependency<span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> CoupledMethod<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span>IFace c <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Face<span style="color: #008000;">&#40;</span>injectedDependency<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #008080; font-style: italic;">// do something with c</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>We can see that injectedDependency is not coupled, but what about the local instance of Face in CoupledMethod? To decouple it use a delegate to return the IFace instance instead of calling new directly. If the coupling had been to a concrete type (Face), we could refactor to an abstract type (IFace) or wrapper first.</p>

<div class="wp_codebox"><table><tr id="p5007"><td class="code" id="p500code7"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> NiceClass
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">delegate</span> IFace IFaceCreatorDelegate<span style="color: #008000;">&#40;</span>DependentClass injectedDependency<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">private</span> DependentClass injectedDependency<span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> IFaceCreatorDelegate iFaceCreator<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> NiceClass<span style="color: #008000;">&#40;</span>DependentClass injectedDependency, IFaceCreatorDelegate iFaceCreator<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">injectedDependency</span> <span style="color: #008000;">=</span> injectedDependency<span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">iFaceCreator</span> <span style="color: #008000;">=</span> iFaceCreator<span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> CoupledMethod<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span>IFace c <span style="color: #008000;">=</span> iFaceCreator<span style="color: #008000;">&#40;</span>injectedDependency<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #008080; font-style: italic;">// do something with c</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>This is an additional three lines of code, which arguably makes the control flow harder to read statically. It does, however, improve testability. I think of this as equivalent to the classic tradeoff between <a href="http://www.threeriversinstitute.org/AbstractVsConcreteParameters.html">abstract and concrete parameters</a>.</p>
<p>Instantiating NiceClass is not too arduous either with a lambda expression (although it is still a little verbose).</p>

<div class="wp_codebox"><table><tr id="p5008"><td class="code" id="p500code8"><pre class="csharp" style="font-family:monospace;">NiceClass nice <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> NiceClass<span style="color: #008000;">&#40;</span>
   dependency, 
   <span style="color: #008000;">&#40;</span>DependentClass d<span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Face<span style="color: #008000;">&#40;</span>d<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.hypercomplex.co.uk/index.php/2009/06/when-dependency-injection-meets-idisposable/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Dependency Injection &amp; Testability</title>
		<link>http://blog.hypercomplex.co.uk/index.php/2009/06/dependency-injection-testability/</link>
		<comments>http://blog.hypercomplex.co.uk/index.php/2009/06/dependency-injection-testability/#comments</comments>
		<pubDate>Wed, 17 Jun 2009 19:01:06 +0000</pubDate>
		<dc:creator>Alex Peck</dc:creator>
				<category><![CDATA[Software Engineering]]></category>
		<category><![CDATA[code coverage]]></category>
		<category><![CDATA[Dependency Injection]]></category>
		<category><![CDATA[talk]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">http://blog.hypercomplex.co.uk/?p=111</guid>
		<description><![CDATA[When objects have no (or few) collaborators it&#8217;s relatively easy to get high unit test code coverage. As the number and complexity of collaborators increases, so too does the difficulty in obtaining decent coverage. This talk illustrates why dependency injection principles improve the testability of your code, and ease mock/stub based unit testing.]]></description>
			<content:encoded><![CDATA[<p>When objects have no (or few) collaborators it&#8217;s relatively easy to get high unit test code coverage. As the number and complexity of collaborators increases, so too does the difficulty in obtaining decent coverage. This talk illustrates why dependency injection principles improve the testability of your code, and ease <a href="http://martinfowler.com/articles/mocksArentStubs.html" target="_blank">mock/stub based unit testing</a>.</p>
<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="425" height="344" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowFullScreen" value="true" /><param name="allowScriptAccess" value="always" /><param name="src" value="http://www.youtube.com/v/RlfLCWKxHJ0&amp;color1=0xb1b1b1&amp;color2=0xcfcfcf&amp;hl=en&amp;feature=player_embedded&amp;fs=1" /><param name="allowfullscreen" value="true" /><embed type="application/x-shockwave-flash" width="425" height="344" src="http://www.youtube.com/v/RlfLCWKxHJ0&amp;color1=0xb1b1b1&amp;color2=0xcfcfcf&amp;hl=en&amp;feature=player_embedded&amp;fs=1" allowscriptaccess="always" allowfullscreen="true"></embed></object></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.hypercomplex.co.uk/index.php/2009/06/dependency-injection-testability/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>TDD: An empirical study at Microsoft</title>
		<link>http://blog.hypercomplex.co.uk/index.php/2009/01/tdd-an-empirical-study-at-microsoft/</link>
		<comments>http://blog.hypercomplex.co.uk/index.php/2009/01/tdd-an-empirical-study-at-microsoft/#comments</comments>
		<pubDate>Mon, 05 Jan 2009 23:49:01 +0000</pubDate>
		<dc:creator>Alex Peck</dc:creator>
				<category><![CDATA[Software Engineering]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">http://blog.hypercomplex.co.uk/?p=520</guid>
		<description><![CDATA[In my experience, TDD focuses you on writing testable code with good edge case and error handling. Most TDD discussions revolve around this kind of anecdotal evidence of improvement. Microsoft research have published a paper exploring the effect of TDD on various quality metrics. You can check out a short Channel 9 overview here.]]></description>
			<content:encoded><![CDATA[<p>In my experience, <a href="http://en.wikipedia.org/wiki/Test-driven_development">TDD</a> focuses you on writing testable code with good edge case and error handling. Most TDD discussions revolve around this kind of anecdotal evidence of improvement.</p>
<p>Microsoft research have published a <a href="http://research.microsoft.com/en-us/projects/esm/nagappan_tdd.pdf">paper</a> exploring the effect of TDD on various quality metrics. You can check out a short Channel 9 overview <a href="http://channel9.msdn.com/posts/Peli/Experimental-study-about-Test-Driven-Development/">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.hypercomplex.co.uk/index.php/2009/01/tdd-an-empirical-study-at-microsoft/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
