<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" version="2.0">
  <channel>
    <title>ploeh blog - Productivity</title>
    <link>http://blog.ploeh.dk/</link>
    <description>Mark Seemann's .NET blog</description>
    <language>en-us</language>
    <copyright>Mark Seemann</copyright>
    <lastBuildDate>Fri, 15 May 2009 05:34:00 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.3.9074.18820</generator>
    <managingEditor>mark@seemann.ms</managingEditor>
    <webMaster>mark@seemann.ms</webMaster>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=ef5bab91-8b41-4cc5-9e44-f6accd855d64</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,ef5bab91-8b41-4cc5-9e44-f6accd855d64.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,ef5bab91-8b41-4cc5-9e44-f6accd855d64.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=ef5bab91-8b41-4cc5-9e44-f6accd855d64</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Dear reader, I hope you are still with me!
</p>
        <p>
After eight posts of <a href="http://autofixture.codeplex.com/">AutoFixture</a> feature
walkthroughs, I can't blame you for wondering why this tool might even be relevant
to you. In this post, we'll finally begin to look at how AutoFixture can help you
towards <a href="http://blog.ploeh.dk/2009/01/28/ZeroFrictionTDD.aspx">Zero-Friction
TDD</a>!
</p>
        <p>
In an earlier post, I described how the <a href="http://blog.ploeh.dk/2009/03/16/FixtureObject.aspx">Fixture
Object</a> pattern can help you greatly reduce the amount of test code that you have
to write. Since AutoFixture was designed to act as a general-purpose Fixture Object,
it can help you reduce the amount of test code even further, letting you focus on <a href="http://blog.ploeh.dk/2009/03/10/SpecificationDrivenDevelopment.aspx">specifying
the behavior</a> of your <a href="http://xunitpatterns.com/SUT.html">SUT</a>.
</p>
        <p>
In that former post, the original example was this complex test that I will repeat
in it's entirety for your benefit (or horror):
</p>
        <!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;\red0\green0\blue255;\red0\green128\blue0;\red163\green21\blue21;}??\fs20 [\cf3 TestMethod\cf0 ]\par ??\cf4 public\cf0  \cf4 void\cf0  NumberSumIsCorrect_Na\u239 ?ve()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf3 Thing\cf0  thing1 = \cf4 new\cf0  \cf3 Thing\cf0 ()\par ??    \{\par ??        Number = 3,\par ??        Text = \cf6 "Anonymous text 1"\par ??\cf0     \};\par ??    \cf3 Thing\cf0  thing2 = \cf4 new\cf0  \cf3 Thing\cf0 ()\par ??    \{\par ??        Number = 6,\par ??        Text = \cf6 "Anonymous text 2"\par ??\cf0     \};\par ??    \cf3 Thing\cf0  thing3 = \cf4 new\cf0  \cf3 Thing\cf0 ()\par ??    \{\par ??        Number = 1,\par ??        Text = \cf6 "Anonymous text 3"\par ??\cf0     \};\par ??\par ??    \cf4 int\cf0  expectedSum = \cf4 new\cf0 [] \{ thing1, thing2, thing3 \}.\par ??        Select(t =&gt; t.Number).Sum();\par ??\par ??    \cf3 IMyInterface\cf0  fake = \cf4 new\cf0  \cf3 FakeMyInterface\cf0 ();\par ??    fake.AddThing(thing1);\par ??    fake.AddThing(thing2);\par ??    fake.AddThing(thing3);\par ??\par ??    \cf3 MyClass\cf0  sut = \cf4 new\cf0  \cf3 MyClass\cf0 (fake);\par ??    \cf5 // Exercise system\par ??\cf0     \cf4 int\cf0  result = sut.CalculateSumOfThings();\par ??    \cf5 // Verify outcome\par ??\cf0     \cf3 Assert\cf0 .AreEqual&lt;\cf4 int\cf0 &gt;(expectedSum, result,\par ??        \cf6 "Sum of things"\cf0 );\par ??    \cf5 // Teardown\par ??\cf0 \}}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">[<span style="color: #2b91af">TestMethod</span>]</pre>
          <pre style="margin: 0px">
            <span style="color: blue">public</span>
            <span style="color: blue">void</span> NumberSumIsCorrect_Naïve()</pre>
          <pre style="margin: 0px">{</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Fixture setup</span></pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">Thing</span> thing1
= <span style="color: blue">new</span><span style="color: #2b91af">Thing</span>()</pre>
          <pre style="margin: 0px">    {</pre>
          <pre style="margin: 0px">        Number = 3,</pre>
          <pre style="margin: 0px">        Text = <span style="color: #a31515">"Anonymous
text 1"</span></pre>
          <pre style="margin: 0px">    };</pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">Thing</span> thing2
= <span style="color: blue">new</span><span style="color: #2b91af">Thing</span>()</pre>
          <pre style="margin: 0px">    {</pre>
          <pre style="margin: 0px">        Number = 6,</pre>
          <pre style="margin: 0px">        Text = <span style="color: #a31515">"Anonymous
text 2"</span></pre>
          <pre style="margin: 0px">    };</pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">Thing</span> thing3
= <span style="color: blue">new</span><span style="color: #2b91af">Thing</span>()</pre>
          <pre style="margin: 0px">    {</pre>
          <pre style="margin: 0px">        Number = 1,</pre>
          <pre style="margin: 0px">        Text = <span style="color: #a31515">"Anonymous
text 3"</span></pre>
          <pre style="margin: 0px">    };</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: blue">int</span> expectedSum
= <span style="color: blue">new</span>[] { thing1, thing2, thing3 }.</pre>
          <pre style="margin: 0px">        Select(t =&gt; t.Number).Sum();</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">IMyInterface</span> fake
= <span style="color: blue">new</span><span style="color: #2b91af">FakeMyInterface</span>();</pre>
          <pre style="margin: 0px">    fake.AddThing(thing1);</pre>
          <pre style="margin: 0px">    fake.AddThing(thing2);</pre>
          <pre style="margin: 0px">    fake.AddThing(thing3);</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">MyClass</span> sut
= <span style="color: blue">new</span><span style="color: #2b91af">MyClass</span>(fake);</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Exercise system</span></pre>
          <pre style="margin: 0px">    <span style="color: blue">int</span> result
= sut.CalculateSumOfThings();</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Verify outcome</span></pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">Assert</span>.AreEqual&lt;<span style="color: blue">int</span>&gt;(expectedSum,
result,</pre>
          <pre style="margin: 0px">        <span style="color: #a31515">"Sum
of things"</span>);</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Teardown</span></pre>
          <pre style="margin: 0px">}</pre>
        </div>
        <p>
This test consists of 18 lines of code.
</p>
        <p>
Using the Fixture Object pattern, I was able to cut that down to 7 lines of code,
which is a 61% improvement (however, the downside was an additional 19 lines of (reusable)
code for MyClassFixture, so the benefit can only be reaped when you have multiple
tests leveraged by the same Fixture Object. This was all covered in the <a href="http://blog.ploeh.dk/2009/03/16/FixtureObject.aspx">former
post, to which I will refer you</a>).
</p>
        <p>
With AutoFixture, we can do much better. Here's a one-off rewrite of the unit test
using AutoFixture:
</p>
        <!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;\red0\green0\blue255;\red0\green128\blue0;\red163\green21\blue21;}??\fs20 [\cf3 TestMethod\cf0 ]\par ??\cf4 public\cf0  \cf4 void\cf0  NumberSumIsCorrect_AutoFixture()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf3 Fixture\cf0  fixture = \cf4 new\cf0  \cf3 Fixture\cf0 ();\par ??    \cf3 IMyInterface\cf0  fake = \cf4 new\cf0  \cf3 FakeMyInterface\cf0 ();\par ??    fixture.Register&lt;\cf3 IMyInterface\cf0 &gt;(() =&gt; fake);\par ??\par ??    \cf4 var\cf0  things = fixture.CreateMany&lt;\cf3 Thing\cf0 &gt;().ToList();\par ??    things.ForEach(t =&gt; fake.AddThing(t));\par ??    \cf4 int\cf0  expectedSum = things.Select(t =&gt; t.Number).Sum();\par ??\par ??    \cf3 MyClass\cf0  sut = fixture.CreateAnonymous&lt;\cf3 MyClass\cf0 &gt;();\par ??    \cf5 // Exercise system\par ??\cf0     \cf4 int\cf0  result = sut.CalculateSumOfThings();\par ??    \cf5 // Verify outcome\par ??\cf0     \cf3 Assert\cf0 .AreEqual&lt;\cf4 int\cf0 &gt;(expectedSum, result,\par ??        \cf6 "Sum of things"\cf0 );\par ??    \cf5 // Teardown\par ??\cf0 \}}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">[<span style="color: #2b91af">TestMethod</span>]</pre>
          <pre style="margin: 0px">
            <span style="color: blue">public</span>
            <span style="color: blue">void</span> NumberSumIsCorrect_AutoFixture()</pre>
          <pre style="margin: 0px">{</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Fixture setup</span></pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">Fixture</span> fixture
= <span style="color: blue">new</span><span style="color: #2b91af">Fixture</span>();</pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">IMyInterface</span> fake
= <span style="color: blue">new</span><span style="color: #2b91af">FakeMyInterface</span>();</pre>
          <pre style="margin: 0px">    fixture.Register&lt;<span style="color: #2b91af">IMyInterface</span>&gt;(()
=&gt; fake);</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: blue">var</span> things
= fixture.CreateMany&lt;<span style="color: #2b91af">Thing</span>&gt;().ToList();</pre>
          <pre style="margin: 0px">    things.ForEach(t =&gt; fake.AddThing(t));</pre>
          <pre style="margin: 0px">    <span style="color: blue">int</span> expectedSum
= things.Select(t =&gt; t.Number).Sum();</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">MyClass</span> sut
= fixture.CreateAnonymous&lt;<span style="color: #2b91af">MyClass</span>&gt;();</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Exercise system</span></pre>
          <pre style="margin: 0px">    <span style="color: blue">int</span> result
= sut.CalculateSumOfThings();</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Verify outcome</span></pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">Assert</span>.AreEqual&lt;<span style="color: blue">int</span>&gt;(expectedSum,
result,</pre>
          <pre style="margin: 0px">        <span style="color: #a31515">"Sum
of things"</span>);</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Teardown</span></pre>
          <pre style="margin: 0px">}</pre>
        </div>
        <p>
In this test, I <a href="http://blog.ploeh.dk/2009/04/23/DealingWithTypesWithoutPublicConstructors.aspx">map
the concrete fake instance to the IMyInterface type</a> in the fixture object, and
then use its ability to <a href="http://blog.ploeh.dk/2009/05/11/AnonymousSequencesWithAutoFixture.aspx">create
many anonymous instances with one method call</a>. Before exercising the SUT, I also
use the fixture instance as a <a href="http://blog.ploeh.dk/2009/02/13/SUTFactory.aspx">SUT
Factory</a>.
</p>
        <p>
Apart from AutoFixture (and FakeMyInterface, which is invariant for all variations,
and thus kept out of the comparison), this test stands alone, but still manages to
reduce the number of code lines to 10 lines – a 44% improvement! In my book, that's
already a significant gain in productivity and maintainability, but we can do better!
</p>
        <p>
If we need to test MyClass repeatedly in similar ways, we can move the common code
to a Fixture Object based on AutoFixture, and the test can be refactored to this:
</p>
        <!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;\red0\green0\blue255;\red0\green128\blue0;\red163\green21\blue21;}??\fs20 [\cf3 TestMethod\cf0 ]\par ??\cf4 public\cf0  \cf4 void\cf0  NumberSumIsCorrect_DerivedFixture()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf3 MyClassFixture\cf0  fixture = \cf4 new\cf0  \cf3 MyClassFixture\cf0 ();\par ??    fixture.AddManyTo(fixture.Things);\par ??\par ??    \cf4 int\cf0  expectedSum = \par ??        fixture.Things.Select(t =&gt; t.Number).Sum();\par ??    \cf3 MyClass\cf0  sut = fixture.CreateAnonymous&lt;\cf3 MyClass\cf0 &gt;();\par ??    \cf5 // Exercise system\par ??\cf0     \cf4 int\cf0  result = sut.CalculateSumOfThings();\par ??    \cf5 // Verify outcome\par ??\cf0     \cf3 Assert\cf0 .AreEqual&lt;\cf4 int\cf0 &gt;(expectedSum, result,\par ??        \cf6 "Sum of things"\cf0 );\par ??    \cf5 // Teardown\par ??\cf0 \}}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">[<span style="color: #2b91af">TestMethod</span>]</pre>
          <pre style="margin: 0px">
            <span style="color: blue">public</span>
            <span style="color: blue">void</span> NumberSumIsCorrect_DerivedFixture()</pre>
          <pre style="margin: 0px">{</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Fixture setup</span></pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">MyClassFixture</span> fixture
= <span style="color: blue">new</span><span style="color: #2b91af">MyClassFixture</span>();</pre>
          <pre style="margin: 0px">    fixture.AddManyTo(fixture.Things);</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: blue">int</span> expectedSum
= </pre>
          <pre style="margin: 0px">        fixture.Things.Select(t =&gt; t.Number).Sum();</pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">MyClass</span> sut
= fixture.CreateAnonymous&lt;<span style="color: #2b91af">MyClass</span>&gt;();</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Exercise system</span></pre>
          <pre style="margin: 0px">    <span style="color: blue">int</span> result
= sut.CalculateSumOfThings();</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Verify outcome</span></pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">Assert</span>.AreEqual&lt;<span style="color: blue">int</span>&gt;(expectedSum,
result,</pre>
          <pre style="margin: 0px">        <span style="color: #a31515">"Sum
of things"</span>);</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Teardown</span></pre>
          <pre style="margin: 0px">}</pre>
        </div>
        <p>
Now we are back at 7 lines of code, which is on par with the original Fixture Object-based
test, but now MyClassFixture is reduced to 8 lines of code:
</p>
        <!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;}??\fs20 \cf1 internal\cf0  \cf1 class\cf0  \cf4 MyClassFixture\cf0  : \cf4 Fixture\par ??\cf0 \{\par ??    \cf1 internal\cf0  MyClassFixture()\par ??    \{\par ??        \cf1 this\cf0 .Things = \cf1 new\cf0  \cf4 List\cf0 &lt;\cf4 Thing\cf0 &gt;();\par ??        \cf1 this\cf0 .Register&lt;\cf4 IMyInterface\cf0 &gt;(() =&gt;\par ??            \{\par ??                \cf1 var\cf0  fake = \cf1 new\cf0  \cf4 FakeMyInterface\cf0 ();\par ??                \cf1 this\cf0 .Things.ToList().ForEach(t =&gt;\par ??                    fake.AddThing(t));\par ??                \cf1 return\cf0  fake;\par ??            \});\par ??    \}\par ??\par ??    \cf1 internal\cf0  \cf4 IList\cf0 &lt;\cf4 Thing\cf0 &gt; Things \{ \cf1 get\cf0 ; \cf1 private\cf0  \cf1 set\cf0 ; \}\par ??\}}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">
            <span style="color: blue">internal</span>
            <span style="color: blue">class</span>
            <span style="color: #2b91af">MyClassFixture</span> : <span style="color: #2b91af">Fixture</span></pre>
          <pre style="margin: 0px">{</pre>
          <pre style="margin: 0px">    <span style="color: blue">internal</span> MyClassFixture()</pre>
          <pre style="margin: 0px">    {</pre>
          <pre style="margin: 0px">        <span style="color: blue">this</span>.Things
= <span style="color: blue">new</span><span style="color: #2b91af">List</span>&lt;<span style="color: #2b91af">Thing</span>&gt;();</pre>
          <pre style="margin: 0px">        <span style="color: blue">this</span>.Register&lt;<span style="color: #2b91af">IMyInterface</span>&gt;(()
=&gt;</pre>
          <pre style="margin: 0px">            {</pre>
          <pre style="margin: 0px">                <span style="color: blue">var</span> fake
= <span style="color: blue">new</span><span style="color: #2b91af">FakeMyInterface</span>();</pre>
          <pre style="margin: 0px">                <span style="color: blue">this</span>.Things.ToList().ForEach(t
=&gt;</pre>
          <pre style="margin: 0px">                    fake.AddThing(t));</pre>
          <pre style="margin: 0px">                <span style="color: blue">return</span> fake;</pre>
          <pre style="margin: 0px">            });</pre>
          <pre style="margin: 0px">    }</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: blue">internal</span><span style="color: #2b91af">IList</span>&lt;<span style="color: #2b91af">Thing</span>&gt;
Things { <span style="color: blue">get</span>; <span style="color: blue">private</span><span style="color: blue">set</span>;
}</pre>
          <pre style="margin: 0px">}</pre>
        </div>
        <p>
Notice how I've moved the IMyInterface-to-FakeMyInterface mapping to MyClassFixture.
Whenever it's asked to create a new instance of IMyInterface, MyClassFixture makes
sure to add all the Thing instances to the fake before returning it.
</p>
        <p>
Compared to the former Fixture Object of 19 lines, that's another 58% improvement.
Considering some of the APIs I encounter in my daily work, the above example is even
rather simple. The more complex and demanding your SUT's API is, the greater the gain
from using AutoFixture will be, since it's going to figure out much of the routine
stuff for you.
</p>
        <p>
With this post, I hope I have given you a taste of the power that AutoFixture provides.
It allows you to focus on specifying the behavior of your SUT, while taking care of
all the infrastructure tedium that tends to get in the way.
</p>
        <img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=ef5bab91-8b41-4cc5-9e44-f6accd855d64" />
      </body>
      <title>AutoFixture As Fixture Object</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,ef5bab91-8b41-4cc5-9e44-f6accd855d64.aspx</guid>
      <link>http://blog.ploeh.dk/2009/05/15/AutoFixtureAsFixtureObject.aspx</link>
      <pubDate>Fri, 15 May 2009 05:34:00 GMT</pubDate>
      <description>&lt;p&gt;
