<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Davepoint &#187; testing</title>
	<atom:link href="http://blog.ceredir.com/index.php/tag/testing/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.ceredir.com</link>
	<description></description>
	<lastBuildDate>Tue, 21 Sep 2010 14:19:41 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Removing types from Unity in unit tests</title>
		<link>http://blog.ceredir.com/index.php/2010/04/27/removing-types-from-unity-in-unit-tests/</link>
		<comments>http://blog.ceredir.com/index.php/2010/04/27/removing-types-from-unity-in-unit-tests/#comments</comments>
		<pubDate>Tue, 27 Apr 2010 07:14:34 +0000</pubDate>
		<dc:creator>David Bending</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[tdd]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[Unity]]></category>

		<guid isPermaLink="false">http://blog.ceredir.com/?p=105</guid>
		<description><![CDATA[Calling teardown on the UnityContainer doesn’t remove a registration from Unity.  Here’s how to unit test code that resolves a type configured by RegisterInstance rather than configuration.  IoC is a thin wrapper around the UnityContainer.

        private ControllerContext _controllerContext;
        private ModelMetadata _modelMetadata;
        private Proposer _proposer;
        private ILookup _lookup;
        private LifetimeManager _lifetimeManager;

        [TestInitialize]
        public void SetupTest()
        {
            [...]]]></description>
			<content:encoded><![CDATA[<p>Calling teardown on the UnityContainer doesn’t remove a registration from Unity.  Here’s how to unit test code that resolves a type configured by RegisterInstance rather than configuration.  IoC is a thin wrapper around the UnityContainer.</p>
<pre class="brush: c-sharp">
        private ControllerContext _controllerContext;
        private ModelMetadata _modelMetadata;
        private Proposer _proposer;
        private ILookup _lookup;
        private LifetimeManager _lifetimeManager;

        [TestInitialize]
        public void SetupTest()
        {
            _controllerContext = new ControllerContext();
            _modelMetadata =
                ModelMetadataProviders.Current.GetMetadataForType(() =&gt; _proposer.AcceptedTerms, typeof (bool));
            _proposer = new Proposer();
            _lifetimeManager = new ContainerControlledLifetimeManager();
            _lookup = MockRepository.GenerateStrictMock&lt;ILookup&gt;();
            IoC.UnityContainer.RegisterInstance(typeof(ILookup), _lookup, _lifetimeManager);
        }
</pre>
<pre class="brush: c-sharp">        [TestCleanup]
        public void VerifyExpectations()
        {
            _lifetimeManager.RemoveValue();
            _lookup.VerifyAllExpectations();
         }       </pre>
<pre class="brush: c-sharp">        [TestMethod]<br />
        public void ErrorMessageIsCorrectlyGenerated()<br />
        {<br />
            var target = new NotDefaultIfAttribute("test/notDefaultIf")<br />
            {<br />
                OtherProperty = "Surname"<br />
            };<br />
            _lookup.Expect(l =&gt; l.GetLabel("test/notDefaultIf")).Return("Blah blah {0} blah");<br />
            Assert.AreEqual("Blah blah AcceptedTerms blah", target.FormatErrorMessage("AcceptedTerms"));<br />
        }</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://blog.ceredir.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://blog.ceredir.com/index.php/2010/04/27/removing-types-from-unity-in-unit-tests/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Pex &#8211; Some Early Thoughts</title>
		<link>http://blog.ceredir.com/index.php/2010/02/02/pex-some-early-thoughts/</link>
		<comments>http://blog.ceredir.com/index.php/2010/02/02/pex-some-early-thoughts/#comments</comments>
		<pubDate>Tue, 02 Feb 2010 09:57:41 +0000</pubDate>
		<dc:creator>David Bending</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[pex]]></category>
		<category><![CDATA[tdd]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://blog.ceredir.com/?p=81</guid>
		<description><![CDATA[I&#39;ve been looking recently at Pex, Microsoft&#39;s automated white box testing software. There&#39;s basically&#160;4 parts to Pex:

A framework for writing parameterised unit tests.
An explorer, for finding boundary conditions in code.
Suggestions of preconditions that should be added to methods.
Moles &#8211; a way of stubbing even static properties and methods (such as DateTime.Now).

Parameterised Unit Tests (PUT)
When manually [...]]]></description>
			<content:encoded><![CDATA[<p>I&#39;ve been looking recently at <a href="http://research.microsoft.com/en-us/projects/Pex/">Pex</a>, Microsoft&#39;s automated white box testing software. There&#39;s basically&nbsp;4 parts to Pex:</p>
<ol>
<li>A framework for writing parameterised unit tests.</li>
<li>An explorer, for finding boundary conditions in code.</li>
<li>Suggestions of preconditions that should be added to methods.</li>
<li>Moles &#8211; a way of stubbing even static properties and methods (such as DateTime.Now).</li>
</ol>
<h2>Parameterised Unit Tests (PUT)</h2>
<p>When manually writing unit tests, we often end up with blocks of tests that are essentially the same in concept, but with different input/output combinations.&nbsp; For example, an addition routine might have tests for different combination of negative and positive numbers.&nbsp; Pex creates its tests a <em>parameterized</em> tests.&nbsp; There is a template test, and underneath that an instance of each test value.</p>
<p>The templates are placed in a source file and look like normal unit tests.&nbsp; The parameterised versions are stored underneath in a .g.cs file.&nbsp; These files are not intended to be edited and are created automatically by the Pex explorer.</p>
<p>You can also write your own PUTs.&nbsp; More on this later&#8230;</p>
<h2>The Explorer</h2>
<p>The &#39;white box&#39; part of Pex refers to the explorer.&nbsp; This essentially looks at all the possible paths through a piece of code and generates a test for each. Let&#39;s look at this piece of code:</p>
<pre class="brush: c-sharp">public int Add(int x, int y)
{
    return x + y;
}</pre>
<p>If we run Pex on this, we get the following test:</p>
<pre class="brush: c-sharp">[TestMethod]
[PexGeneratedBy(typeof(CalculatorTest))]
public void Add01()
{
    int i;
    Calculator s0 = new Calculator();
    i = this.Add(s0, 0, 0);
    Assert.AreEqual&lt;int&gt;(0, i);
    Assert.IsNotNull((object)s0);
}</pre>
<p>Actually there&#39;s a bit more to it &#8211; but this shows the basic details.&nbsp; As we can see, it&#39;s a fairly dull test.&nbsp; in particular Pex hasn&#39;t added any code to see if the result of the operation has overflowed. On the other hand let&#39;s see what it does if there&#39;s a bit more going on in the method:</p>
<pre class="brush: c-sharp">public int Fibonacci(int n)
{
    if (n &lt; 0)
    {
        throw new ArgumentOutOfRangeException(&quot;n&quot;);
    }

    if (n == 0)
    {
        return 0;
    }

    if (n == 1)
    {
        return 1;
    }

    return Fibonacci(n - 2) + Fibonacci(n - 1);
}</pre>
<p>This is an (admiteddly rubbish) implementation of Fibonacci. It&#39;s more interesting that Add because there&#39;s more paths through it. This time when we run Pex we get:</p>
<pre class="brush: c-sharp">[TestMethod]
[PexGeneratedBy(typeof(CalculatorTest))]
public void Fibonacci01()
{
    int i;
    Calculator s0 = new Calculator();
    i = this.Fibonacci(s0, 0);
    Assert.AreEqual&lt;int&gt;(0, i);
    Assert.IsNotNull((object)s0);
}

[TestMethod]
[PexGeneratedBy(typeof(CalculatorTest))]
public void Fibonacci02()
{
    int i;
    Calculator s0 = new Calculator();
    i = this.Fibonacci(s0, 1);
    Assert.AreEqual&lt;int&gt;(1, i);
    Assert.IsNotNull((object)s0);
}

[TestMethod]
[PexGeneratedBy(typeof(CalculatorTest))]
public void Fibonacci03()
{
    int i;
    Calculator s0 = new Calculator();
    i = this.Fibonacci(s0, 2);
    Assert.AreEqual&lt;int&gt;(1, i);
    Assert.IsNotNull((object)s0);
}

[TestMethod]
[PexGeneratedBy(typeof(CalculatorTest))]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void Fibonacci04()
{
    int i;
    Calculator s0 = new Calculator();
    i = this.Fibonacci(s0, int.MinValue);
}

[TestMethod]
[PexGeneratedBy(typeof(CalculatorTest))]
public void Fibonacci05()
{
    int i;
    Calculator s0 = new Calculator();
    i = this.Fibonacci(s0, 14);
    Assert.AreEqual&lt;int&gt;(377, i);
    Assert.IsNotNull((object)s0);
}&nbsp;</pre>
<p>So Pex has created a test case for each path through the program.</p>
<h2>Suggestions</h2>
<p>When passing reference types to a method, Pex will realise that the value could be null.&nbsp; For all such methods it will suggest the a pre-condition is added.&nbsp; There&#39;s also a button that will get Pex to automatically insert the pre-condition into the code for you.&nbsp; This saves a bit of time, but isn&#39;t a huge win.&nbsp; That said Pex does understand code contracts so there may be bigger wins to be had here.</p>
<h2>Moles</h2>
<p>Pex also contains a mocking framework called Moles.&nbsp; Unlike most mocking frameworks, Moles do their work at runtime level (like Isolator) and consequently you can use them to mock static properties.&nbsp; Consider this code:</p>
<pre class="brush: c-sharp">public void Y2kBug()
{
    if (DateTime.Now == new DateTime(1999, 12, 31))
    {
        throw new InvalidProgramException(&quot;Arrgghhh!!!!&quot;);
    }
}&nbsp;</pre>
<p>We&#39;d like to unit test this, and we don&#39;t want to have to change the system clock to do so.&nbsp; Unfortunately, DateTime.Now is static so the likes of Rhino Mocks and MoQ can&#39;t help.&nbsp; With Pex Moles we can do the following:</p>
<pre class="brush: c-sharp">[TestMethod]
[ExpectedException(typeof(InvalidProgramException))]
[HostType(&quot;Moles&quot;)]
public void Y2kBugTest()
{
    MDateTime.NowGet = () =&gt; new DateTime(1999, 12, 31);

    Y2k target = new Y2k();
    target.Y2kBug();
} </pre>
<p>We have generated Moles for mscorlib (by choosing Add New Item -&gt; Pex Moles) in Visual Studio.&nbsp; This creates a mole for each mscorlib type in the Moles namespace with the M prefix.&nbsp; We then tell the mole to return the value we ant in the unit test (note that we supply a delegate not a value, and that properties are a Put and a Get mole).&nbsp; By specifying the <code>HostType </code>attribute for our test, we cause the unit test runner to route the code trhough a mole supplying the value we want.</p>
<p>See Nikolai Tellman&#39;s video for more on this at: <a href="http://channel9.msdn.com/posts/Peli/Moles-Replace-any-NET-method-with-a-delegate/">http://channel9.msdn.com/posts/Peli/Moles-Replace-any-NET-method-with-a-delegate/</a>.</p>
<p>
	&nbsp;</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://blog.ceredir.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://blog.ceredir.com/index.php/2010/02/02/pex-some-early-thoughts/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Adobe BrowserLab</title>
		<link>http://blog.ceredir.com/index.php/2010/01/20/adobe-browserlab/</link>
		<comments>http://blog.ceredir.com/index.php/2010/01/20/adobe-browserlab/#comments</comments>
		<pubDate>Wed, 20 Jan 2010 13:30:29 +0000</pubDate>
		<dc:creator>David Bending</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://blog.ceredir.com/?p=60</guid>
		<description><![CDATA[I got forwarded this link to Adobe BrowserLab.  Is a great way of seeing how your site looks in different browsers.  Thanks Nick!
Adobe Labs &#8211; Adobe BrowserLab.
]]></description>
			<content:encoded><![CDATA[<p>I got forwarded this link to Adobe BrowserLab.  Is a great way of seeing how your site looks in different browsers.  Thanks Nick!</p>
<p><a href="http://labs.adobe.com/technologies/browserlab/">Adobe Labs &#8211; Adobe BrowserLab</a>.</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://blog.ceredir.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://blog.ceredir.com/index.php/2010/01/20/adobe-browserlab/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

