<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xml:base="http://quest.windwards.net"  xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
 <title>Quest for improvement - test-driven</title>
 <link>http://quest.windwards.net/tags/test-driven</link>
 <description></description>
 <language>en</language>
<item>
 <title>What makes a good unit test?</title>
 <link>http://quest.windwards.net/content/what-makes-good-unit-test</link>
 <description>&lt;div class=&quot;field field-name-body field-type-text-with-summary field-label-hidden&quot;&gt;&lt;div class=&quot;field-items&quot;&gt;&lt;div class=&quot;field-item even&quot; property=&quot;content:encoded&quot;&gt;&lt;h2&gt;
	Motivating rant&lt;/h2&gt;
&lt;p&gt;There are numerous articles out there talking about how to write unit tests. Still, I find that they mostly assume that the reader is an accomplished developer trying to learn to write unit tests.&lt;/p&gt;
&lt;p&gt;In particular, many articles, such as &lt;a href=&quot;http://wiki.developerforce.com/page/How_to_Write_Good_Unit_Tests&quot;&gt;this one from the force.com people&lt;/a&gt;, may have the right idea, but atrocious examples. In the linked article, the getFraction() example doesn&#039;t test the behaviour of a unit, it tests the division operation of the processor! Not only is this a fairly unlikely failure, it is also highly specific to the processor the unit test is run on, and doesn&#039;t say whether it is going to work on any other processor the code is need to work on.&lt;/p&gt;
&lt;p&gt;For those who are trying to learn programming, a somewhat different approach is needed. This article tries to find an approach that work in a situation where you are trying to learn programming with a test-driven approach.&lt;/p&gt;
&lt;h2&gt;
	What is a &quot;test&quot;?&lt;/h2&gt;
&lt;p&gt;Technically, a test is a small program that tries to make certain that some part of the production software performs as expected.&lt;/p&gt;
&lt;p&gt;Simply put, each test should tell a story. This story contains&lt;/p&gt;
&lt;ul&gt;&lt;li&gt;
		a protagonist: it may be a procedure, method, class or an object,&lt;/li&gt;
&lt;li&gt;
		(usually) supporting characters: these are typically mocked objects or methods, and&lt;/li&gt;
&lt;li&gt;
		an interesting story: this is usually a use case for a part (the so-called &quot;unit&quot;) of the system.&lt;/li&gt;
&lt;/ul&gt;&lt;h2&gt;
	So what is a &quot;unit&quot;?&lt;/h2&gt;
&lt;p&gt;Most people ask this question when they try to make sense of test-driven development. The traditional answer is that the unit is, in &lt;a href=&quot;http://en.wikipedia.org/wiki/Unit_testing&quot;&gt;Wikipedia&#039;s terms&lt;/a&gt; &quot;&lt;em&gt;the smallest testable part of an application&lt;/em&gt;.&quot; The problem is that when we write the test before the implementation, the choice of a unit is a side-effect of the narrative of the test and so does not require particular consideration.&lt;/p&gt;
&lt;h2&gt;
	So what about this story?&lt;/h2&gt;
&lt;p&gt;Without further ado:&lt;/p&gt;
&lt;pre class=&quot;brush: java&quot;&gt;
public void testWolfCanWasteHousesButNotThatOfPracticalPiglet() {
    Wolf wolf = new Wolf();
    Piglet fifer = Hamlet.lookupPiglet(&quot;&lt;i&gt;Fifer&lt;/i&gt;&quot;);
    assert !fifer.isMason(); // sanity check
    House house = fifer.getHouse();
    wolf.huffAndPuff(house);
    assert house.isRuined();
    assert house.getDoor().isOpen();

    Piglet practical = Hamlet.lookupPiglet(&quot;Practical&quot;);
    assert practical.isMason();
    house = practical.getHouse();
    wolf.huffAndPuff(house);
    assert !house.isRuined();
    assert !house.getDoor().isOpen();
}&lt;/pre&gt;&lt;p&gt;This test documents the contract between a Wolf and various piglets. Basicly, it says that a Wolf can huff and puff and blow the house down, unless the house is built by a mason.&lt;/p&gt;
&lt;p&gt;Depending on programming language and development environment, exactly how to execute a test varies. There are numerous articles detailing this for various programming language and environment. Search engines are your friends.&lt;/p&gt;
&lt;p&gt;Tests should be named for what they test. The ultimate goal of the test name is so that a developer immediately understands contract has been broken.&lt;/p&gt;
&lt;h2&gt;
	Finally&lt;/h2&gt;
&lt;p&gt;Finally, there are some &lt;a href=&quot;http://blog.stevensanderson.com/2009/08/24/writing-great-unit-tests-best-and-worst-practises/&quot;&gt;fine articles about unit testing&lt;/a&gt; out there.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;field field-name-field-tags field-type-taxonomy-term-reference field-label-above&quot;&gt;&lt;div class=&quot;field-label&quot;&gt;Tags:&amp;nbsp;&lt;/div&gt;&lt;div class=&quot;field-items&quot;&gt;&lt;div class=&quot;field-item even&quot; rel=&quot;dc:subject&quot;&gt;&lt;a href=&quot;/tags/teaching&quot; typeof=&quot;skos:Concept&quot; property=&quot;rdfs:label skos:prefLabel&quot; datatype=&quot;&quot;&gt;teaching&lt;/a&gt;&lt;/div&gt;&lt;div class=&quot;field-item odd&quot; rel=&quot;dc:subject&quot;&gt;&lt;a href=&quot;/tags/test-driven&quot; typeof=&quot;skos:Concept&quot; property=&quot;rdfs:label skos:prefLabel&quot; datatype=&quot;&quot;&gt;test-driven&lt;/a&gt;&lt;/div&gt;&lt;div class=&quot;field-item even&quot; rel=&quot;dc:subject&quot;&gt;&lt;a href=&quot;/tags/quest-blog&quot; typeof=&quot;skos:Concept&quot; property=&quot;rdfs:label skos:prefLabel&quot; datatype=&quot;&quot;&gt;quest-blog&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;</description>
 <pubDate>Thu, 12 Jul 2012 07:06:28 +0000</pubDate>
 <dc:creator>quest</dc:creator>
 <guid isPermaLink="false">21 at http://quest.windwards.net</guid>
 <comments>http://quest.windwards.net/content/what-makes-good-unit-test#comments</comments>
</item>
</channel>
</rss>