Dear reader, I hope you are still with me!
&lt;/p&gt;
&lt;p&gt;
After eight posts of &lt;a href="http://autofixture.codeplex.com/"&gt;AutoFixture&lt;/a&gt; feature
walkthroughs, I can't blame you for wondering why this tool might even be relevant
to you. In this post, we'll finally begin to look at how AutoFixture can help you
towards &lt;a href="http://blog.ploeh.dk/2009/01/28/ZeroFrictionTDD.aspx"&gt;Zero-Friction
TDD&lt;/a&gt;!
&lt;/p&gt;
&lt;p&gt;
In an earlier post, I described how the &lt;a href="http://blog.ploeh.dk/2009/03/16/FixtureObject.aspx"&gt;Fixture
Object&lt;/a&gt; pattern can help you greatly reduce the amount of test code that you have
to write. Since AutoFixture was designed to act as a general-purpose Fixture Object,
it can help you reduce the amount of test code even further, letting you focus on &lt;a href="http://blog.ploeh.dk/2009/03/10/SpecificationDrivenDevelopment.aspx"&gt;specifying
the behavior&lt;/a&gt; of your &lt;a href="http://xunitpatterns.com/SUT.html"&gt;SUT&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
In that former post, the original example was this complex test that I will repeat
in it's entirety for your benefit (or horror):
&lt;/p&gt;
&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;\red0\green0\blue255;\red0\green128\blue0;\red163\green21\blue21;}??\fs20 [\cf3 TestMethod\cf0 ]\par ??\cf4 public\cf0  \cf4 void\cf0  NumberSumIsCorrect_Na\u239 ?ve()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf3 Thing\cf0  thing1 = \cf4 new\cf0  \cf3 Thing\cf0 ()\par ??    \{\par ??        Number = 3,\par ??        Text = \cf6 "Anonymous text 1"\par ??\cf0     \};\par ??    \cf3 Thing\cf0  thing2 = \cf4 new\cf0  \cf3 Thing\cf0 ()\par ??    \{\par ??        Number = 6,\par ??        Text = \cf6 "Anonymous text 2"\par ??\cf0     \};\par ??    \cf3 Thing\cf0  thing3 = \cf4 new\cf0  \cf3 Thing\cf0 ()\par ??    \{\par ??        Number = 1,\par ??        Text = \cf6 "Anonymous text 3"\par ??\cf0     \};\par ??\par ??    \cf4 int\cf0  expectedSum = \cf4 new\cf0 [] \{ thing1, thing2, thing3 \}.\par ??        Select(t =&amp;gt; t.Number).Sum();\par ??\par ??    \cf3 IMyInterface\cf0  fake = \cf4 new\cf0  \cf3 FakeMyInterface\cf0 ();\par ??    fake.AddThing(thing1);\par ??    fake.AddThing(thing2);\par ??    fake.AddThing(thing3);\par ??\par ??    \cf3 MyClass\cf0  sut = \cf4 new\cf0  \cf3 MyClass\cf0 (fake);\par ??    \cf5 // Exercise system\par ??\cf0     \cf4 int\cf0  result = sut.CalculateSumOfThings();\par ??    \cf5 // Verify outcome\par ??\cf0     \cf3 Assert\cf0 .AreEqual&amp;lt;\cf4 int\cf0 &amp;gt;(expectedSum, result,\par ??        \cf6 "Sum of things"\cf0 );\par ??    \cf5 // Teardown\par ??\cf0 \}}
--&gt;
&lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;[&lt;span style="color: #2b91af"&gt;TestMethod&lt;/span&gt;]&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; NumberSumIsCorrect_Naïve()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Fixture setup&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Thing&lt;/span&gt; thing1
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Thing&lt;/span&gt;()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Number = 3,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Text = &lt;span style="color: #a31515"&gt;"Anonymous
text 1"&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; };&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Thing&lt;/span&gt; thing2
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Thing&lt;/span&gt;()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Number = 6,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Text = &lt;span style="color: #a31515"&gt;"Anonymous
text 2"&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; };&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Thing&lt;/span&gt; thing3
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Thing&lt;/span&gt;()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Number = 1,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Text = &lt;span style="color: #a31515"&gt;"Anonymous
text 3"&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; };&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;int&lt;/span&gt; expectedSum
= &lt;span style="color: blue"&gt;new&lt;/span&gt;[] { thing1, thing2, thing3 }.&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Select(t =&amp;gt; t.Number).Sum();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;IMyInterface&lt;/span&gt; fake
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;FakeMyInterface&lt;/span&gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; fake.AddThing(thing1);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; fake.AddThing(thing2);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; fake.AddThing(thing3);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt; sut
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt;(fake);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Exercise system&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;int&lt;/span&gt; result
= sut.CalculateSumOfThings();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Verify outcome&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.AreEqual&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt;(expectedSum,
result,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515"&gt;"Sum
of things"&lt;/span&gt;);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Teardown&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
This test consists of 18 lines of code.
&lt;/p&gt;
&lt;p&gt;
Using the Fixture Object pattern, I was able to cut that down to 7 lines of code,
which is a 61% improvement (however, the downside was an additional 19 lines of (reusable)
code for MyClassFixture, so the benefit can only be reaped when you have multiple
tests leveraged by the same Fixture Object. This was all covered in the &lt;a href="http://blog.ploeh.dk/2009/03/16/FixtureObject.aspx"&gt;former
post, to which I will refer you&lt;/a&gt;).
&lt;/p&gt;
&lt;p&gt;
With AutoFixture, we can do much better. Here's a one-off rewrite of the unit test
using AutoFixture:
&lt;/p&gt;
&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;\red0\green0\blue255;\red0\green128\blue0;\red163\green21\blue21;}??\fs20 [\cf3 TestMethod\cf0 ]\par ??\cf4 public\cf0  \cf4 void\cf0  NumberSumIsCorrect_AutoFixture()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf3 Fixture\cf0  fixture = \cf4 new\cf0  \cf3 Fixture\cf0 ();\par ??    \cf3 IMyInterface\cf0  fake = \cf4 new\cf0  \cf3 FakeMyInterface\cf0 ();\par ??    fixture.Register&amp;lt;\cf3 IMyInterface\cf0 &amp;gt;(() =&amp;gt; fake);\par ??\par ??    \cf4 var\cf0  things = fixture.CreateMany&amp;lt;\cf3 Thing\cf0 &amp;gt;().ToList();\par ??    things.ForEach(t =&amp;gt; fake.AddThing(t));\par ??    \cf4 int\cf0  expectedSum = things.Select(t =&amp;gt; t.Number).Sum();\par ??\par ??    \cf3 MyClass\cf0  sut = fixture.CreateAnonymous&amp;lt;\cf3 MyClass\cf0 &amp;gt;();\par ??    \cf5 // Exercise system\par ??\cf0     \cf4 int\cf0  result = sut.CalculateSumOfThings();\par ??    \cf5 // Verify outcome\par ??\cf0     \cf3 Assert\cf0 .AreEqual&amp;lt;\cf4 int\cf0 &amp;gt;(expectedSum, result,\par ??        \cf6 "Sum of things"\cf0 );\par ??    \cf5 // Teardown\par ??\cf0 \}}
--&gt;
&lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;[&lt;span style="color: #2b91af"&gt;TestMethod&lt;/span&gt;]&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; NumberSumIsCorrect_AutoFixture()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Fixture setup&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Fixture&lt;/span&gt; fixture
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Fixture&lt;/span&gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;IMyInterface&lt;/span&gt; fake
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;FakeMyInterface&lt;/span&gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; fixture.Register&amp;lt;&lt;span style="color: #2b91af"&gt;IMyInterface&lt;/span&gt;&amp;gt;(()
=&amp;gt; fake);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; things
= fixture.CreateMany&amp;lt;&lt;span style="color: #2b91af"&gt;Thing&lt;/span&gt;&amp;gt;().ToList();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; things.ForEach(t =&amp;gt; fake.AddThing(t));&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;int&lt;/span&gt; expectedSum
= things.Select(t =&amp;gt; t.Number).Sum();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt; sut
= fixture.CreateAnonymous&amp;lt;&lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt;&amp;gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Exercise system&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;int&lt;/span&gt; result
= sut.CalculateSumOfThings();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Verify outcome&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.AreEqual&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt;(expectedSum,
result,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515"&gt;"Sum
of things"&lt;/span&gt;);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Teardown&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
In this test, I &lt;a href="http://blog.ploeh.dk/2009/04/23/DealingWithTypesWithoutPublicConstructors.aspx"&gt;map
the concrete fake instance to the IMyInterface type&lt;/a&gt; in the fixture object, and
then use its ability to &lt;a href="http://blog.ploeh.dk/2009/05/11/AnonymousSequencesWithAutoFixture.aspx"&gt;create
many anonymous instances with one method call&lt;/a&gt;. Before exercising the SUT, I also
use the fixture instance as a &lt;a href="http://blog.ploeh.dk/2009/02/13/SUTFactory.aspx"&gt;SUT
Factory&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
Apart from AutoFixture (and FakeMyInterface, which is invariant for all variations,
and thus kept out of the comparison), this test stands alone, but still manages to
reduce the number of code lines to 10 lines – a 44% improvement! In my book, that's
already a significant gain in productivity and maintainability, but we can do better!
&lt;/p&gt;
&lt;p&gt;
If we need to test MyClass repeatedly in similar ways, we can move the common code
to a Fixture Object based on AutoFixture, and the test can be refactored to this:
&lt;/p&gt;
&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;\red0\green0\blue255;\red0\green128\blue0;\red163\green21\blue21;}??\fs20 [\cf3 TestMethod\cf0 ]\par ??\cf4 public\cf0  \cf4 void\cf0  NumberSumIsCorrect_DerivedFixture()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf3 MyClassFixture\cf0  fixture = \cf4 new\cf0  \cf3 MyClassFixture\cf0 ();\par ??    fixture.AddManyTo(fixture.Things);\par ??\par ??    \cf4 int\cf0  expectedSum = \par ??        fixture.Things.Select(t =&amp;gt; t.Number).Sum();\par ??    \cf3 MyClass\cf0  sut = fixture.CreateAnonymous&amp;lt;\cf3 MyClass\cf0 &amp;gt;();\par ??    \cf5 // Exercise system\par ??\cf0     \cf4 int\cf0  result = sut.CalculateSumOfThings();\par ??    \cf5 // Verify outcome\par ??\cf0     \cf3 Assert\cf0 .AreEqual&amp;lt;\cf4 int\cf0 &amp;gt;(expectedSum, result,\par ??        \cf6 "Sum of things"\cf0 );\par ??    \cf5 // Teardown\par ??\cf0 \}}
--&gt;
&lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;[&lt;span style="color: #2b91af"&gt;TestMethod&lt;/span&gt;]&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; NumberSumIsCorrect_DerivedFixture()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Fixture setup&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;MyClassFixture&lt;/span&gt; fixture
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MyClassFixture&lt;/span&gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; fixture.AddManyTo(fixture.Things);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;int&lt;/span&gt; expectedSum
= &lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fixture.Things.Select(t =&amp;gt; t.Number).Sum();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt; sut
= fixture.CreateAnonymous&amp;lt;&lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt;&amp;gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Exercise system&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;int&lt;/span&gt; result
= sut.CalculateSumOfThings();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Verify outcome&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.AreEqual&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt;(expectedSum,
result,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515"&gt;"Sum
of things"&lt;/span&gt;);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Teardown&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
Now we are back at 7 lines of code, which is on par with the original Fixture Object-based
test, but now MyClassFixture is reduced to 8 lines of code:
&lt;/p&gt;
&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;}??\fs20 \cf1 internal\cf0  \cf1 class\cf0  \cf4 MyClassFixture\cf0  : \cf4 Fixture\par ??\cf0 \{\par ??    \cf1 internal\cf0  MyClassFixture()\par ??    \{\par ??        \cf1 this\cf0 .Things = \cf1 new\cf0  \cf4 List\cf0 &amp;lt;\cf4 Thing\cf0 &amp;gt;();\par ??        \cf1 this\cf0 .Register&amp;lt;\cf4 IMyInterface\cf0 &amp;gt;(() =&amp;gt;\par ??            \{\par ??                \cf1 var\cf0  fake = \cf1 new\cf0  \cf4 FakeMyInterface\cf0 ();\par ??                \cf1 this\cf0 .Things.ToList().ForEach(t =&amp;gt;\par ??                    fake.AddThing(t));\par ??                \cf1 return\cf0  fake;\par ??            \});\par ??    \}\par ??\par ??    \cf1 internal\cf0  \cf4 IList\cf0 &amp;lt;\cf4 Thing\cf0 &amp;gt; Things \{ \cf1 get\cf0 ; \cf1 private\cf0  \cf1 set\cf0 ; \}\par ??\}}
--&gt;
&lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;internal&lt;/span&gt; &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MyClassFixture&lt;/span&gt; : &lt;span style="color: #2b91af"&gt;Fixture&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;internal&lt;/span&gt; MyClassFixture()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;this&lt;/span&gt;.Things
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Thing&lt;/span&gt;&amp;gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;this&lt;/span&gt;.Register&amp;lt;&lt;span style="color: #2b91af"&gt;IMyInterface&lt;/span&gt;&amp;gt;(()
=&amp;gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; fake
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;FakeMyInterface&lt;/span&gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;this&lt;/span&gt;.Things.ToList().ForEach(t
=&amp;gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fake.AddThing(t));&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;return&lt;/span&gt; fake;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; });&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;internal&lt;/span&gt; &lt;span style="color: #2b91af"&gt;IList&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Thing&lt;/span&gt;&amp;gt;
Things { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;set&lt;/span&gt;;
}&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
Notice how I've moved the IMyInterface-to-FakeMyInterface mapping to MyClassFixture.
Whenever it's asked to create a new instance of IMyInterface, MyClassFixture makes
sure to add all the Thing instances to the fake before returning it.
&lt;/p&gt;
&lt;p&gt;
Compared to the former Fixture Object of 19 lines, that's another 58% improvement.
Considering some of the APIs I encounter in my daily work, the above example is even
rather simple. The more complex and demanding your SUT's API is, the greater the gain
from using AutoFixture will be, since it's going to figure out much of the routine
stuff for you.
&lt;/p&gt;
&lt;p&gt;
With this post, I hope I have given you a taste of the power that AutoFixture provides.
It allows you to focus on specifying the behavior of your SUT, while taking care of
all the infrastructure tedium that tends to get in the way.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=ef5bab91-8b41-4cc5-9e44-f6accd855d64" /&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,ef5bab91-8b41-4cc5-9e44-f6accd855d64.aspx</comments>
      <category>AutoFixture</category>
      <category>Productivity</category>
      <category>Unit Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=2104ff91-b287-4c7c-a436-75d3c7a81f7a</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,2104ff91-b287-4c7c-a436-75d3c7a81f7a.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,2104ff91-b287-4c7c-a436-75d3c7a81f7a.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=2104ff91-b287-4c7c-a436-75d3c7a81f7a</wfw:commentRss>
      <slash:comments>2</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
It gives me great pleasure to announce my latest project: <a href="http://autofixture.codeplex.com/">AutoFixture</a>!
</p>
        <p>
What is AutoFixture?
</p>
        <p>
In essence, AutoFixture is a library that creates <a href="http://blogs.msdn.com/ploeh/archive/2008/11/17/anonymous-variables.aspx">Anonymous
Variables</a> for you when you write unit tests. The intention is that it should enhance
your productivity when you do Test-Driven Development – as it has done mine.
</p>
        <p>
Instead of using mental resources on creating Anonymous Variables, AutoFixture can
do it for you. By default, it uses <a href="http://blog.ploeh.dk/2009/03/05/ConstrainedNonDeterminism.aspx">Constrained
Non-Determinism</a>, but you can configure it to behave differently if you wish.
</p>
        <p>
Here’s a very basic example:
</p>
        <!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;\red0\green0\blue255;\red0\green128\blue0;\red163\green21\blue21;}??\fs20 [\cf3 TestMethod\cf0 ]\par ??\cf4 public\cf0  \cf4 void\cf0  IntroductoryTest()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf3 Fixture\cf0  fixture = \cf4 new\cf0  \cf3 Fixture\cf0 ();\par ??\par ??    \cf4 int\cf0  expectedNumber = fixture.CreateAnonymous&lt;\cf4 int\cf0 &gt;();\par ??    \cf3 MyClass\cf0  sut = fixture.CreateAnonymous&lt;\cf3 MyClass\cf0 &gt;();\par ??    \cf5 // Exercise system\par ??\cf0     \cf4 int\cf0  result = sut.Echo(expectedNumber);\par ??    \cf5 // Verify outcome\par ??\cf0     \cf3 Assert\cf0 .AreEqual&lt;\cf4 int\cf0 &gt;(expectedNumber, result, \cf6 "Echo"\cf0 );\par ??    \cf5 // Teardown\par ??\cf0 \}}
-->
        <div style="font-size: 10pt; background: white; color: black; font-family: courier new">
          <pre style="margin: 0px">[<span style="color: #2b91af">TestMethod</span>]</pre>
          <pre style="margin: 0px">
            <span style="color: blue">public</span>
            <span style="color: blue">void</span> IntroductoryTest()</pre>
          <pre style="margin: 0px">{</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Fixture setup</span></pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">Fixture</span> fixture
= <span style="color: blue">new</span><span style="color: #2b91af">Fixture</span>();</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: blue">int</span> expectedNumber
= fixture.CreateAnonymous&lt;<span style="color: blue">int</span>&gt;();</pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">MyClass</span> sut
= fixture.CreateAnonymous&lt;<span style="color: #2b91af">MyClass</span>&gt;();</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Exercise system</span></pre>
          <pre style="margin: 0px">    <span style="color: blue">int</span> result
= sut.Echo(expectedNumber);</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Verify outcome</span></pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">Assert</span>.AreEqual&lt;<span style="color: blue">int</span>&gt;(expectedNumber,
result, <span style="color: #a31515">"Echo"</span>);</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Teardown</span></pre>
          <pre style="margin: 0px">}</pre>
        </div>
        <p>
The Fixture class is your main entry point to AutoFixture. You can use it as is, customize
it, or derive from it as you please; it makes a great base class for a <a href="http://blog.ploeh.dk/2009/03/16/FixtureObject.aspx">Fixture
Object</a>. 
</p>
        <p>
The <em>expectedNumber</em> variable may be an <a href="http://blog.ploeh.dk/2009/03/11/ExplicitExpectations.aspx">Explicit
Expectation</a>, but its value is Anonymous, so instead of coming up with a number
ourselves, we can let the CreateAnonymous&lt;T&gt; method do it for us.
</p>
        <p>
This method can create instances of most CLR types as long as they have a public constructor
(it uses Reflection), but for many primitive types (like Int32), it has specific,
customizable algorithms for creating values using Constrained Non-Determinism.
</p>
        <p>
When creating the <a href="http://xunitpatterns.com/SUT.html">SUT</a>, we can also
use Fixture as an excellent <a href="http://blog.ploeh.dk/2009/02/13/SUTFactory.aspx">SUT
Factory</a>. Since it will do whatever it can to create an instance of the type you
ask for, it is pretty robust if you decide to refactor the SUT’s constructor.
</p>
        <p>
