<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://wotudo.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Search results matching tags 'Programming' and 'guest'</title><link>http://wotudo.net/search/SearchResults.aspx?o=DateDescending&amp;tag=Programming,guest&amp;orTags=0</link><description>Search results matching tags 'Programming' and 'guest'</description><dc:language>en-US</dc:language><generator>CommunityServer 2007 (Build: 20423.869)</generator><item><title>Pex – Effective Unit Testing for .NET</title><link>http://wotudo.net/blogs/wotudo/archive/2010/02/05/pex-effective-unit-testing-for-net.aspx</link><pubDate>Fri, 05 Feb 2010 13:24:00 GMT</pubDate><guid isPermaLink="false">2f2f3f54-a0d5-494d-ad23-22a6d9c85854:813</guid><dc:creator>paulfo</dc:creator><description>&lt;p&gt;Unit testing is a software development methodology where the developer writes tests that validate a small portion of code. A unit test is usually composed of three ingredients: (1) data such as strings or integers, (2) a sequence of method calls that exercise the code under test and (3) assertions that verify the behaviour.&lt;/p&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;int&lt;/span&gt; AddBroken(&lt;span class="kwrd"&gt;int&lt;/span&gt; x, &lt;span class="kwrd"&gt;int&lt;/span&gt; y) { 
    &lt;span class="kwrd"&gt;if&lt;/span&gt; (x &amp;gt; 1000) x = 1000; &lt;span class="rem"&gt;// it’s too big! &lt;/span&gt;
    &lt;span class="kwrd"&gt;return&lt;/span&gt; x + y; 
} 
[TestMethod] &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; AddOneTwo () { 
    &lt;span class="kwrd"&gt;int&lt;/span&gt; z = AddBroken(1, 2); 
    Assert.AreEqual(3, z); 
} // &lt;span class="kwrd"&gt;this&lt;/span&gt; test passes!&lt;/pre&gt;&lt;br /&gt;
&lt;p&gt;In many cases, the data provided by the developer is irrelevant, partial or redundant. Pex is an automated test generation tool for .NET that automatically generates the data for your unit tests.&lt;/p&gt;
&lt;p&gt;Given a parameterized unit test, i.e. a unit test method with parameters, Pex tries to generate data inputs to cover all reachable branches. To achieve this, Pex analyses the code as it is running and uses the observed program behavior to systematically generate an exhaustive set of data inputs that will cover many program branches. Since every assertion in the code is actually a conditional branch, Pex also finds bugs by trying to determine inputs that will cause assertion to fail, in the code or in the tests. In the end, Pex produces a small test suite that achieves high code coverage.&lt;/p&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;span class="rem"&gt;// parameterized unit test written by hand &lt;/span&gt;
[PexMethod] &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; AddAnyNumbers(&lt;span class="kwrd"&gt;int&lt;/span&gt; x, &lt;span class="kwrd"&gt;int&lt;/span&gt; y) { 
&lt;span class="rem"&gt;// this test should pass for any x and y &lt;/span&gt;
    &lt;span class="kwrd"&gt;int&lt;/span&gt; z = AddBroken(x, y); 
    Assert.AreEqual(x + y, z); 
}

&lt;span class="rem"&gt;// automatically generated unit test by Pex &lt;/span&gt;
[TestMethod] &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; AddAnyNumbers01() { 
    AddAnyNumbers(1001, 0); 
} &lt;span class="rem"&gt;// this test fails the assert 1001 + 0 != 1000!&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;
&lt;p&gt;Unit tests should be executable in isolation from the environment. However, in practice, many unit tests are actually integration tests as they communicate with a database or rely on the file system or even the current machine time. Such unit tests are usually slow, non-deterministic, hard to setup and diagnose. To deal with these issues, developers should introduce abstraction layers, i.e. interfaces, between the code and the environment. To greatly simplify this task, Pex provides a framework, Stubs, which automatically generates implementations of the interfaces. In many other cases, environment dependencies are hard-coded, without using interfaces. For those scenarios, if it is not feasible introduce abstraction layers, Pex provides a framework, Moles, that allows replacing any .NET method with a user-defined delegate, in effect detouring calls to environment-facing methods at test time. This effectively isolates unit tests without changing the application code, enabling testing of SharePoint applications, ASP.NET, etc.&lt;/p&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;int&lt;/span&gt; AddBroken(&lt;span class="kwrd"&gt;int&lt;/span&gt; x, &lt;span class="kwrd"&gt;int&lt;/span&gt; y) { 
    &lt;span class="kwrd"&gt;if&lt;/span&gt; (DateTime.Now == &lt;span class="kwrd"&gt;new&lt;/span&gt; DateTime(2000,1,1)) &lt;span class="kwrd"&gt;throw&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; Exception(“y2k bug!”); 
    &lt;span class="kwrd"&gt;return&lt;/span&gt; x + y; 
} 
&lt;span class="rem"&gt;// parameterized unit test written by hand &lt;/span&gt;
[PexMethod] &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; AddAnyNumbers(&lt;span class="kwrd"&gt;int&lt;/span&gt; x, &lt;span class="kwrd"&gt;int&lt;/span&gt; y, DateTime now) { 
&lt;span class="rem"&gt;// this test should pass for any x, y and current time ‘now’ &lt;/span&gt;
    MDateTime.NowGet = () =&amp;gt; { &lt;span class="kwrd"&gt;return&lt;/span&gt; now; }; 
&lt;span class="rem"&gt;// let’s make DateTime.Now return ‘now’ always &lt;/span&gt;
    &lt;span class="kwrd"&gt;int&lt;/span&gt; z = AddBroken(x, y); 
    Assert.AreEqual(x + y, z); 
}&lt;/pre&gt;&lt;br /&gt;
&lt;p&gt;Pex is an incubation project for Visual Studio, developed by Microsoft Research, and currently available via &lt;a href="http://msdn.microsoft.com/devlabs"&gt;http://msdn.microsoft.com/devlabs&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;A guest post by: Peli de Halleux, Senior Research Software Developer Engineer, Microsoft Research, Redmond.&lt;/p&gt;
&lt;div style="TEXT-ALIGN:right;PADDING-BOTTOM:4px;MARGIN:0px;PADDING-LEFT:0px;PADDING-RIGHT:0px;PADDING-TOP:4px;" class="wlWriterHeaderFooter"&gt;&lt;a href="http://digg.com/submit?url=http%3a%2f%2fwotudo.net%2fblogs%2fwotudo%2farchive%2f2010%2f02%2f05%2fpex-effective-unit-testing-for-net.aspx&amp;amp;title=Pex+%e2%80%93+Effective+Unit+Testing+for+.NET"&gt;&lt;img style="BORDER-BOTTOM:0px;BORDER-LEFT:0px;BORDER-TOP:0px;BORDER-RIGHT:0px;" title="Digg This" border="0" alt="Digg This" src="http://digg.com/img/badges/100x20-digg-button.png" width="100" height="20" /&gt;&lt;/a&gt;&lt;/div&gt;</description></item></channel></rss>