The above example only hints at what AutoFixture can do. Since the example is very
simple, it may be hard to immediately understand its value, so in future posts I will
expand on specific AutoFixture features and principles.
</p>
        <p>
Getting started with AutoFixture is as simple as downloading it from <a href="http://autofixture.codeplex.com/">CodePlex</a> and
referencing the assembly in Visual Studio.
</p>
        <img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=2104ff91-b287-4c7c-a436-75d3c7a81f7a" />
      </body>
      <title>Announcing: AutoFixture</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,2104ff91-b287-4c7c-a436-75d3c7a81f7a.aspx</guid>
      <link>http://blog.ploeh.dk/2009/03/22/AnnouncingAutoFixture.aspx</link>
      <pubDate>Sun, 22 Mar 2009 06:50:54 GMT</pubDate>
      <description>&lt;p&gt;
It gives me great pleasure to announce my latest project: &lt;a href="http://autofixture.codeplex.com/"&gt;AutoFixture&lt;/a&gt;!
&lt;/p&gt;
&lt;p&gt;
What is AutoFixture?
&lt;/p&gt;
&lt;p&gt;
In essence, AutoFixture is a library that creates &lt;a href="http://blogs.msdn.com/ploeh/archive/2008/11/17/anonymous-variables.aspx"&gt;Anonymous
Variables&lt;/a&gt; for you when you write unit tests. The intention is that it should enhance
your productivity when you do Test-Driven Development – as it has done mine.
&lt;/p&gt;
&lt;p&gt;
Instead of using mental resources on creating Anonymous Variables, AutoFixture can
do it for you. By default, it uses &lt;a href="http://blog.ploeh.dk/2009/03/05/ConstrainedNonDeterminism.aspx"&gt;Constrained
Non-Determinism&lt;/a&gt;, but you can configure it to behave differently if you wish.
&lt;/p&gt;
&lt;p&gt;
Here’s a very basic example:
&lt;/p&gt;
&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;\red0\green0\blue255;\red0\green128\blue0;\red163\green21\blue21;}??\fs20 [\cf3 TestMethod\cf0 ]\par ??\cf4 public\cf0  \cf4 void\cf0  IntroductoryTest()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf3 Fixture\cf0  fixture = \cf4 new\cf0  \cf3 Fixture\cf0 ();\par ??\par ??    \cf4 int\cf0  expectedNumber = fixture.CreateAnonymous&amp;lt;\cf4 int\cf0 &amp;gt;();\par ??    \cf3 MyClass\cf0  sut = fixture.CreateAnonymous&amp;lt;\cf3 MyClass\cf0 &amp;gt;();\par ??    \cf5 // Exercise system\par ??\cf0     \cf4 int\cf0  result = sut.Echo(expectedNumber);\par ??    \cf5 // Verify outcome\par ??\cf0     \cf3 Assert\cf0 .AreEqual&amp;lt;\cf4 int\cf0 &amp;gt;(expectedNumber, result, \cf6 "Echo"\cf0 );\par ??    \cf5 // Teardown\par ??\cf0 \}}
--&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: courier new"&gt;&lt;pre style="margin: 0px"&gt;[&lt;span style="color: #2b91af"&gt;TestMethod&lt;/span&gt;]&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; IntroductoryTest()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Fixture setup&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Fixture&lt;/span&gt; fixture
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Fixture&lt;/span&gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;int&lt;/span&gt; expectedNumber
= fixture.CreateAnonymous&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt; sut
= fixture.CreateAnonymous&amp;lt;&lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt;&amp;gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Exercise system&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;int&lt;/span&gt; result
= sut.Echo(expectedNumber);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Verify outcome&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.AreEqual&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt;(expectedNumber,
result, &lt;span style="color: #a31515"&gt;"Echo"&lt;/span&gt;);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Teardown&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
The Fixture class is your main entry point to AutoFixture. You can use it as is, customize
it, or derive from it as you please; it makes a great base class for a &lt;a href="http://blog.ploeh.dk/2009/03/16/FixtureObject.aspx"&gt;Fixture
Object&lt;/a&gt;. 
&lt;/p&gt;
&lt;p&gt;
The &lt;em&gt;expectedNumber&lt;/em&gt; variable may be an &lt;a href="http://blog.ploeh.dk/2009/03/11/ExplicitExpectations.aspx"&gt;Explicit
Expectation&lt;/a&gt;, but its value is Anonymous, so instead of coming up with a number
ourselves, we can let the CreateAnonymous&amp;lt;T&amp;gt; method do it for us.
&lt;/p&gt;
&lt;p&gt;
This method can create instances of most CLR types as long as they have a public constructor
(it uses Reflection), but for many primitive types (like Int32), it has specific,
customizable algorithms for creating values using Constrained Non-Determinism.
&lt;/p&gt;
&lt;p&gt;
When creating the &lt;a href="http://xunitpatterns.com/SUT.html"&gt;SUT&lt;/a&gt;, we can also
use Fixture as an excellent &lt;a href="http://blog.ploeh.dk/2009/02/13/SUTFactory.aspx"&gt;SUT
Factory&lt;/a&gt;. Since it will do whatever it can to create an instance of the type you
ask for, it is pretty robust if you decide to refactor the SUT’s constructor.
&lt;/p&gt;
&lt;p&gt;
The above example only hints at what AutoFixture can do. Since the example is very
simple, it may be hard to immediately understand its value, so in future posts I will
expand on specific AutoFixture features and principles.
&lt;/p&gt;
&lt;p&gt;
Getting started with AutoFixture is as simple as downloading it from &lt;a href="http://autofixture.codeplex.com/"&gt;CodePlex&lt;/a&gt; and
referencing the assembly in Visual Studio.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=2104ff91-b287-4c7c-a436-75d3c7a81f7a" /&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,2104ff91-b287-4c7c-a436-75d3c7a81f7a.aspx</comments>
      <category>AutoFixture</category>
      <category>Productivity</category>
      <category>Unit Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=a6ac8eab-1593-4a08-ae66-f32599a51c09</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,a6ac8eab-1593-4a08-ae66-f32599a51c09.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,a6ac8eab-1593-4a08-ae66-f32599a51c09.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=a6ac8eab-1593-4a08-ae66-f32599a51c09</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
(A <a href="http://blog.ploeh.dk/2009/01/28/ZeroFrictionTDD.aspx">Zero-Friction TDD</a> post)
</p>
        <p>
For a simple API, setting up the <a href="http://xunitpatterns.com/test%20fixture%20-%20xUnit.html">Fixture</a> may
be as simple as creating a new instance of the <a href="http://xunitpatterns.com/SUT.html">SUT</a>,
and possibly any <a href="http://blog.ploeh.dk/2009/03/11/ExplicitExpectations.aspx">Expected</a> or <a href="http://blogs.msdn.com/ploeh/archive/2008/11/17/anonymous-variables.aspx">Anonymous
Variables</a>. On the other hand, for a complex API, setting up the fixture may require
quite a bit of (potentially repetitive) code.
</p>
        <p>
Since the DRY principle also applies to test code, it quickly becomes necessary to
create test-specific helper methods and other <a href="http://xunitpatterns.com/Test%20Utility%20Method.html#SUT%20Encapsulation%20Method">SUT
API Encapsulation</a>, and I've found that instead of creating a more or less unplanned
set of disconnected helper methods, it's much cleaner (and, not to mention, much more
object-oriented) to create a single object that represents the Fixture, and attach
the helper methods to this object.
</p>
        <p>
Let's look at an example.
</p>
        <p>
Here's a unit test with a complex <a href="http://xunitpatterns.com/fixture%20setup.html">Fixture
Setup</a>:
</p>
        <!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;\red0\green0\blue255;\red0\green128\blue0;\red163\green21\blue21;}??\fs20 [\cf3 TestMethod\cf0 ]\par ??\cf4 public\cf0  \cf4 void\cf0  NumberSumIsCorrect_Na\u239 ?ve()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf3 Thing\cf0  thing1 = \cf4 new\cf0  \cf3 Thing\cf0 ()\par ??    \{\par ??        Number = 3,\par ??        Text = \cf6 "Anonymous text 1"\par ??\cf0     \};\par ??    \cf3 Thing\cf0  thing2 = \cf4 new\cf0  \cf3 Thing\cf0 ()\par ??    \{\par ??        Number = 6,\par ??        Text = \cf6 "Anonymous text 2"\par ??\cf0     \};\par ??    \cf3 Thing\cf0  thing3 = \cf4 new\cf0  \cf3 Thing\cf0 ()\par ??    \{\par ??        Number = 1,\par ??        Text = \cf6 "Anonymous text 3"\par ??\cf0     \};\par ??\par ??    \cf4 int\cf0  expectedSum = \cf4 new\cf0 [] \{ thing1, thing2, thing3 \}.\par ??        Select(t =&gt; t.Number).Sum();\par ??\par ??    \cf3 IMyInterface\cf0  fake = \cf4 new\cf0  \cf3 FakeMyInterface\cf0 ();\par ??    fake.AddThing(thing1);\par ??    fake.AddThing(thing2);\par ??    fake.AddThing(thing3);\par ??\par ??    \cf3 MyClass\cf0  sut = \cf4 new\cf0  \cf3 MyClass\cf0 (fake);\par ??    \cf5 // Exercise system\par ??\cf0     \cf4 int\cf0  result = sut.CalculateSumOfThings();\par ??    \cf5 // Verify outcome\par ??\cf0     \cf3 Assert\cf0 .AreEqual&lt;\cf4 int\cf0 &gt;(expectedSum, result,\par ??        \cf6 "Sum of things"\cf0 );\par ??    \cf5 // Teardown\par ??\cf0 \}}
-->
        <div style="font-size: 10pt; background: white; color: black; font-family: courier new">
          <pre style="margin: 0px">[<span style="color: #2b91af">TestMethod</span>]</pre>
          <pre style="margin: 0px">
            <span style="color: blue">public</span>
            <span style="color: blue">void</span> NumberSumIsCorrect_Naïve()</pre>
          <pre style="margin: 0px">{</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Fixture setup</span></pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">Thing</span> thing1
= <span style="color: blue">new</span><span style="color: #2b91af">Thing</span>()</pre>
          <pre style="margin: 0px">    {</pre>
          <pre style="margin: 0px">        Number = 3,</pre>
          <pre style="margin: 0px">        Text = <span style="color: #a31515">"Anonymous
text 1"</span></pre>
          <pre style="margin: 0px">    };</pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">Thing</span> thing2
= <span style="color: blue">new</span><span style="color: #2b91af">Thing</span>()</pre>
          <pre style="margin: 0px">    {</pre>
          <pre style="margin: 0px">        Number = 6,</pre>
          <pre style="margin: 0px">        Text = <span style="color: #a31515">"Anonymous
text 2"</span></pre>
          <pre style="margin: 0px">    };</pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">Thing</span> thing3
= <span style="color: blue">new</span><span style="color: #2b91af">Thing</span>()</pre>
          <pre style="margin: 0px">    {</pre>
          <pre style="margin: 0px">        Number = 1,</pre>
          <pre style="margin: 0px">        Text = <span style="color: #a31515">"Anonymous
text 3"</span></pre>
          <pre style="margin: 0px">    };</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: blue">int</span> expectedSum
= <span style="color: blue">new</span>[] { thing1, thing2, thing3 }.</pre>
          <pre style="margin: 0px">        Select(t =&gt; t.Number).Sum();</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">IMyInterface</span> fake
= <span style="color: blue">new</span><span style="color: #2b91af">FakeMyInterface</span>();</pre>
          <pre style="margin: 0px">    fake.AddThing(thing1);</pre>
          <pre style="margin: 0px">    fake.AddThing(thing2);</pre>
          <pre style="margin: 0px">    fake.AddThing(thing3);</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">MyClass</span> sut
= <span style="color: blue">new</span><span style="color: #2b91af">MyClass</span>(fake);</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Exercise system</span></pre>
          <pre style="margin: 0px">    <span style="color: blue">int</span> result
= sut.CalculateSumOfThings();</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Verify outcome</span></pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">Assert</span>.AreEqual&lt;<span style="color: blue">int</span>&gt;(expectedSum,
result,</pre>
          <pre style="margin: 0px">        <span style="color: #a31515">"Sum
of things"</span>);</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Teardown</span></pre>
          <pre style="margin: 0px">}</pre>
        </div>
        <p>
If this was a truly one-off test and you know with certainty that there's going to
be no other tests just remotely similar to this one, just hard-coding the entire Fixture
Setup inline is in order, but as soon as the need for similar tests arises, this approach
leads to repetitive code, and hence unmaintainable tests.
</p>
        <p>
The more repetitive code that can be delegated to helper methods the better. A common
refactoring of the previous test might then look something like this:
</p>
        <!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;\red0\green0\blue255;\red0\green128\blue0;\red163\green21\blue21;}??\fs20 [\cf3 TestMethod\cf0 ]\par ??\cf4 public\cf0  \cf4 void\cf0  NumberSumIsCorrect_Helpers()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf3 Thing\cf0  thing1 = \cf3 MyClassTest\cf0 .CreateAnonymousThing();\par ??    \cf3 Thing\cf0  thing2 = \cf3 MyClassTest\cf0 .CreateAnonymousThing();\par ??    \cf3 Thing\cf0  thing3 = \cf3 MyClassTest\cf0 .CreateAnonymousThing();\par ??    \cf3 Thing\cf0 [] things = \cf4 new\cf0 [] \{ thing1, thing2, thing3 \};\par ??\par ??    \cf4 int\cf0  expectedSum = things.Select(t =&gt; t.Number).Sum();\par ??\par ??    \cf3 IMyInterface\cf0  fake = \cf4 new\cf0  \cf3 FakeMyInterface\cf0 ();\par ??    \cf3 MyClassTest\cf0 .AddThingsToMyInterface(fake, things);\par ??\par ??    \cf3 MyClass\cf0  sut = \cf4 new\cf0  \cf3 MyClass\cf0 (fake);\par ??    \cf5 // Exercise system\par ??\cf0     \cf4 int\cf0  result = sut.CalculateSumOfThings();\par ??    \cf5 // Verify outcome\par ??\cf0     \cf3 Assert\cf0 .AreEqual&lt;\cf4 int\cf0 &gt;(expectedSum, result,\par ??        \cf6 "Sum of things"\cf0 );\par ??    \cf5 // Teardown\par ??\cf0 \}}
-->
        <div style="font-size: 10pt; background: white; color: black; font-family: courier new">
          <pre style="margin: 0px">[<span style="color: #2b91af">TestMethod</span>]</pre>
          <pre style="margin: 0px">
            <span style="color: blue">public</span>
            <span style="color: blue">void</span> NumberSumIsCorrect_Helpers()</pre>
          <pre style="margin: 0px">{</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Fixture setup</span></pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">Thing</span> thing1
= <span style="color: #2b91af">MyClassTest</span>.CreateAnonymousThing();</pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">Thing</span> thing2
= <span style="color: #2b91af">MyClassTest</span>.CreateAnonymousThing();</pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">Thing</span> thing3
= <span style="color: #2b91af">MyClassTest</span>.CreateAnonymousThing();</pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">Thing</span>[]
things = <span style="color: blue">new</span>[] { thing1, thing2, thing3 };</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: blue">int</span> expectedSum
= things.Select(t =&gt; t.Number).Sum();</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">IMyInterface</span> fake
= <span style="color: blue">new</span><span style="color: #2b91af">FakeMyInterface</span>();</pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">MyClassTest</span>.AddThingsToMyInterface(fake,
things);</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">MyClass</span> sut
= <span style="color: blue">new</span><span style="color: #2b91af">MyClass</span>(fake);</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Exercise system</span></pre>
          <pre style="margin: 0px">    <span style="color: blue">int</span> result
= sut.CalculateSumOfThings();</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Verify outcome</span></pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">Assert</span>.AreEqual&lt;<span style="color: blue">int</span>&gt;(expectedSum,
result,</pre>
          <pre style="margin: 0px">        <span style="color: #a31515">"Sum
of things"</span>);</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Teardown</span></pre>
          <pre style="margin: 0px">}</pre>
        </div>
        <p>
While this is better, the helper methods are static methods, so it's necessary to
pass too much state around. The array of Things and the fake is both needed in the
test itself, as well as in the AddThingsToMyInterface helper method.
</p>
        <p>
By moving and refactoring the helper methods to a new class that represents the Fixture,
the test code becomes both more reusable and more readable.
</p>
        <!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;\red0\green0\blue255;\red0\green128\blue0;\red163\green21\blue21;}??\fs20 [\cf3 TestMethod\cf0 ]\par ??\cf4 public\cf0  \cf4 void\cf0  NumberSumIsCorrect_FixtureObject()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf3 MyClassFixture\cf0  fixture = \cf4 new\cf0  \cf3 MyClassFixture\cf0 ();\par ??    fixture.AddAnonymousThings();\par ??\par ??    \cf4 int\cf0  expectedSum = \par ??        fixture.Things.Select(t =&gt; t.Number).Sum();\par ??    \cf3 MyClass\cf0  sut = fixture.CreateSut();\par ??    \cf5 // Exercise system\par ??\cf0     \cf4 int\cf0  result = sut.CalculateSumOfThings();\par ??    \cf5 // Verify outcome\par ??\cf0     \cf3 Assert\cf0 .AreEqual&lt;\cf4 int\cf0 &gt;(expectedSum, result,\par ??        \cf6 "Sum of things"\cf0 );\par ??    \cf5 // Teardown\par ??\cf0 \}}
-->
        <div style="font-size: 10pt; background: white; color: black; font-family: courier new">
          <pre style="margin: 0px">[<span style="color: #2b91af">TestMethod</span>]</pre>
          <pre style="margin: 0px">
            <span style="color: blue">public</span>
            <span style="color: blue">void</span> NumberSumIsCorrect_FixtureObject()</pre>
          <pre style="margin: 0px">{</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Fixture setup</span></pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">MyClassFixture</span> fixture
= <span style="color: blue">new</span><span style="color: #2b91af">MyClassFixture</span>();</pre>
          <pre style="margin: 0px">    fixture.AddAnonymousThings();</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: blue">int</span> expectedSum
= </pre>
          <pre style="margin: 0px">        fixture.Things.Select(t =&gt; t.Number).Sum();</pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">MyClass</span> sut
= fixture.CreateSut();</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Exercise system</span></pre>
          <pre style="margin: 0px">    <span style="color: blue">int</span> result
= sut.CalculateSumOfThings();</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Verify outcome</span></pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">Assert</span>.AreEqual&lt;<span style="color: blue">int</span>&gt;(expectedSum,
result,</pre>
          <pre style="margin: 0px">        <span style="color: #a31515">"Sum
of things"</span>);</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Teardown</span></pre>
          <pre style="margin: 0px">}</pre>
        </div>
        <p>
The MyClassFixture instance now holds the state of the Fixture, so there's much less
need to pass around as much data as before. The set of Things is now contained within
the Fixture object itself, and the fake has totally disappeared from the test; it's
still present, but now encapsulated within MyClassFixture.
</p>
        <!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;}??\fs20 \cf1 internal\cf0  \cf1 class\cf0  \cf4 MyClassFixture\par ??\cf0 \{\par ??    \cf1 public\cf0  MyClassFixture()\par ??    \{\par ??        \cf1 this\cf0 .Fake = \cf1 new\cf0  \cf4 FakeMyInterface\cf0 ();\par ??        \cf1 this\cf0 .Things = \cf1 new\cf0  \cf4 List\cf0 &lt;\cf4 Thing\cf0 &gt;();\par ??    \}\par ??\par ??    \cf1 internal\cf0  \cf4 FakeMyInterface\cf0  Fake \{ \cf1 get\cf0 ; \cf1 private\cf0  \cf1 set\cf0 ; \}\par ??\par ??    \cf1 internal\cf0  \cf4 IList\cf0 &lt;\cf4 Thing\cf0 &gt; Things \{ \cf1 get\cf0 ; \cf1 private\cf0  \cf1 set\cf0 ; \}\par ??\par ??    \cf1 internal\cf0  \cf1 void\cf0  AddAnonymousThings()\par ??    \{\par ??        \cf1 int\cf0  many = 3;\par ??        \cf1 for\cf0  (\cf1 int\cf0  i = 0; i &lt; many; i++)\par ??        \{\par ??            \cf4 Thing\cf0  t = \cf1 this\cf0 .CreateAnonymousThing();\par ??            \cf1 this\cf0 .Things.Add(t);\par ??            \cf1 this\cf0 .Fake.AddThing(t);\par ??        \}\par ??    \}\par ??\par ??    \cf1 internal\cf0  \cf4 MyClass\cf0  CreateSut()\par ??    \{\par ??        \cf1 return\cf0  \cf1 new\cf0  \cf4 MyClass\cf0 (\cf1 this\cf0 .Fake);\par ??    \}\par ??\par ??    \cf1 private\cf0  \cf4 Thing\cf0  CreateAnonymousThing()\par ??    \{\par ??        \cf4 Thing\cf0  t = \cf1 new\cf0  \cf4 Thing\cf0 ();\par ??        t.Number = \cf1 this\cf0 .Things.Count + 1;\par ??        t.Text = \cf4 Guid\cf0 .NewGuid().ToString();\par ??        \cf1 return\cf0  t;\par ??    \}\par ??\}}
-->
        <div style="font-size: 10pt; background: white; color: black; font-family: courier new">
          <pre style="margin: 0px">
            <span style="color: blue">internal</span>
            <span style="color: blue">class</span>
            <span style="color: #2b91af">MyClassFixture</span>
          </pre>
          <pre style="margin: 0px">{</pre>
          <pre style="margin: 0px">    <span style="color: blue">public</span> MyClassFixture()</pre>
          <pre style="margin: 0px">    {</pre>
          <pre style="margin: 0px">        <span style="color: blue">this</span>.Fake
= <span style="color: blue">new</span><span style="color: #2b91af">FakeMyInterface</span>();</pre>
          <pre style="margin: 0px">        <span style="color: blue">this</span>.Things
= <span style="color: blue">new</span><span style="color: #2b91af">List</span>&lt;<span style="color: #2b91af">Thing</span>&gt;();</pre>
          <pre style="margin: 0px">    }</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: blue">internal</span><span style="color: #2b91af">FakeMyInterface</span> Fake
{ <span style="color: blue">get</span>; <span style="color: blue">private</span><span style="color: blue">set</span>;
}</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: blue">internal</span><span style="color: #2b91af">IList</span>&lt;<span style="color: #2b91af">Thing</span>&gt;
Things { <span style="color: blue">get</span>; <span style="color: blue">private</span><span style="color: blue">set</span>;
}</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: blue">internal</span><span style="color: blue">void</span> AddAnonymousThings()</pre>
          <pre style="margin: 0px">    {</pre>
          <pre style="margin: 0px">        <span style="color: blue">int</span> many
= 3;</pre>
          <pre style="margin: 0px">        <span style="color: blue">for</span> (<span style="color: blue">int</span> i
= 0; i &lt; many; i++)</pre>
          <pre style="margin: 0px">        {</pre>
          <pre style="margin: 0px">            <span style="color: #2b91af">Thing</span> t
= <span style="color: blue">this</span>.CreateAnonymousThing();</pre>
          <pre style="margin: 0px">            <span style="color: blue">this</span>.Things.Add(t);</pre>
          <pre style="margin: 0px">            <span style="color: blue">this</span>.Fake.AddThing(t);</pre>
          <pre style="margin: 0px">        }</pre>
          <pre style="margin: 0px">    }</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: blue">internal</span><span style="color: #2b91af">MyClass</span> CreateSut()</pre>
          <pre style="margin: 0px">    {</pre>
          <pre style="margin: 0px">        <span style="color: blue">return</span><span style="color: blue">new</span><span style="color: #2b91af">MyClass</span>(<span style="color: blue">this</span>.Fake);</pre>
          <pre style="margin: 0px">    }</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: blue">private</span><span style="color: #2b91af">Thing</span> CreateAnonymousThing()</pre>
          <pre style="margin: 0px">    {</pre>
          <pre style="margin: 0px">        <span style="color: #2b91af">Thing</span> t
= <span style="color: blue">new</span><span style="color: #2b91af">Thing</span>();</pre>
          <pre style="margin: 0px">        t.Number = <span style="color: blue">this</span>.Things.Count
+ 1;</pre>
          <pre style="margin: 0px">        t.Text = <span style="color: #2b91af">Guid</span>.NewGuid().ToString();</pre>
          <pre style="margin: 0px">        <span style="color: blue">return</span> t;</pre>
          <pre style="margin: 0px">    }</pre>
          <pre style="margin: 0px">}</pre>
        </div>
        <p>
The CreateAnonymousThing method uses <a href="http://blog.ploeh.dk/2009/03/05/ConstrainedNonDeterminism.aspx">Constrained
Non-Determinism</a> to create unique Thing instances. The AddAnonymousThings method
uses <a href="http://blogs.msdn.com/ploeh/archive/2008/12/08/3-is-many.aspx">3 as
an equivalence of many</a>, and the CreateSut method acts as a <a href="http://blog.ploeh.dk/2009/02/13/SUTFactory.aspx">SUT
Factory</a>.
</p>
        <p>
This is both more reusable and more expressive than a collection of disjointed static
helper methods.
</p>
        <p>
Whenever I begin to feel that setting up a Test Fixture is becoming too cumbersome,
Fixture Object is the first pattern I consider.
</p>
        <img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=a6ac8eab-1593-4a08-ae66-f32599a51c09" />
      </body>
      <title>Fixture Object</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,a6ac8eab-1593-4a08-ae66-f32599a51c09.aspx</guid>
      <link>http://blog.ploeh.dk/2009/03/16/FixtureObject.aspx</link>
      <pubDate>Mon, 16 Mar 2009 20:13:29 GMT</pubDate>
      <description>&lt;p&gt;
(A &lt;a href="http://blog.ploeh.dk/2009/01/28/ZeroFrictionTDD.aspx"&gt;Zero-Friction TDD&lt;/a&gt; post)
&lt;/p&gt;
&lt;p&gt;
For a simple API, setting up the &lt;a href="http://xunitpatterns.com/test%20fixture%20-%20xUnit.html"&gt;Fixture&lt;/a&gt; may
be as simple as creating a new instance of the &lt;a href="http://xunitpatterns.com/SUT.html"&gt;SUT&lt;/a&gt;,
and possibly any &lt;a href="http://blog.ploeh.dk/2009/03/11/ExplicitExpectations.aspx"&gt;Expected&lt;/a&gt; or &lt;a href="http://blogs.msdn.com/ploeh/archive/2008/11/17/anonymous-variables.aspx"&gt;Anonymous
Variables&lt;/a&gt;. On the other hand, for a complex API, setting up the fixture may require
quite a bit of (potentially repetitive) code.
&lt;/p&gt;
&lt;p&gt;
Since the DRY principle also applies to test code, it quickly becomes necessary to
create test-specific helper methods and other &lt;a href="http://xunitpatterns.com/Test%20Utility%20Method.html#SUT%20Encapsulation%20Method"&gt;SUT
API Encapsulation&lt;/a&gt;, and I've found that instead of creating a more or less unplanned
set of disconnected helper methods, it's much cleaner (and, not to mention, much more
object-oriented) to create a single object that represents the Fixture, and attach
the helper methods to this object.
&lt;/p&gt;
&lt;p&gt;
Let's look at an example.
&lt;/p&gt;
&lt;p&gt;
Here's a unit test with a complex &lt;a href="http://xunitpatterns.com/fixture%20setup.html"&gt;Fixture
Setup&lt;/a&gt;:
&lt;/p&gt;
&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;\red0\green0\blue255;\red0\green128\blue0;\red163\green21\blue21;}??\fs20 [\cf3 TestMethod\cf0 ]\par ??\cf4 public\cf0  \cf4 void\cf0  NumberSumIsCorrect_Na\u239 ?ve()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf3 Thing\cf0  thing1 = \cf4 new\cf0  \cf3 Thing\cf0 ()\par ??    \{\par ??        Number = 3,\par ??        Text = \cf6 "Anonymous text 1"\par ??\cf0     \};\par ??    \cf3 Thing\cf0  thing2 = \cf4 new\cf0  \cf3 Thing\cf0 ()\par ??    \{\par ??        Number = 6,\par ??        Text = \cf6 "Anonymous text 2"\par ??\cf0     \};\par ??    \cf3 Thing\cf0  thing3 = \cf4 new\cf0  \cf3 Thing\cf0 ()\par ??    \{\par ??        Number = 1,\par ??        Text = \cf6 "Anonymous text 3"\par ??\cf0     \};\par ??\par ??    \cf4 int\cf0  expectedSum = \cf4 new\cf0 [] \{ thing1, thing2, thing3 \}.\par ??        Select(t =&amp;gt; t.Number).Sum();\par ??\par ??    \cf3 IMyInterface\cf0  fake = \cf4 new\cf0  \cf3 FakeMyInterface\cf0 ();\par ??    fake.AddThing(thing1);\par ??    fake.AddThing(thing2);\par ??    fake.AddThing(thing3);\par ??\par ??    \cf3 MyClass\cf0  sut = \cf4 new\cf0  \cf3 MyClass\cf0 (fake);\par ??    \cf5 // Exercise system\par ??\cf0     \cf4 int\cf0  result = sut.CalculateSumOfThings();\par ??    \cf5 // Verify outcome\par ??\cf0     \cf3 Assert\cf0 .AreEqual&amp;lt;\cf4 int\cf0 &amp;gt;(expectedSum, result,\par ??        \cf6 "Sum of things"\cf0 );\par ??    \cf5 // Teardown\par ??\cf0 \}}
--&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: courier new"&gt;&lt;pre style="margin: 0px"&gt;[&lt;span style="color: #2b91af"&gt;TestMethod&lt;/span&gt;]&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; NumberSumIsCorrect_Naïve()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Fixture setup&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Thing&lt;/span&gt; thing1
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Thing&lt;/span&gt;()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Number = 3,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Text = &lt;span style="color: #a31515"&gt;"Anonymous
text 1"&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; };&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Thing&lt;/span&gt; thing2
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Thing&lt;/span&gt;()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Number = 6,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Text = &lt;span style="color: #a31515"&gt;"Anonymous
text 2"&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; };&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Thing&lt;/span&gt; thing3
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Thing&lt;/span&gt;()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Number = 1,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Text = &lt;span style="color: #a31515"&gt;"Anonymous
text 3"&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; };&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;int&lt;/span&gt; expectedSum
= &lt;span style="color: blue"&gt;new&lt;/span&gt;[] { thing1, thing2, thing3 }.&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Select(t =&amp;gt; t.Number).Sum();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;IMyInterface&lt;/span&gt; fake
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;FakeMyInterface&lt;/span&gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; fake.AddThing(thing1);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; fake.AddThing(thing2);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; fake.AddThing(thing3);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt; sut
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt;(fake);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Exercise system&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;int&lt;/span&gt; result
= sut.CalculateSumOfThings();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Verify outcome&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.AreEqual&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt;(expectedSum,
result,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515"&gt;"Sum
of things"&lt;/span&gt;);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Teardown&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
If this was a truly one-off test and you know with certainty that there's going to
be no other tests just remotely similar to this one, just hard-coding the entire Fixture
Setup inline is in order, but as soon as the need for similar tests arises, this approach
leads to repetitive code, and hence unmaintainable tests.
&lt;/p&gt;
&lt;p&gt;
The more repetitive code that can be delegated to helper methods the better. A common
refactoring of the previous test might then look something like this:
&lt;/p&gt;
&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;\red0\green0\blue255;\red0\green128\blue0;\red163\green21\blue21;}??\fs20 [\cf3 TestMethod\cf0 ]\par ??\cf4 public\cf0  \cf4 void\cf0  NumberSumIsCorrect_Helpers()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf3 Thing\cf0  thing1 = \cf3 MyClassTest\cf0 .CreateAnonymousThing();\par ??    \cf3 Thing\cf0  thing2 = \cf3 MyClassTest\cf0 .CreateAnonymousThing();\par ??    \cf3 Thing\cf0  thing3 = \cf3 MyClassTest\cf0 .CreateAnonymousThing();\par ??    \cf3 Thing\cf0 [] things = \cf4 new\cf0 [] \{ thing1, thing2, thing3 \};\par ??\par ??    \cf4 int\cf0  expectedSum = things.Select(t =&amp;gt; t.Number).Sum();\par ??\par ??    \cf3 IMyInterface\cf0  fake = \cf4 new\cf0  \cf3 FakeMyInterface\cf0 ();\par ??    \cf3 MyClassTest\cf0 .AddThingsToMyInterface(fake, things);\par ??\par ??    \cf3 MyClass\cf0  sut = \cf4 new\cf0  \cf3 MyClass\cf0 (fake);\par ??    \cf5 // Exercise system\par ??\cf0     \cf4 int\cf0  result = sut.CalculateSumOfThings();\par ??    \cf5 // Verify outcome\par ??\cf0     \cf3 Assert\cf0 .AreEqual&amp;lt;\cf4 int\cf0 &amp;gt;(expectedSum, result,\par ??        \cf6 "Sum of things"\cf0 );\par ??    \cf5 // Teardown\par ??\cf0 \}}
--&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: courier new"&gt;&lt;pre style="margin: 0px"&gt;[&lt;span style="color: #2b91af"&gt;TestMethod&lt;/span&gt;]&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; NumberSumIsCorrect_Helpers()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Fixture setup&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Thing&lt;/span&gt; thing1
= &lt;span style="color: #2b91af"&gt;MyClassTest&lt;/span&gt;.CreateAnonymousThing();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Thing&lt;/span&gt; thing2
= &lt;span style="color: #2b91af"&gt;MyClassTest&lt;/span&gt;.CreateAnonymousThing();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Thing&lt;/span&gt; thing3
= &lt;span style="color: #2b91af"&gt;MyClassTest&lt;/span&gt;.CreateAnonymousThing();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Thing&lt;/span&gt;[]
things = &lt;span style="color: blue"&gt;new&lt;/span&gt;[] { thing1, thing2, thing3 };&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;int&lt;/span&gt; expectedSum
= things.Select(t =&amp;gt; t.Number).Sum();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;IMyInterface&lt;/span&gt; fake
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;FakeMyInterface&lt;/span&gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;MyClassTest&lt;/span&gt;.AddThingsToMyInterface(fake,
things);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt; sut
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt;(fake);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Exercise system&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;int&lt;/span&gt; result
= sut.CalculateSumOfThings();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Verify outcome&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.AreEqual&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt;(expectedSum,
result,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515"&gt;"Sum
of things"&lt;/span&gt;);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Teardown&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
While this is better, the helper methods are static methods, so it's necessary to
pass too much state around. The array of Things and the fake is both needed in the
test itself, as well as in the AddThingsToMyInterface helper method.
&lt;/p&gt;
&lt;p&gt;
By moving and refactoring the helper methods to a new class that represents the Fixture,
the test code becomes both more reusable and more readable.
&lt;/p&gt;
&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;\red0\green0\blue255;\red0\green128\blue0;\red163\green21\blue21;}??\fs20 [\cf3 TestMethod\cf0 ]\par ??\cf4 public\cf0  \cf4 void\cf0  NumberSumIsCorrect_FixtureObject()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf3 MyClassFixture\cf0  fixture = \cf4 new\cf0  \cf3 MyClassFixture\cf0 ();\par ??    fixture.AddAnonymousThings();\par ??\par ??    \cf4 int\cf0  expectedSum = \par ??        fixture.Things.Select(t =&amp;gt; t.Number).Sum();\par ??    \cf3 MyClass\cf0  sut = fixture.CreateSut();\par ??    \cf5 // Exercise system\par ??\cf0     \cf4 int\cf0  result = sut.CalculateSumOfThings();\par ??    \cf5 // Verify outcome\par ??\cf0     \cf3 Assert\cf0 .AreEqual&amp;lt;\cf4 int\cf0 &amp;gt;(expectedSum, result,\par ??        \cf6 "Sum of things"\cf0 );\par ??    \cf5 // Teardown\par ??\cf0 \}}
--&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: courier new"&gt;&lt;pre style="margin: 0px"&gt;[&lt;span style="color: #2b91af"&gt;TestMethod&lt;/span&gt;]&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; NumberSumIsCorrect_FixtureObject()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Fixture setup&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;MyClassFixture&lt;/span&gt; fixture
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MyClassFixture&lt;/span&gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; fixture.AddAnonymousThings();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;int&lt;/span&gt; expectedSum
= &lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fixture.Things.Select(t =&amp;gt; t.Number).Sum();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt; sut
= fixture.CreateSut();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Exercise system&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;int&lt;/span&gt; result
= sut.CalculateSumOfThings();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Verify outcome&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.AreEqual&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt;(expectedSum,
result,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515"&gt;"Sum
of things"&lt;/span&gt;);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Teardown&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
The MyClassFixture instance now holds the state of the Fixture, so there's much less
need to pass around as much data as before. The set of Things is now contained within
the Fixture object itself, and the fake has totally disappeared from the test; it's
still present, but now encapsulated within MyClassFixture.
&lt;/p&gt;
&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;}??\fs20 \cf1 internal\cf0  \cf1 class\cf0  \cf4 MyClassFixture\par ??\cf0 \{\par ??    \cf1 public\cf0  MyClassFixture()\par ??    \{\par ??        \cf1 this\cf0 .Fake = \cf1 new\cf0  \cf4 FakeMyInterface\cf0 ();\par ??        \cf1 this\cf0 .Things = \cf1 new\cf0  \cf4 List\cf0 &amp;lt;\cf4 Thing\cf0 &amp;gt;();\par ??    \}\par ??\par ??    \cf1 internal\cf0  \cf4 FakeMyInterface\cf0  Fake \{ \cf1 get\cf0 ; \cf1 private\cf0  \cf1 set\cf0 ; \}\par ??\par ??    \cf1 internal\cf0  \cf4 IList\cf0 &amp;lt;\cf4 Thing\cf0 &amp;gt; Things \{ \cf1 get\cf0 ; \cf1 private\cf0  \cf1 set\cf0 ; \}\par ??\par ??    \cf1 internal\cf0  \cf1 void\cf0  AddAnonymousThings()\par ??    \{\par ??        \cf1 int\cf0  many = 3;\par ??        \cf1 for\cf0  (\cf1 int\cf0  i = 0; i &amp;lt; many; i++)\par ??        \{\par ??            \cf4 Thing\cf0  t = \cf1 this\cf0 .CreateAnonymousThing();\par ??            \cf1 this\cf0 .Things.Add(t);\par ??            \cf1 this\cf0 .Fake.AddThing(t);\par ??        \}\par ??    \}\par ??\par ??    \cf1 internal\cf0  \cf4 MyClass\cf0  CreateSut()\par ??    \{\par ??        \cf1 return\cf0  \cf1 new\cf0  \cf4 MyClass\cf0 (\cf1 this\cf0 .Fake);\par ??    \}\par ??\par ??    \cf1 private\cf0  \cf4 Thing\cf0  CreateAnonymousThing()\par ??    \{\par ??        \cf4 Thing\cf0  t = \cf1 new\cf0  \cf4 Thing\cf0 ();\par ??        t.Number = \cf1 this\cf0 .Things.Count + 1;\par ??        t.Text = \cf4 Guid\cf0 .NewGuid().ToString();\par ??        \cf1 return\cf0  t;\par ??    \}\par ??\}}
--&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: courier new"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;internal&lt;/span&gt; &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MyClassFixture&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;public&lt;/span&gt; MyClassFixture()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;this&lt;/span&gt;.Fake
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;FakeMyInterface&lt;/span&gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;this&lt;/span&gt;.Things
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Thing&lt;/span&gt;&amp;gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;internal&lt;/span&gt; &lt;span style="color: #2b91af"&gt;FakeMyInterface&lt;/span&gt; Fake
{ &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;set&lt;/span&gt;;
}&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;internal&lt;/span&gt; &lt;span style="color: #2b91af"&gt;IList&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Thing&lt;/span&gt;&amp;gt;
Things { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;set&lt;/span&gt;;
}&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;internal&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; AddAnonymousThings()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;int&lt;/span&gt; many
= 3;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;for&lt;/span&gt; (&lt;span style="color: blue"&gt;int&lt;/span&gt; i
= 0; i &amp;lt; many; i++)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Thing&lt;/span&gt; t
= &lt;span style="color: blue"&gt;this&lt;/span&gt;.CreateAnonymousThing();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;this&lt;/span&gt;.Things.Add(t);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;this&lt;/span&gt;.Fake.AddThing(t);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;internal&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt; CreateSut()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt;(&lt;span style="color: blue"&gt;this&lt;/span&gt;.Fake);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Thing&lt;/span&gt; CreateAnonymousThing()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Thing&lt;/span&gt; t
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Thing&lt;/span&gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; t.Number = &lt;span style="color: blue"&gt;this&lt;/span&gt;.Things.Count
+ 1;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; t.Text = &lt;span style="color: #2b91af"&gt;Guid&lt;/span&gt;.NewGuid().ToString();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;return&lt;/span&gt; t;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
The CreateAnonymousThing method uses &lt;a href="http://blog.ploeh.dk/2009/03/05/ConstrainedNonDeterminism.aspx"&gt;Constrained
Non-Determinism&lt;/a&gt; to create unique Thing instances. The AddAnonymousThings method
uses &lt;a href="http://blogs.msdn.com/ploeh/archive/2008/12/08/3-is-many.aspx"&gt;3 as
an equivalence of many&lt;/a&gt;, and the CreateSut method acts as a &lt;a href="http://blog.ploeh.dk/2009/02/13/SUTFactory.aspx"&gt;SUT
Factory&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
This is both more reusable and more expressive than a collection of disjointed static
helper methods.
&lt;/p&gt;
&lt;p&gt;
Whenever I begin to feel that setting up a Test Fixture is becoming too cumbersome,
Fixture Object is the first pattern I consider.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=a6ac8eab-1593-4a08-ae66-f32599a51c09" /&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,a6ac8eab-1593-4a08-ae66-f32599a51c09.aspx</comments>
      <category>Productivity</category>
      <category>Unit Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=4d2cf05c-151b-4cd2-8849-27d2290c19e0</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,4d2cf05c-151b-4cd2-8849-27d2290c19e0.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,4d2cf05c-151b-4cd2-8849-27d2290c19e0.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=4d2cf05c-151b-4cd2-8849-27d2290c19e0</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
This may turn out to be the most controversial of my <a href="http://blog.ploeh.dk/2009/01/28/ZeroFrictionTDD.aspx">Zero-Friction
TDD</a> posts so far, as it supposedly goes against conventional wisdom. However,
I have found this approach to be really powerful since I began using it about a year
ago.
</p>
        <p>
In my previous post, I explained how <a href="http://blog.ploeh.dk/2009/03/03/DerivedValuesEnsureExecutableSpecification.aspx">Derived
Values help ensure that tests act as Executable Specification</a>. In short, a test
should clearly specify the relationship between input and outcome, as this test does:
</p>
        <!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;\red0\green0\blue255;\red0\green128\blue0;\red163\green21\blue21;}??\fs20 [\cf3 TestMethod\cf0 ]\par ??\cf4 public\cf0  \cf4 void\cf0  InvertWillReverseText()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf4 string\cf0  anonymousText = \cf6 "ploeh"\cf0 ;\par ??    \cf4 string\cf0  expectedResult =\par ??        \cf4 new\cf0  \cf4 string\cf0 (anonymousText.Reverse().ToArray());\par ??    \cf3 MyClass\cf0  sut = \cf4 new\cf0  \cf3 MyClass\cf0 ();\par ??    \cf5 // Exercise system\par ??\cf0     \cf4 string\cf0  result = sut.Invert(anonymousText);\par ??    \cf5 // Verify outcome\par ??\cf0     \cf3 Assert\cf0 .AreEqual&lt;\cf4 string\cf0 &gt;(expectedResult, result,\par ??        \cf6 "DoWork"\cf0 );\par ??    \cf5 // Teardown\par ??\cf0 \}}
-->
        <div style="font-size: 10pt; background: white; color: black; font-family: courier new">
          <pre style="margin: 0px">[<span style="color: #2b91af">TestMethod</span>]</pre>
          <pre style="margin: 0px">
            <span style="color: blue">public</span>
            <span style="color: blue">void</span> InvertWillReverseText()</pre>
          <pre style="margin: 0px">{</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Fixture setup</span></pre>
          <pre style="margin: 0px">    <span style="color: blue">string</span> anonymousText
= <span style="color: #a31515">"ploeh"</span>;</pre>
          <pre style="margin: 0px">    <span style="color: blue">string</span> expectedResult
=</pre>
          <pre style="margin: 0px">        <span style="color: blue">new</span><span style="color: blue">string</span>(anonymousText.Reverse().ToArray());</pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">MyClass</span> sut
= <span style="color: blue">new</span><span style="color: #2b91af">MyClass</span>();</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Exercise system</span></pre>
          <pre style="margin: 0px">    <span style="color: blue">string</span> result
= sut.Invert(anonymousText);</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Verify outcome</span></pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">Assert</span>.AreEqual&lt;<span style="color: blue">string</span>&gt;(expectedResult,
result,</pre>
          <pre style="margin: 0px">        <span style="color: #a31515">"DoWork"</span>);</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Teardown</span></pre>
          <pre style="margin: 0px">}</pre>
        </div>
        <p>
However, it is very tempting to just hardcode the expected value. Consistently using <a href="http://blog.ploeh.dk/ct.ashx?id=bab36bea-feb3-436b-a2e2-2860324b704c&amp;url=http%3a%2f%2fxunitpatterns.com%2fDerived%2520Value.html">Derived
Values</a> to establish the relationship between input and outcome requires discipline. 
</p>
        <p>
To help myself enforce this discipline, I use well-defined, but essentially random,
input, because when the input is random, I don't know the value at design time, and
hence, it is impossible for me to accidentally hard-code any assertions.
</p>
        <!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;\red0\green0\blue255;\red0\green128\blue0;\red163\green21\blue21;}??\fs20 [\cf3 TestMethod\cf0 ]\par ??\cf4 public\cf0  \cf4 void\cf0  InvertWillReverseText_Cnd()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf4 string\cf0  anonymousText = \cf3 Guid\cf0 .NewGuid().ToString();\par ??    \cf4 string\cf0  expectedResult =\par ??        \cf4 new\cf0  \cf4 string\cf0 (anonymousText.Reverse().ToArray());\par ??    \cf3 MyClass\cf0  sut = \cf4 new\cf0  \cf3 MyClass\cf0 ();\par ??    \cf5 // Exercise system\par ??\cf0     \cf4 string\cf0  result = sut.Invert(anonymousText);\par ??    \cf5 // Verify outcome\par ??\cf0     \cf3 Assert\cf0 .AreEqual&lt;\cf4 string\cf0 &gt;(expectedResult, result,\par ??        \cf6 "DoWork"\cf0 );\par ??    \cf5 // Teardown\par ??\cf0 \}}
-->
        <div style="font-size: 10pt; background: white; color: black; font-family: courier new">
          <pre style="margin: 0px">[<span style="color: #2b91af">TestMethod</span>]</pre>
          <pre style="margin: 0px">
            <span style="color: blue">public</span>
            <span style="color: blue">void</span> InvertWillReverseText_Cnd()</pre>
          <pre style="margin: 0px">{</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Fixture setup</span></pre>
          <pre style="margin: 0px">    <span style="color: blue">string</span> anonymousText
= <span style="color: #2b91af">Guid</span>.NewGuid().ToString();</pre>
          <pre style="margin: 0px">    <span style="color: blue">string</span> expectedResult
=</pre>
          <pre style="margin: 0px">        <span style="color: blue">new</span><span style="color: blue">string</span>(anonymousText.Reverse().ToArray());</pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">MyClass</span> sut
= <span style="color: blue">new</span><span style="color: #2b91af">MyClass</span>();</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Exercise system</span></pre>
          <pre style="margin: 0px">    <span style="color: blue">string</span> result
= sut.Invert(anonymousText);</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Verify outcome</span></pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">Assert</span>.AreEqual&lt;<span style="color: blue">string</span>&gt;(expectedResult,
result,</pre>
          <pre style="margin: 0px">        <span style="color: #a31515">"DoWork"</span>);</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Teardown</span></pre>
          <pre style="margin: 0px">}</pre>
        </div>
        <p>
For strings, I prefer Guids, as the above example demonstrates. For numbers, I often
just use the sequence of natural numbers (i.e. <em>1, 2, 3, 4, 5...</em>). For booleans,
I often use an alternating sequence (i.e. <em>true, false, true, false...</em>).
</p>
        <p>
While this technique causes input to become non-deterministic, I always pick the non-deterministic
value-generating algorithm in such a way that it creates 'nice' values; I call this
principle <strong>Constrained Non-Determinism</strong>. Values are carefully generated
to stay far away from any boundary conditions that may cause the <a href="http://blog.ploeh.dk/ct.ashx?id=bab36bea-feb3-436b-a2e2-2860324b704c&amp;url=http%3a%2f%2fxunitpatterns.com%2fSUT.html">SUT</a> to
behave differently in each test run.
</p>
        <p>
Conventional unit testing wisdom dictates that unit tests should be deterministic,
so how can I possibly endorse this technique?
</p>
        <p>
To understand this, it's important to know <em>why</em> the rule about deterministic
unit tests exist. It exists because we want to be certain that each time we execute
a test suite, we verify the exact same behavior as we did the last time (given that
no tests changed). Since we also use test suites as regression tests, it's important
that we can be confident that each and every test run verifies the exact same <em>specification</em>.
</p>
        <p>
Constrained Non-Determinism doesn't invalidate that goal, because the algorithm that
generates the values must be carefully picked to always create values that stay within
the input's <a href="http://xunitpatterns.com/equivalence%20class.html">Equivalence
Class</a>.
</p>
        <p>
In a surprisingly large set of APIs, strings, for example, are treated as opaque values
that don't influence behavior in themselves. Many enterprise applications mostly store
and read data from persistent data stores, and the value of a string in itself is
often inconsequential from the point of view of the code's execution path. Data stores
may have constraints on the length of strings, so Constrained Non-Determinism dictates
that you should pick the generating algorithm so that the string length always stays
within (or exceeds, if that's what you want to test) the constraint. Guid.ToString
always returns a string with the length of 36, which is fine for a large number of
scenarios.
</p>
        <p>
Note that Constrained Non-Determinism is only relevant for <a href="http://blogs.msdn.com/ploeh/archive/2008/11/17/anonymous-variables.aspx">Anonymous
Variables</a>. For input where the value holds a particular meaning in the context
of the SUT, you will still need to hand-pick values as always. E.g. if the input is
expected to be an XML string conforming to a particular schema, a Guid string makes
no sense.
</p>
        <p>
A secondary benefit of Constrained Non-Determinism is that you don't have to pause
to come up with values for Anonymous Variables when you are writing the test.
</p>
        <p>
While this advice may be controversial, I can only recommend it - I've been using
this technique for about a year now, and have only become more fond of it as I have
gained more experience with it.
</p>
        <img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=4d2cf05c-151b-4cd2-8849-27d2290c19e0" />
      </body>
      <title>Constrained Non-Determinism</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,4d2cf05c-151b-4cd2-8849-27d2290c19e0.aspx</guid>
      <link>http://blog.ploeh.dk/2009/03/05/ConstrainedNonDeterminism.aspx</link>
      <pubDate>Thu, 05 Mar 2009 20:23:05 GMT</pubDate>
      <description>&lt;p&gt;
This may turn out to be the most controversial of my &lt;a href="http://blog.ploeh.dk/2009/01/28/ZeroFrictionTDD.aspx"&gt;Zero-Friction
TDD&lt;/a&gt; posts so far, as it supposedly goes against conventional wisdom. However,
I have found this approach to be really powerful since I began using it about a year
ago.
&lt;/p&gt;
&lt;p&gt;
In my previous post, I explained how &lt;a href="http://blog.ploeh.dk/2009/03/03/DerivedValuesEnsureExecutableSpecification.aspx"&gt;Derived
Values help ensure that tests act as Executable Specification&lt;/a&gt;. In short, a test
should clearly specify the relationship between input and outcome, as this test does:
&lt;/p&gt;
&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;\red0\green0\blue255;\red0\green128\blue0;\red163\green21\blue21;}??\fs20 [\cf3 TestMethod\cf0 ]\par ??\cf4 public\cf0  \cf4 void\cf0  InvertWillReverseText()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf4 string\cf0  anonymousText = \cf6 "ploeh"\cf0 ;\par ??    \cf4 string\cf0  expectedResult =\par ??        \cf4 new\cf0  \cf4 string\cf0 (anonymousText.Reverse().ToArray());\par ??    \cf3 MyClass\cf0  sut = \cf4 new\cf0  \cf3 MyClass\cf0 ();\par ??    \cf5 // Exercise system\par ??\cf0     \cf4 string\cf0  result = sut.Invert(anonymousText);\par ??    \cf5 // Verify outcome\par ??\cf0     \cf3 Assert\cf0 .AreEqual&amp;lt;\cf4 string\cf0 &amp;gt;(expectedResult, result,\par ??        \cf6 "DoWork"\cf0 );\par ??    \cf5 // Teardown\par ??\cf0 \}}
--&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: courier new"&gt;&lt;pre style="margin: 0px"&gt;[&lt;span style="color: #2b91af"&gt;TestMethod&lt;/span&gt;]&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; InvertWillReverseText()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Fixture setup&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;string&lt;/span&gt; anonymousText
= &lt;span style="color: #a31515"&gt;"ploeh"&lt;/span&gt;;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;string&lt;/span&gt; expectedResult
=&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: blue"&gt;string&lt;/span&gt;(anonymousText.Reverse().ToArray());&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt; sut
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Exercise system&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;string&lt;/span&gt; result
= sut.Invert(anonymousText);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Verify outcome&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.AreEqual&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt;(expectedResult,
result,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515"&gt;"DoWork"&lt;/span&gt;);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Teardown&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
However, it is very tempting to just hardcode the expected value. Consistently using &lt;a href="http://blog.ploeh.dk/ct.ashx?id=bab36bea-feb3-436b-a2e2-2860324b704c&amp;amp;url=http%3a%2f%2fxunitpatterns.com%2fDerived%2520Value.html"&gt;Derived
Values&lt;/a&gt; to establish the relationship between input and outcome requires discipline. 
&lt;/p&gt;
&lt;p&gt;
To help myself enforce this discipline, I use well-defined, but essentially random,
input, because when the input is random, I don't know the value at design time, and
hence, it is impossible for me to accidentally hard-code any assertions.
&lt;/p&gt;
&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;\red0\green0\blue255;\red0\green128\blue0;\red163\green21\blue21;}??\fs20 [\cf3 TestMethod\cf0 ]\par ??\cf4 public\cf0  \cf4 void\cf0  InvertWillReverseText_Cnd()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf4 string\cf0  anonymousText = \cf3 Guid\cf0 .NewGuid().ToString();\par ??    \cf4 string\cf0  expectedResult =\par ??        \cf4 new\cf0  \cf4 string\cf0 (anonymousText.Reverse().ToArray());\par ??    \cf3 MyClass\cf0  sut = \cf4 new\cf0  \cf3 MyClass\cf0 ();\par ??    \cf5 // Exercise system\par ??\cf0     \cf4 string\cf0  result = sut.Invert(anonymousText);\par ??    \cf5 // Verify outcome\par ??\cf0     \cf3 Assert\cf0 .AreEqual&amp;lt;\cf4 string\cf0 &amp;gt;(expectedResult, result,\par ??        \cf6 "DoWork"\cf0 );\par ??    \cf5 // Teardown\par ??\cf0 \}}
--&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: courier new"&gt;&lt;pre style="margin: 0px"&gt;[&lt;span style="color: #2b91af"&gt;TestMethod&lt;/span&gt;]&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; InvertWillReverseText_Cnd()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Fixture setup&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;string&lt;/span&gt; anonymousText
= &lt;span style="color: #2b91af"&gt;Guid&lt;/span&gt;.NewGuid().ToString();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;string&lt;/span&gt; expectedResult
=&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: blue"&gt;string&lt;/span&gt;(anonymousText.Reverse().ToArray());&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt; sut
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Exercise system&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;string&lt;/span&gt; result
= sut.Invert(anonymousText);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Verify outcome&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.AreEqual&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt;(expectedResult,
result,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515"&gt;"DoWork"&lt;/span&gt;);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Teardown&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
For strings, I prefer Guids, as the above example demonstrates. For numbers, I often
just use the sequence of natural numbers (i.e. &lt;em&gt;1, 2, 3, 4, 5...&lt;/em&gt;). For booleans,
I often use an alternating sequence (i.e. &lt;em&gt;true, false, true, false...&lt;/em&gt;).
&lt;/p&gt;
&lt;p&gt;
While this technique causes input to become non-deterministic, I always pick the non-deterministic
value-generating algorithm in such a way that it creates 'nice' values; I call this
principle &lt;strong&gt;Constrained Non-Determinism&lt;/strong&gt;. Values are carefully generated
to stay far away from any boundary conditions that may cause the &lt;a href="http://blog.ploeh.dk/ct.ashx?id=bab36bea-feb3-436b-a2e2-2860324b704c&amp;amp;url=http%3a%2f%2fxunitpatterns.com%2fSUT.html"&gt;SUT&lt;/a&gt; to
behave differently in each test run.
&lt;/p&gt;
&lt;p&gt;
Conventional unit testing wisdom dictates that unit tests should be deterministic,
so how can I possibly endorse this technique?
&lt;/p&gt;
&lt;p&gt;
To understand this, it's important to know &lt;em&gt;why&lt;/em&gt; the rule about deterministic
unit tests exist. It exists because we want to be certain that each time we execute
a test suite, we verify the exact same behavior as we did the last time (given that
no tests changed). Since we also use test suites as regression tests, it's important
that we can be confident that each and every test run verifies the exact same &lt;em&gt;specification&lt;/em&gt;.
&lt;/p&gt;
&lt;p&gt;
Constrained Non-Determinism doesn't invalidate that goal, because the algorithm that
generates the values must be carefully picked to always create values that stay within
the input's &lt;a href="http://xunitpatterns.com/equivalence%20class.html"&gt;Equivalence
Class&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
In a surprisingly large set of APIs, strings, for example, are treated as opaque values
that don't influence behavior in themselves. Many enterprise applications mostly store
and read data from persistent data stores, and the value of a string in itself is
often inconsequential from the point of view of the code's execution path. Data stores
may have constraints on the length of strings, so Constrained Non-Determinism dictates
that you should pick the generating algorithm so that the string length always stays
within (or exceeds, if that's what you want to test) the constraint. Guid.ToString
always returns a string with the length of 36, which is fine for a large number of
scenarios.
&lt;/p&gt;
&lt;p&gt;
Note that Constrained Non-Determinism is only relevant for &lt;a href="http://blogs.msdn.com/ploeh/archive/2008/11/17/anonymous-variables.aspx"&gt;Anonymous
Variables&lt;/a&gt;. For input where the value holds a particular meaning in the context
of the SUT, you will still need to hand-pick values as always. E.g. if the input is
expected to be an XML string conforming to a particular schema, a Guid string makes
no sense.
&lt;/p&gt;
&lt;p&gt;
A secondary benefit of Constrained Non-Determinism is that you don't have to pause
to come up with values for Anonymous Variables when you are writing the test.
&lt;/p&gt;
&lt;p&gt;
While this advice may be controversial, I can only recommend it - I've been using
this technique for about a year now, and have only become more fond of it as I have
gained more experience with it.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=4d2cf05c-151b-4cd2-8849-27d2290c19e0" /&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,4d2cf05c-151b-4cd2-8849-27d2290c19e0.aspx</comments>
      <category>Productivity</category>
      <category>Unit Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=bab36bea-feb3-436b-a2e2-2860324b704c</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,bab36bea-feb3-436b-a2e2-2860324b704c.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,bab36bea-feb3-436b-a2e2-2860324b704c.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=bab36bea-feb3-436b-a2e2-2860324b704c</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
In this <a href="http://blog.ploeh.dk/2009/01/28/ZeroFrictionTDD.aspx">Zero-Friction
TDD</a> post, I’d like to take a detour around the concept of tests as <a href="http://xunitpatterns.com/Goals%20of%20Test%20Automation.html#Executable%20Specification">Executable
Specification</a>.
</p>
        <p>
An important aspect of test maintainability is readability. Tests should act both
as Executable Specification as well as <a href="http://xunitpatterns.com/Goals%20of%20Test%20Automation.html#Tests%20as%20Documentation">documentation</a>,
which puts a lot of responsibility on the test.
</p>
        <p>
One facet of test readability is to make the relationship between the <a href="http://xunitpatterns.com/test%20fixture%20-%20xUnit.html">Fixture</a>,
the <a href="http://xunitpatterns.com/SUT.html">SUT</a> and the verification as easy
to understand as possible. In other words, it should be clear to the <a href="http://xunitpatterns.com/test%20reader.html">Test
Reader</a> what is being asserted, and why.
</p>
        <p>
Consider a test like this one:
</p>
        <!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;\red0\green0\blue255;\red0\green128\blue0;\red163\green21\blue21;}??\fs20 [\cf3 TestMethod\cf0 ]\par ??\cf4 public\cf0  \cf4 void\cf0  InvertWillReverseText_Na\u239 ?ve()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf3 MyClass\cf0  sut = \cf4 new\cf0  \cf3 MyClass\cf0 ();\par ??    \cf5 // Exercise system\par ??\cf0     \cf4 string\cf0  result = sut.Invert(\cf6 "ploeh"\cf0 );\par ??    \cf5 // Verify outcome\par ??\cf0     \cf3 Assert\cf0 .AreEqual&lt;\cf4 string\cf0 &gt;(\cf6 "heolp"\cf0 , result, \cf6 "DoWork"\cf0 );\par ??    \cf5 // Teardown\par ??\cf0 \}}
-->
        <div style="font-size: 10pt; background: white; color: black; font-family: courier new">
          <pre style="margin: 0px">[<span style="color: #2b91af">TestMethod</span>]</pre>
          <pre style="margin: 0px">
            <span style="color: blue">public</span>
            <span style="color: blue">void</span> InvertWillReverseText_Naïve()</pre>
          <pre style="margin: 0px">{</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Fixture setup</span></pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">MyClass</span> sut
= <span style="color: blue">new</span><span style="color: #2b91af">MyClass</span>();</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Exercise system</span></pre>
          <pre style="margin: 0px">    <span style="color: blue">string</span> result
= sut.Invert(<span style="color: #a31515">"ploeh"</span>);</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Verify outcome</span></pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">Assert</span>.AreEqual&lt;<span style="color: blue">string</span>&gt;(<span style="color: #a31515">"heolp"</span>,
result, <span style="color: #a31515">"DoWork"</span>);</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Teardown</span></pre>
          <pre style="margin: 0px">}</pre>
        </div>
        <p>
Since this test is so simple, I expect that you can easily figure out that it implies
that the Invert method should simply reverse its input argument, but one of the reasons
this seems to be evident is because of the proximity of the two strings, as well as
the test’s name.
</p>
        <p>
In a test of a more complex API, this may not be quite as evident.
</p>
        <!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;\red0\green0\blue255;\red0\green128\blue0;\red163\green21\blue21;}??\fs20 [\cf3 TestMethod\cf0 ]\par ??\cf4 public\cf0  \cf4 void\cf0  DoItWillReturnCorrectResult_Na\u239 ?ve()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf3 MyClass\cf0  sut = \cf4 new\cf0  \cf3 MyClass\cf0 ();\par ??    \cf5 // Exercise system\par ??\cf0     \cf4 int\cf0  result = sut.DoIt(\cf6 "ploeh"\cf0 );\par ??    \cf5 // Verify outcome\par ??\cf0     \cf3 Assert\cf0 .AreEqual&lt;\cf4 int\cf0 &gt;(42, result, \cf6 "DoIt"\cf0 );\par ??    \cf5 // Teardown\par ??\cf0 \}}
-->
        <div style="font-size: 10pt; background: white; color: black; font-family: courier new">
          <pre style="margin: 0px">[<span style="color: #2b91af">TestMethod</span>]</pre>
          <pre style="margin: 0px">
            <span style="color: blue">public</span>
            <span style="color: blue">void</span> DoItWillReturnCorrectResult_Naïve()</pre>
          <pre style="margin: 0px">{</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Fixture setup</span></pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">MyClass</span> sut
= <span style="color: blue">new</span><span style="color: #2b91af">MyClass</span>();</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Exercise system</span></pre>
          <pre style="margin: 0px">    <span style="color: blue">int</span> result
= sut.DoIt(<span style="color: #a31515">"ploeh"</span>);</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Verify outcome</span></pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">Assert</span>.AreEqual&lt;<span style="color: blue">int</span>&gt;(42,
result, <span style="color: #a31515">"DoIt"</span>);</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Teardown</span></pre>
          <pre style="margin: 0px">}</pre>
        </div>
        <p>
In this test, there's no apparent relationship between the input (<em>ploeh</em>)
and the output (<em>42</em>). Whatever the algorithm is behind the DoIt method, it's
completely opaque to the Test Reader, and the test fails in its role as specification
and documentation.
</p>
        <p>
Returning to the first example, it would be better if the relationship between input
and output was explicitly described:
</p>
        <!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;\red0\green0\blue255;\red0\green128\blue0;\red163\green21\blue21;}??\fs20 [\cf3 TestMethod\cf0 ]\par ??\cf4 public\cf0  \cf4 void\cf0  InvertWillReverseText()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf4 string\cf0  anonymousText = \cf6 "ploeh"\cf0 ;\par ??    \cf4 string\cf0  expectedResult =\par ??        \cf4 new\cf0  \cf4 string\cf0 (anonymousText.Reverse().ToArray());\par ??    \cf3 MyClass\cf0  sut = \cf4 new\cf0  \cf3 MyClass\cf0 ();\par ??    \cf5 // Exercise system\par ??\cf0     \cf4 string\cf0  result = sut.Invert(anonymousText);\par ??    \cf5 // Verify outcome\par ??\cf0     \cf3 Assert\cf0 .AreEqual&lt;\cf4 string\cf0 &gt;(expectedResult, result,\par ??        \cf6 "DoWork"\cf0 );\par ??    \cf5 // Teardown\par ??\cf0 \}}
-->
        <div style="font-size: 10pt; background: white; color: black; font-family: courier new">
          <pre style="margin: 0px">[<span style="color: #2b91af">TestMethod</span>]</pre>
          <pre style="margin: 0px">
            <span style="color: blue">public</span>
            <span style="color: blue">void</span> InvertWillReverseText()</pre>
          <pre style="margin: 0px">{</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Fixture setup</span></pre>
          <pre style="margin: 0px">    <span style="color: blue">string</span> anonymousText
= <span style="color: #a31515">"ploeh"</span>;</pre>
          <pre style="margin: 0px">    <span style="color: blue">string</span> expectedResult
=</pre>
          <pre style="margin: 0px">        <span style="color: blue">new</span><span style="color: blue">string</span>(anonymousText.Reverse().ToArray());</pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">MyClass</span> sut
= <span style="color: blue">new</span><span style="color: #2b91af">MyClass</span>();</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Exercise system</span></pre>
          <pre style="margin: 0px">    <span style="color: blue">string</span> result
= sut.Invert(anonymousText);</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Verify outcome</span></pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">Assert</span>.AreEqual&lt;<span style="color: blue">string</span>&gt;(expectedResult,
result,</pre>
          <pre style="margin: 0px">        <span style="color: #a31515">"DoWork"</span>);</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Teardown</span></pre>
          <pre style="margin: 0px">}</pre>
        </div>
        <p>
In this case, the input and expected outcome are clearly related, and we call the
expectedResult variable a <a href="http://xunitpatterns.com/Derived%20Value.html">Derived
Value</a>, since we explicitly derive the expected result from the input.
</p>
        <p>
Note that I’m not asking you to re-implement the whole algorithm in the test, but
only to establish a relationship. One of the main rules of thumb of unit testing is
that a test should never contain conditional branches, so there must be at least one
test case per path though the SUT.
</p>
        <p>
In the example, the Invert method actually looks like this:
</p>
        <!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;}??\fs20 \cf1 public\cf0  \cf1 string\cf0  Invert(\cf1 string\cf0  message)\par ??\{\par ??    \cf1 double\cf0  d;\par ??    \cf1 if\cf0  (\cf1 double\cf0 .TryParse(message, \cf1 out\cf0  d))\par ??    \{\par ??        \cf1 return\cf0  (1d / d).ToString();\par ??    \}\par ??\par ??    \cf1 return\cf0  \cf1 new\cf0  \cf1 string\cf0 (message.Reverse().ToArray());\par ??\}}
-->
        <div style="font-size: 10pt; background: white; color: black; font-family: courier new">
          <pre style="margin: 0px">
            <span style="color: blue">public</span>
            <span style="color: blue">string</span> Invert(<span style="color: blue">string</span> message)</pre>
          <pre style="margin: 0px">{</pre>
          <pre style="margin: 0px">    <span style="color: blue">double</span> d;</pre>
          <pre style="margin: 0px">    <span style="color: blue">if</span> (<span style="color: blue">double</span>.TryParse(message, <span style="color: blue">out</span> d))</pre>
          <pre style="margin: 0px">    {</pre>
          <pre style="margin: 0px">        <span style="color: blue">return</span> (1d
/ d).ToString();</pre>
          <pre style="margin: 0px">    }</pre>
          <pre style="margin: 0px"> </pre>
          <pre style="margin: 0px">    <span style="color: blue">return</span><span style="color: blue">new</span><span style="color: blue">string</span>(message.Reverse().ToArray());</pre>
          <pre style="margin: 0px">}</pre>
        </div>
        <p>
Note that the above test only reproduces that part of the algorithm that corresponds
to the <a href="http://xunitpatterns.com/equivalence%20class.html">Equivalence Class</a> defined
by the input, whereas the branch that is triggered by a number string can be tested
by another test case that doesn’t specify string reversion.
</p>
        <!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;\red0\green0\blue255;\red0\green128\blue0;\red163\green21\blue21;}??\fs20 [\cf3 TestMethod\cf0 ]\par ??\cf4 public\cf0  \cf4 void\cf0  InvertWillInvertNumber()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf4 double\cf0  anonymousNumber = 10;\par ??    \cf4 string\cf0  numberText = anonymousNumber.ToString();\par ??    \cf4 string\cf0  expectedResult = \par ??        (1d / anonymousNumber).ToString();\par ??    \cf3 MyClass\cf0  sut = \cf4 new\cf0  \cf3 MyClass\cf0 ();\par ??    \cf5 // Exercise system\par ??\cf0     \cf4 string\cf0  result = sut.Invert(numberText);\par ??    \cf5 // Verify outcome\par ??\cf0     \cf3 Assert\cf0 .AreEqual&lt;\cf4 string\cf0 &gt;(expectedResult, result,\par ??        \cf6 "DoWork"\cf0 );\par ??    \cf5 // Teardown\par ??\cf0 \}}
-->
        <div style="font-size: 10pt; background: white; color: black; font-family: courier new">
          <pre style="margin: 0px">[<span style="color: #2b91af">TestMethod</span>]</pre>
          <pre style="margin: 0px">
            <span style="color: blue">public</span>
            <span style="color: blue">void</span> InvertWillInvertNumber()</pre>
          <pre style="margin: 0px">{</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Fixture setup</span></pre>
          <pre style="margin: 0px">    <span style="color: blue">double</span> anonymousNumber
= 10;</pre>
          <pre style="margin: 0px">    <span style="color: blue">string</span> numberText
= anonymousNumber.ToString();</pre>
          <pre style="margin: 0px">    <span style="color: blue">string</span> expectedResult
= </pre>
          <pre style="margin: 0px">        (1d / anonymousNumber).ToString();</pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">MyClass</span> sut
= <span style="color: blue">new</span><span style="color: #2b91af">MyClass</span>();</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Exercise system</span></pre>
          <pre style="margin: 0px">    <span style="color: blue">string</span> result
= sut.Invert(numberText);</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Verify outcome</span></pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">Assert</span>.AreEqual&lt;<span style="color: blue">string</span>&gt;(expectedResult,
result,</pre>
          <pre style="margin: 0px">        <span style="color: #a31515">"DoWork"</span>);</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Teardown</span></pre>
          <pre style="margin: 0px">}</pre>
        </div>
        <p>
In this way, we can break down the test cases to individual Executable Specifications
that define the expected behavior for each Equivalence Class.
</p>
        <p>
While such tests more clearly provide both specification and documentation, it requires
discipline to write tests in this way. Particularly when the algorithm is so simple
as is the case here, it's very tempting to just hard-code the values directly into
the assertion.
</p>
        <p>
In a future post, I’ll explain <a href="http://blog.ploeh.dk/2009/03/05/ConstrainedNonDeterminism.aspx">how
we can force ourselves to do the right thing per default</a>.
</p>
        <img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=bab36bea-feb3-436b-a2e2-2860324b704c" />
      </body>
      <title>Derived Values Ensure Executable Specification</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,bab36bea-feb3-436b-a2e2-2860324b704c.aspx</guid>
      <link>http://blog.ploeh.dk/2009/03/03/DerivedValuesEnsureExecutableSpecification.aspx</link>
      <pubDate>Tue, 03 Mar 2009 20:01:29 GMT</pubDate>
      <description>&lt;p&gt;
In this &lt;a href="http://blog.ploeh.dk/2009/01/28/ZeroFrictionTDD.aspx"&gt;Zero-Friction
TDD&lt;/a&gt; post, I’d like to take a detour around the concept of tests as &lt;a href="http://xunitpatterns.com/Goals%20of%20Test%20Automation.html#Executable%20Specification"&gt;Executable
Specification&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
An important aspect of test maintainability is readability. Tests should act both
as Executable Specification as well as &lt;a href="http://xunitpatterns.com/Goals%20of%20Test%20Automation.html#Tests%20as%20Documentation"&gt;documentation&lt;/a&gt;,
which puts a lot of responsibility on the test.
&lt;/p&gt;
&lt;p&gt;
One facet of test readability is to make the relationship between the &lt;a href="http://xunitpatterns.com/test%20fixture%20-%20xUnit.html"&gt;Fixture&lt;/a&gt;,
the &lt;a href="http://xunitpatterns.com/SUT.html"&gt;SUT&lt;/a&gt; and the verification as easy
to understand as possible. In other words, it should be clear to the &lt;a href="http://xunitpatterns.com/test%20reader.html"&gt;Test
Reader&lt;/a&gt; what is being asserted, and why.
&lt;/p&gt;
&lt;p&gt;
Consider a test like this one:
&lt;/p&gt;
&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;\red0\green0\blue255;\red0\green128\blue0;\red163\green21\blue21;}??\fs20 [\cf3 TestMethod\cf0 ]\par ??\cf4 public\cf0  \cf4 void\cf0  InvertWillReverseText_Na\u239 ?ve()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf3 MyClass\cf0  sut = \cf4 new\cf0  \cf3 MyClass\cf0 ();\par ??    \cf5 // Exercise system\par ??\cf0     \cf4 string\cf0  result = sut.Invert(\cf6 "ploeh"\cf0 );\par ??    \cf5 // Verify outcome\par ??\cf0     \cf3 Assert\cf0 .AreEqual&amp;lt;\cf4 string\cf0 &amp;gt;(\cf6 "heolp"\cf0 , result, \cf6 "DoWork"\cf0 );\par ??    \cf5 // Teardown\par ??\cf0 \}}
--&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: courier new"&gt;&lt;pre style="margin: 0px"&gt;[&lt;span style="color: #2b91af"&gt;TestMethod&lt;/span&gt;]&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; InvertWillReverseText_Naïve()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Fixture setup&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt; sut
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Exercise system&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;string&lt;/span&gt; result
= sut.Invert(&lt;span style="color: #a31515"&gt;"ploeh"&lt;/span&gt;);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Verify outcome&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.AreEqual&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt;(&lt;span style="color: #a31515"&gt;"heolp"&lt;/span&gt;,
result, &lt;span style="color: #a31515"&gt;"DoWork"&lt;/span&gt;);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Teardown&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
Since this test is so simple, I expect that you can easily figure out that it implies
that the Invert method should simply reverse its input argument, but one of the reasons
this seems to be evident is because of the proximity of the two strings, as well as
the test’s name.
&lt;/p&gt;
&lt;p&gt;
In a test of a more complex API, this may not be quite as evident.
&lt;/p&gt;
&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;\red0\green0\blue255;\red0\green128\blue0;\red163\green21\blue21;}??\fs20 [\cf3 TestMethod\cf0 ]\par ??\cf4 public\cf0  \cf4 void\cf0  DoItWillReturnCorrectResult_Na\u239 ?ve()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf3 MyClass\cf0  sut = \cf4 new\cf0  \cf3 MyClass\cf0 ();\par ??    \cf5 // Exercise system\par ??\cf0     \cf4 int\cf0  result = sut.DoIt(\cf6 "ploeh"\cf0 );\par ??    \cf5 // Verify outcome\par ??\cf0     \cf3 Assert\cf0 .AreEqual&amp;lt;\cf4 int\cf0 &amp;gt;(42, result, \cf6 "DoIt"\cf0 );\par ??    \cf5 // Teardown\par ??\cf0 \}}
--&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: courier new"&gt;&lt;pre style="margin: 0px"&gt;[&lt;span style="color: #2b91af"&gt;TestMethod&lt;/span&gt;]&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; DoItWillReturnCorrectResult_Naïve()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Fixture setup&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt; sut
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Exercise system&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;int&lt;/span&gt; result
= sut.DoIt(&lt;span style="color: #a31515"&gt;"ploeh"&lt;/span&gt;);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Verify outcome&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.AreEqual&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt;(42,
result, &lt;span style="color: #a31515"&gt;"DoIt"&lt;/span&gt;);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Teardown&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
In this test, there's no apparent relationship between the input (&lt;em&gt;ploeh&lt;/em&gt;)
and the output (&lt;em&gt;42&lt;/em&gt;). Whatever the algorithm is behind the DoIt method, it's
completely opaque to the Test Reader, and the test fails in its role as specification
and documentation.
&lt;/p&gt;
&lt;p&gt;
Returning to the first example, it would be better if the relationship between input
and output was explicitly described:
&lt;/p&gt;
&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;\red0\green0\blue255;\red0\green128\blue0;\red163\green21\blue21;}??\fs20 [\cf3 TestMethod\cf0 ]\par ??\cf4 public\cf0  \cf4 void\cf0  InvertWillReverseText()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf4 string\cf0  anonymousText = \cf6 "ploeh"\cf0 ;\par ??    \cf4 string\cf0  expectedResult =\par ??        \cf4 new\cf0  \cf4 string\cf0 (anonymousText.Reverse().ToArray());\par ??    \cf3 MyClass\cf0  sut = \cf4 new\cf0  \cf3 MyClass\cf0 ();\par ??    \cf5 // Exercise system\par ??\cf0     \cf4 string\cf0  result = sut.Invert(anonymousText);\par ??    \cf5 // Verify outcome\par ??\cf0     \cf3 Assert\cf0 .AreEqual&amp;lt;\cf4 string\cf0 &amp;gt;(expectedResult, result,\par ??        \cf6 "DoWork"\cf0 );\par ??    \cf5 // Teardown\par ??\cf0 \}}
--&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: courier new"&gt;&lt;pre style="margin: 0px"&gt;[&lt;span style="color: #2b91af"&gt;TestMethod&lt;/span&gt;]&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; InvertWillReverseText()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Fixture setup&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;string&lt;/span&gt; anonymousText
= &lt;span style="color: #a31515"&gt;"ploeh"&lt;/span&gt;;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;string&lt;/span&gt; expectedResult
=&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: blue"&gt;string&lt;/span&gt;(anonymousText.Reverse().ToArray());&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt; sut
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Exercise system&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;string&lt;/span&gt; result
= sut.Invert(anonymousText);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Verify outcome&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.AreEqual&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt;(expectedResult,
result,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515"&gt;"DoWork"&lt;/span&gt;);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Teardown&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
In this case, the input and expected outcome are clearly related, and we call the
expectedResult variable a &lt;a href="http://xunitpatterns.com/Derived%20Value.html"&gt;Derived
Value&lt;/a&gt;, since we explicitly derive the expected result from the input.
&lt;/p&gt;
&lt;p&gt;
Note that I’m not asking you to re-implement the whole algorithm in the test, but
only to establish a relationship. One of the main rules of thumb of unit testing is
that a test should never contain conditional branches, so there must be at least one
test case per path though the SUT.
&lt;/p&gt;
&lt;p&gt;
In the example, the Invert method actually looks like this:
&lt;/p&gt;
&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;}??\fs20 \cf1 public\cf0  \cf1 string\cf0  Invert(\cf1 string\cf0  message)\par ??\{\par ??    \cf1 double\cf0  d;\par ??    \cf1 if\cf0  (\cf1 double\cf0 .TryParse(message, \cf1 out\cf0  d))\par ??    \{\par ??        \cf1 return\cf0  (1d / d).ToString();\par ??    \}\par ??\par ??    \cf1 return\cf0  \cf1 new\cf0  \cf1 string\cf0 (message.Reverse().ToArray());\par ??\}}
--&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: courier new"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;string&lt;/span&gt; Invert(&lt;span style="color: blue"&gt;string&lt;/span&gt; message)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;double&lt;/span&gt; d;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;if&lt;/span&gt; (&lt;span style="color: blue"&gt;double&lt;/span&gt;.TryParse(message, &lt;span style="color: blue"&gt;out&lt;/span&gt; d))&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;return&lt;/span&gt; (1d
/ d).ToString();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: blue"&gt;string&lt;/span&gt;(message.Reverse().ToArray());&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
Note that the above test only reproduces that part of the algorithm that corresponds
to the &lt;a href="http://xunitpatterns.com/equivalence%20class.html"&gt;Equivalence Class&lt;/a&gt; defined
by the input, whereas the branch that is triggered by a number string can be tested
by another test case that doesn’t specify string reversion.
&lt;/p&gt;
&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;\red0\green0\blue255;\red0\green128\blue0;\red163\green21\blue21;}??\fs20 [\cf3 TestMethod\cf0 ]\par ??\cf4 public\cf0  \cf4 void\cf0  InvertWillInvertNumber()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf4 double\cf0  anonymousNumber = 10;\par ??    \cf4 string\cf0  numberText = anonymousNumber.ToString();\par ??    \cf4 string\cf0  expectedResult = \par ??        (1d / anonymousNumber).ToString();\par ??    \cf3 MyClass\cf0  sut = \cf4 new\cf0  \cf3 MyClass\cf0 ();\par ??    \cf5 // Exercise system\par ??\cf0     \cf4 string\cf0  result = sut.Invert(numberText);\par ??    \cf5 // Verify outcome\par ??\cf0     \cf3 Assert\cf0 .AreEqual&amp;lt;\cf4 string\cf0 &amp;gt;(expectedResult, result,\par ??        \cf6 "DoWork"\cf0 );\par ??    \cf5 // Teardown\par ??\cf0 \}}
--&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: courier new"&gt;&lt;pre style="margin: 0px"&gt;[&lt;span style="color: #2b91af"&gt;TestMethod&lt;/span&gt;]&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; InvertWillInvertNumber()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Fixture setup&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;double&lt;/span&gt; anonymousNumber
= 10;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;string&lt;/span&gt; numberText
= anonymousNumber.ToString();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;string&lt;/span&gt; expectedResult
= &lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (1d / anonymousNumber).ToString();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt; sut
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Exercise system&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;string&lt;/span&gt; result
= sut.Invert(numberText);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Verify outcome&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.AreEqual&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt;(expectedResult,
result,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515"&gt;"DoWork"&lt;/span&gt;);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Teardown&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
In this way, we can break down the test cases to individual Executable Specifications
that define the expected behavior for each Equivalence Class.
&lt;/p&gt;
&lt;p&gt;
While such tests more clearly provide both specification and documentation, it requires
discipline to write tests in this way. Particularly when the algorithm is so simple
as is the case here, it's very tempting to just hard-code the values directly into
the assertion.
&lt;/p&gt;
&lt;p&gt;
In a future post, I’ll explain &lt;a href="http://blog.ploeh.dk/2009/03/05/ConstrainedNonDeterminism.aspx"&gt;how
we can force ourselves to do the right thing per default&lt;/a&gt;.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=bab36bea-feb3-436b-a2e2-2860324b704c" /&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,bab36bea-feb3-436b-a2e2-2860324b704c.aspx</comments>
      <category>Productivity</category>
      <category>Unit Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=ec9010e9-3496-47a6-bed1-141becd04a13</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,ec9010e9-3496-47a6-bed1-141becd04a13.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,ec9010e9-3496-47a6-bed1-141becd04a13.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=ec9010e9-3496-47a6-bed1-141becd04a13</wfw:commentRss>
      <slash:comments>2</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
In my <a href="http://blog.ploeh.dk/2009/01/28/ZeroFrictionTDD.aspx">Zero-Friction
TDD</a> series, I focus on establishing a set of good habits that can potentially
make you more productive while writing tests TDD style. While being able to quickly
write good tests is important, this is not the only quality on which you should focus.
</p>
        <p>
Maintainability, not only of your production code, but also of your test code, is
important, and the DRY principle is just as applicable here.
</p>
        <p>
Consider a test like this:
</p>
        <!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;\red0\green0\blue255;\red0\green128\blue0;}??\fs20 [\cf3 TestMethod\cf0 ]\par ??\cf4 public\cf0  \cf4 void\cf0  SomeTestUsingConstructorToCreateSut()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf3 MyClass\cf0  sut = \cf4 new\cf0  \cf3 MyClass\cf0 ();\par ??    \cf5 // Exercise system\par ??\cf0     \cf5 // ...\par ??\cf0     \cf5 // Verify outcome\par ??\cf0     \cf5 // ...\par ??\cf0     \cf5 // Teardown\par ??\cf0 \}}
-->
        <div style="font-size: 10pt; background: white; color: black; font-family: courier new">
          <pre style="margin: 0px">[<span style="color: #2b91af">TestMethod</span>]</pre>
          <pre style="margin: 0px">
            <span style="color: blue">public</span>
            <span style="color: blue">void</span> SomeTestUsingConstructorToCreateSut()</pre>
          <pre style="margin: 0px">{</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Fixture setup</span></pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">MyClass</span> sut
= <span style="color: blue">new</span><span style="color: #2b91af">MyClass</span>();</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Exercise system</span></pre>
          <pre style="margin: 0px">    <span style="color: green">//
...</span></pre>
          <pre style="margin: 0px">    <span style="color: green">//
Verify outcome</span></pre>
          <pre style="margin: 0px">    <span style="color: green">//
...</span></pre>
          <pre style="margin: 0px">    <span style="color: green">//
Teardown</span></pre>
          <pre style="margin: 0px">}</pre>
        </div>
        <p>
Such a test represents an anti-pattern you can easily fall victim to. The main item
of interest here is that I create the <a href="http://xunitpatterns.com/SUT.html">SUT</a> using
its constructor. You could say that I have hard-coded this particular constructor
usage into my test.
</p>
        <p>
This is not a problem if there's only one test of MyClass, but once you have many,
this starts to become a drag on your ability to refactor your code.
</p>
        <p>
Imagine that you want to change the constructor of MyClass from the default constructor
to one that takes a dependency, like this:
</p>
        <!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;}??\fs20 \cf1 public\cf0  MyClass(\cf4 IMyInterface\cf0  dependency)}
-->
        <div style="font-size: 10pt; background: white; color: black; font-family: courier new">
          <pre style="margin: 0px">
            <span style="color: blue">public</span> MyClass(<span style="color: #2b91af">IMyInterface</span> dependency)</pre>
        </div>
        <p>
If you have many (in this case, not <a href="http://blogs.msdn.com/ploeh/archive/2008/12/08/3-is-many.aspx">three</a>,
but dozens) tests using the default constructor, this simple change will force you
to visit all these tests and modify them to be able to compile again.
</p>
        <p>
If, instead, we use a factory to create the SUT in each test, there's a single place
where we can go and update the creation logic.
</p>
        <!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;\red0\green0\blue255;\red0\green128\blue0;}??\fs20 [\cf3 TestMethod\cf0 ]\par ??\cf4 public\cf0  \cf4 void\cf0  SomeTestUsingFactoryToCreateSut()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf3 MyClass\cf0  sut = \cf3 MyClassFactory\cf0 .Create();\par ??    \cf5 // Exercise system\par ??\cf0     \cf5 // ...\par ??\cf0     \cf5 // Verify outcome\par ??\cf0     \cf5 // ...\par ??\cf0     \cf5 // Teardown\par ??\cf0 \}}
-->
        <div style="font-size: 10pt; background: white; color: black; font-family: courier new">
          <pre style="margin: 0px">[<span style="color: #2b91af">TestMethod</span>]</pre>
          <pre style="margin: 0px">
            <span style="color: blue">public</span>
            <span style="color: blue">void</span> SomeTestUsingFactoryToCreateSut()</pre>
          <pre style="margin: 0px">{</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Fixture setup</span></pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">MyClass</span> sut
= <span style="color: #2b91af">MyClassFactory</span>.Create();</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Exercise system</span></pre>
          <pre style="margin: 0px">    <span style="color: green">//
...</span></pre>
          <pre style="margin: 0px">    <span style="color: green">//
Verify outcome</span></pre>
          <pre style="margin: 0px">    <span style="color: green">//
...</span></pre>
          <pre style="margin: 0px">    <span style="color: green">//
Teardown</span></pre>
          <pre style="margin: 0px">}</pre>
        </div>
        <p>
The MyClassFactory class is a test-specific helper class (more formally, it's part
of our <a href="http://xunitpatterns.com/Test%20Utility%20Method.html#SUT%20API%20Encapsulation">SUT
API Encapsulation</a>) that is part of the unit test project. Using this factory,
we only need to modify the Create method to implement the constructor change.
</p>
        <!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;}??\fs20 \cf1 internal\cf0  \cf1 static\cf0  \cf4 MyClass\cf0  Create()\par ??\{\par ??    \cf4 IMyInterface\cf0  fake = \cf1 new\cf0  \cf4 FakeMyInterface\cf0 ();\par ??    \cf1 return\cf0  \cf1 new\cf0  \cf4 MyClass\cf0 (fake);\par ??\}}
-->
        <div style="font-size: 10pt; background: white; color: black; font-family: courier new">
          <pre style="margin: 0px">
            <span style="color: blue">internal</span>
            <span style="color: blue">static</span>
            <span style="color: #2b91af">MyClass</span> Create()</pre>
          <pre style="margin: 0px">{</pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">IMyInterface</span> fake
= <span style="color: blue">new</span><span style="color: #2b91af">FakeMyInterface</span>();</pre>
          <pre style="margin: 0px">    <span style="color: blue">return</span><span style="color: blue">new</span><span style="color: #2b91af">MyClass</span>(fake);</pre>
          <pre style="margin: 0px">}</pre>
        </div>
        <p>
Instead of having to modify many individual tests to support the signature change
of the constructor, there's now one central place where we can go and do that. This
pattern supports refactoring much better, so consider making this a habit of yours.
</p>
        <p>
One exception to this rule concerns tests that explicitly deal with the constructor,
such as this one:
</p>
        <!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;\red0\green0\blue255;\red0\green128\blue0;}??\fs20 [\cf3 ExpectedException\cf0 (\cf4 typeof\cf0 (\cf3 ArgumentNullException\cf0 ))]\par ??[\cf3 TestMethod\cf0 ]\par ??\cf4 public\cf0  \cf4 void\cf0  CreateWithNullMyInterfaceWillThrow()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf3 IMyInterface\cf0  nullMyInterface = \cf4 null\cf0 ;\par ??    \cf5 // Exercise system\par ??\cf0     \cf4 new\cf0  \cf3 MyClass\cf0 (nullMyInterface);\par ??    \cf5 // Verify outcome (expected exception)\par ??\cf0     \cf5 // Teardown\par ??\cf0 \}}
-->
        <div style="font-size: 10pt; background: white; color: black; font-family: courier new">
          <pre style="margin: 0px">[<span style="color: #2b91af">ExpectedException</span>(<span style="color: blue">typeof</span>(<span style="color: #2b91af">ArgumentNullException</span>))]</pre>
          <pre style="margin: 0px">[<span style="color: #2b91af">TestMethod</span>]</pre>
          <pre style="margin: 0px">
            <span style="color: blue">public</span>
            <span style="color: blue">void</span> CreateWithNullMyInterfaceWillThrow()</pre>
          <pre style="margin: 0px">{</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Fixture setup</span></pre>
          <pre style="margin: 0px">    <span style="color: #2b91af">IMyInterface</span> nullMyInterface
= <span style="color: blue">null</span>;</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Exercise system</span></pre>
          <pre style="margin: 0px">    <span style="color: blue">new</span><span style="color: #2b91af">MyClass</span>(nullMyInterface);</pre>
          <pre style="margin: 0px">    <span style="color: green">//
Verify outcome (expected exception)</span></pre>
          <pre style="margin: 0px">    <span style="color: green">//
Teardown</span></pre>
          <pre style="margin: 0px">}</pre>
        </div>
        <p>
In a case like this, where you explicitly want to deal with the constructor in an
anomalous way, I consider it reasonable to deviate from the rule of using a factory
to create the SUT. Although this may result in a need to fix the SUT creation logic
in more than one place, instead of only in the factory itself, it's likely to be constrained
to a few places instead of dozens or more, since normally, you will only have a handful
of these explicit constructor tests.
</p>
        <p>
Compared to my Zero-Friction TDD tips and tricks, this particular advice has the potential
to marginally slow you down. However, this investments pays off when you want to refactor
your SUT's constructor, and remember that you can always just write the <a href="http://blogs.msdn.com/ploeh/archive/2009/01/05/use-the-generate-method-stub-smart-tag-to-stay-in-the-zone.aspx">call
to the factory and move on without implementing it right away</a>.
</p>
        <img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=ec9010e9-3496-47a6-bed1-141becd04a13" />
      </body>
      <title>SUT Factory</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,ec9010e9-3496-47a6-bed1-141becd04a13.aspx</guid>
      <link>http://blog.ploeh.dk/2009/02/13/SUTFactory.aspx</link>
      <pubDate>Fri, 13 Feb 2009 07:56:21 GMT</pubDate>
      <description>&lt;p&gt;
In my &lt;a href="http://blog.ploeh.dk/2009/01/28/ZeroFrictionTDD.aspx"&gt;Zero-Friction
TDD&lt;/a&gt; series, I focus on establishing a set of good habits that can potentially
make you more productive while writing tests TDD style. While being able to quickly
write good tests is important, this is not the only quality on which you should focus.
&lt;/p&gt;
&lt;p&gt;
Maintainability, not only of your production code, but also of your test code, is
important, and the DRY principle is just as applicable here.
&lt;/p&gt;
&lt;p&gt;
Consider a test like this:
&lt;/p&gt;
&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;\red0\green0\blue255;\red0\green128\blue0;}??\fs20 [\cf3 TestMethod\cf0 ]\par ??\cf4 public\cf0  \cf4 void\cf0  SomeTestUsingConstructorToCreateSut()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf3 MyClass\cf0  sut = \cf4 new\cf0  \cf3 MyClass\cf0 ();\par ??    \cf5 // Exercise system\par ??\cf0     \cf5 // ...\par ??\cf0     \cf5 // Verify outcome\par ??\cf0     \cf5 // ...\par ??\cf0     \cf5 // Teardown\par ??\cf0 \}}
--&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: courier new"&gt;&lt;pre style="margin: 0px"&gt;[&lt;span style="color: #2b91af"&gt;TestMethod&lt;/span&gt;]&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; SomeTestUsingConstructorToCreateSut()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Fixture setup&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt; sut
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Exercise system&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
...&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Verify outcome&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
...&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Teardown&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
Such a test represents an anti-pattern you can easily fall victim to. The main item
of interest here is that I create the &lt;a href="http://xunitpatterns.com/SUT.html"&gt;SUT&lt;/a&gt; using
its constructor. You could say that I have hard-coded this particular constructor
usage into my test.
&lt;/p&gt;
&lt;p&gt;
This is not a problem if there's only one test of MyClass, but once you have many,
this starts to become a drag on your ability to refactor your code.
&lt;/p&gt;
&lt;p&gt;
Imagine that you want to change the constructor of MyClass from the default constructor
to one that takes a dependency, like this:
&lt;/p&gt;
&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;}??\fs20 \cf1 public\cf0  MyClass(\cf4 IMyInterface\cf0  dependency)}
--&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: courier new"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; MyClass(&lt;span style="color: #2b91af"&gt;IMyInterface&lt;/span&gt; dependency)&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
If you have many (in this case, not &lt;a href="http://blogs.msdn.com/ploeh/archive/2008/12/08/3-is-many.aspx"&gt;three&lt;/a&gt;,
but dozens) tests using the default constructor, this simple change will force you
to visit all these tests and modify them to be able to compile again.
&lt;/p&gt;
&lt;p&gt;
If, instead, we use a factory to create the SUT in each test, there's a single place
where we can go and update the creation logic.
&lt;/p&gt;
&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;\red0\green0\blue255;\red0\green128\blue0;}??\fs20 [\cf3 TestMethod\cf0 ]\par ??\cf4 public\cf0  \cf4 void\cf0  SomeTestUsingFactoryToCreateSut()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf3 MyClass\cf0  sut = \cf3 MyClassFactory\cf0 .Create();\par ??    \cf5 // Exercise system\par ??\cf0     \cf5 // ...\par ??\cf0     \cf5 // Verify outcome\par ??\cf0     \cf5 // ...\par ??\cf0     \cf5 // Teardown\par ??\cf0 \}}
--&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: courier new"&gt;&lt;pre style="margin: 0px"&gt;[&lt;span style="color: #2b91af"&gt;TestMethod&lt;/span&gt;]&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; SomeTestUsingFactoryToCreateSut()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Fixture setup&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt; sut
= &lt;span style="color: #2b91af"&gt;MyClassFactory&lt;/span&gt;.Create();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Exercise system&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
...&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Verify outcome&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
...&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Teardown&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
The MyClassFactory class is a test-specific helper class (more formally, it's part
of our &lt;a href="http://xunitpatterns.com/Test%20Utility%20Method.html#SUT%20API%20Encapsulation"&gt;SUT
API Encapsulation&lt;/a&gt;) that is part of the unit test project. Using this factory,
we only need to modify the Create method to implement the constructor change.
&lt;/p&gt;
&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;}??\fs20 \cf1 internal\cf0  \cf1 static\cf0  \cf4 MyClass\cf0  Create()\par ??\{\par ??    \cf4 IMyInterface\cf0  fake = \cf1 new\cf0  \cf4 FakeMyInterface\cf0 ();\par ??    \cf1 return\cf0  \cf1 new\cf0  \cf4 MyClass\cf0 (fake);\par ??\}}
--&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: courier new"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;internal&lt;/span&gt; &lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt; Create()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;IMyInterface&lt;/span&gt; fake
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;FakeMyInterface&lt;/span&gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt;(fake);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
Instead of having to modify many individual tests to support the signature change
of the constructor, there's now one central place where we can go and do that. This
pattern supports refactoring much better, so consider making this a habit of yours.
&lt;/p&gt;
&lt;p&gt;
One exception to this rule concerns tests that explicitly deal with the constructor,
such as this one:
&lt;/p&gt;
&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;\red0\green0\blue255;\red0\green128\blue0;}??\fs20 [\cf3 ExpectedException\cf0 (\cf4 typeof\cf0 (\cf3 ArgumentNullException\cf0 ))]\par ??[\cf3 TestMethod\cf0 ]\par ??\cf4 public\cf0  \cf4 void\cf0  CreateWithNullMyInterfaceWillThrow()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf3 IMyInterface\cf0  nullMyInterface = \cf4 null\cf0 ;\par ??    \cf5 // Exercise system\par ??\cf0     \cf4 new\cf0  \cf3 MyClass\cf0 (nullMyInterface);\par ??    \cf5 // Verify outcome (expected exception)\par ??\cf0     \cf5 // Teardown\par ??\cf0 \}}
--&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: courier new"&gt;&lt;pre style="margin: 0px"&gt;[&lt;span style="color: #2b91af"&gt;ExpectedException&lt;/span&gt;(&lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;ArgumentNullException&lt;/span&gt;))]&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;[&lt;span style="color: #2b91af"&gt;TestMethod&lt;/span&gt;]&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; CreateWithNullMyInterfaceWillThrow()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Fixture setup&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;IMyInterface&lt;/span&gt; nullMyInterface
= &lt;span style="color: blue"&gt;null&lt;/span&gt;;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Exercise system&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MyClass&lt;/span&gt;(nullMyInterface);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Verify outcome (expected exception)&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//
Teardown&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
In a case like this, where you explicitly want to deal with the constructor in an
anomalous way, I consider it reasonable to deviate from the rule of using a factory
to create the SUT. Although this may result in a need to fix the SUT creation logic
in more than one place, instead of only in the factory itself, it's likely to be constrained
to a few places instead of dozens or more, since normally, you will only have a handful
of these explicit constructor tests.
&lt;/p&gt;
&lt;p&gt;
Compared to my Zero-Friction TDD tips and tricks, this particular advice has the potential
to marginally slow you down. However, this investments pays off when you want to refactor
your SUT's constructor, and remember that you can always just write the &lt;a href="http://blogs.msdn.com/ploeh/archive/2009/01/05/use-the-generate-method-stub-smart-tag-to-stay-in-the-zone.aspx"&gt;call
to the factory and move on without implementing it right away&lt;/a&gt;.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=ec9010e9-3496-47a6-bed1-141becd04a13" /&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,ec9010e9-3496-47a6-bed1-141becd04a13.aspx</comments>
      <category>Productivity</category>
      <category>Unit Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=b0a3b644-dd52-46c4-91cf-8261c11431b3</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,b0a3b644-dd52-46c4-91cf-8261c11431b3.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,b0a3b644-dd52-46c4-91cf-8261c11431b3.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=b0a3b644-dd52-46c4-91cf-8261c11431b3</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
In my original post on <a href="http://blogs.msdn.com/ploeh/archive/2008/11/13/zero-friction-tdd.aspx">Zero-Friction
TDD</a>, I continually updated the list of posts in the series, so that there would
always be a central 'table of contents' for this topic.
</p>
        <p>
Since <a href="http://blog.ploeh.dk/2009/01/28/LivingInInterestingTimes.aspx">I'll
lose the ability to keep editing any of my previous postings</a> on the old <a href="http://blogs.msdn.com/ploeh">ploeh
blog</a>, the present post now contains the most updated list of Zero-Friction TDD
articles:
</p>
        <ul>
          <li>
            <a href="http://blogs.msdn.com/ploeh/archive/2008/10/06/naming-sut-test-variables.aspx">Naming
SUT Test Variables</a>
          </li>
          <li>
            <a href="http://blogs.msdn.com/ploeh/archive/2008/11/14/naming-direct-output-variables.aspx">Naming
Direct Output Variables</a>
          </li>
          <li>
            <a href="http://blogs.msdn.com/ploeh/archive/2008/11/17/anonymous-variables.aspx">Anonymous
Variables</a>
          </li>
          <li>
            <a href="http://blogs.msdn.com/ploeh/archive/2008/11/21/ignore-irrelevant-return-values.aspx">Ignore
Irrelevant Return Values</a>
          </li>
          <li>
            <a href="http://blogs.msdn.com/ploeh/archive/2008/12/01/testmethod-code-snippet.aspx">testmethod
Code Snippet</a>
          </li>
          <li>
            <a href="http://blogs.msdn.com/ploeh/archive/2008/12/08/3-is-many.aspx">3 Is Many</a>
          </li>
          <li>
            <a href="http://blogs.msdn.com/ploeh/archive/2008/12/11/why-use-areequal-t.aspx">Why
Use AreEqual&lt;T&gt;?</a>
          </li>
          <li>
            <a href="http://blogs.msdn.com/ploeh/archive/2008/12/17/assert-messages-are-not-optional.aspx">Assert
Messages Are Not Optional</a>
          </li>
          <li>
            <a href="http://blogs.msdn.com/ploeh/archive/2009/01/05/use-the-generate-method-stub-smart-tag-to-stay-in-the-zone.aspx">Use
The Generate Method Stub Smart Tag To Stay In The Zone</a>
          </li>
          <li>
            <a href="http://blogs.msdn.com/ploeh/archive/2009/01/12/test-driven-properties.aspx">Test-Driven
Properties</a>
          </li>
          <li>
            <a href="http://blog.ploeh.dk/2009/02/13/SUTFactory.aspx">SUT Factory</a>
          </li>
          <li>
            <a href="http://blog.ploeh.dk/2009/03/03/DerivedValuesEnsureExecutableSpecification.aspx">Derived
Values Ensure Executable Specification</a>
          </li>
          <li>
            <a href="http://blog.ploeh.dk/2009/03/05/ConstrainedNonDeterminism.aspx">Constrained
Non-Determinism</a>
          </li>
          <li>
            <a href="http://blog.ploeh.dk/2009/03/11/ExplicitExpectations.aspx">Explicit Expectations</a>
          </li>
          <li>
            <a href="http://blog.ploeh.dk/2009/03/16/FixtureObject.aspx">Fixture Object</a>
          </li>
          <li>
            <a href="http://blog.ploeh.dk/2009/05/15/AutoFixtureAsFixtureObject.aspx">AutoFixture
As Fixture Object</a>
          </li>
        </ul>
        <img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=b0a3b644-dd52-46c4-91cf-8261c11431b3" />
      </body>
      <title>Zero-Friction TDD</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,b0a3b644-dd52-46c4-91cf-8261c11431b3.aspx</guid>
      <link>http://blog.ploeh.dk/2009/01/28/ZeroFrictionTDD.aspx</link>
      <pubDate>Wed, 28 Jan 2009 14:46:44 GMT</pubDate>
      <description>&lt;p&gt;
In my original post on &lt;a href="http://blogs.msdn.com/ploeh/archive/2008/11/13/zero-friction-tdd.aspx"&gt;Zero-Friction
TDD&lt;/a&gt;, I continually updated the list of posts in the series, so that there would
always be a central 'table of contents' for this topic.
&lt;/p&gt;
&lt;p&gt;
Since &lt;a href="http://blog.ploeh.dk/2009/01/28/LivingInInterestingTimes.aspx"&gt;I'll
lose the ability to keep editing any of my previous postings&lt;/a&gt; on the old &lt;a href="http://blogs.msdn.com/ploeh"&gt;ploeh
blog&lt;/a&gt;, the present post now contains the most updated list of Zero-Friction TDD
articles:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;a href="http://blogs.msdn.com/ploeh/archive/2008/10/06/naming-sut-test-variables.aspx"&gt;Naming
SUT Test Variables&lt;/a&gt; 
&lt;li&gt;
&lt;a href="http://blogs.msdn.com/ploeh/archive/2008/11/14/naming-direct-output-variables.aspx"&gt;Naming
Direct Output Variables&lt;/a&gt; 
&lt;li&gt;
&lt;a href="http://blogs.msdn.com/ploeh/archive/2008/11/17/anonymous-variables.aspx"&gt;Anonymous
Variables&lt;/a&gt; 
&lt;li&gt;
&lt;a href="http://blogs.msdn.com/ploeh/archive/2008/11/21/ignore-irrelevant-return-values.aspx"&gt;Ignore
Irrelevant Return Values&lt;/a&gt; 
&lt;li&gt;
&lt;a href="http://blogs.msdn.com/ploeh/archive/2008/12/01/testmethod-code-snippet.aspx"&gt;testmethod
Code Snippet&lt;/a&gt; 
&lt;li&gt;
&lt;a href="http://blogs.msdn.com/ploeh/archive/2008/12/08/3-is-many.aspx"&gt;3 Is Many&lt;/a&gt; 
&lt;li&gt;
&lt;a href="http://blogs.msdn.com/ploeh/archive/2008/12/11/why-use-areequal-t.aspx"&gt;Why
Use AreEqual&amp;lt;T&amp;gt;?&lt;/a&gt; 
&lt;li&gt;
&lt;a href="http://blogs.msdn.com/ploeh/archive/2008/12/17/assert-messages-are-not-optional.aspx"&gt;Assert
Messages Are Not Optional&lt;/a&gt; 
&lt;li&gt;
&lt;a href="http://blogs.msdn.com/ploeh/archive/2009/01/05/use-the-generate-method-stub-smart-tag-to-stay-in-the-zone.aspx"&gt;Use
The Generate Method Stub Smart Tag To Stay In The Zone&lt;/a&gt; 
&lt;li&gt;
&lt;a href="http://blogs.msdn.com/ploeh/archive/2009/01/12/test-driven-properties.aspx"&gt;Test-Driven
Properties&lt;/a&gt; 
&lt;li&gt;
&lt;a href="http://blog.ploeh.dk/2009/02/13/SUTFactory.aspx"&gt;SUT Factory&lt;/a&gt; 
&lt;li&gt;
&lt;a href="http://blog.ploeh.dk/2009/03/03/DerivedValuesEnsureExecutableSpecification.aspx"&gt;Derived
Values Ensure Executable Specification&lt;/a&gt; 
&lt;li&gt;
&lt;a href="http://blog.ploeh.dk/2009/03/05/ConstrainedNonDeterminism.aspx"&gt;Constrained
Non-Determinism&lt;/a&gt; 
&lt;li&gt;
&lt;a href="http://blog.ploeh.dk/2009/03/11/ExplicitExpectations.aspx"&gt;Explicit Expectations&lt;/a&gt; 
&lt;li&gt;
&lt;a href="http://blog.ploeh.dk/2009/03/16/FixtureObject.aspx"&gt;Fixture Object&lt;/a&gt; 
&lt;li&gt;
&lt;a href="http://blog.ploeh.dk/2009/05/15/AutoFixtureAsFixtureObject.aspx"&gt;AutoFixture
As Fixture Object&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=b0a3b644-dd52-46c4-91cf-8261c11431b3" /&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,b0a3b644-dd52-46c4-91cf-8261c11431b3.aspx</comments>
      <category>Productivity</category>
      <category>Unit Testing</category>
    </item>
  </channel>
</rss>