<?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:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>Peli's Farm - Pex, Stubs, Moles, QuickGraph, MbUnit, Reflector Addins - Testing</title>
    <link>http://blog.dotnetwiki.org/</link>
    <description>TouchDevelop, Pex4Fun, Rise4Fun, Pex, Moles, QuickGraph, MbUnit, Reflector Addins</description>
    <language>en-us</language>
    <copyright>Jonathan 'Peli' de Halleux</copyright>
    <lastBuildDate>Mon, 22 Nov 2010 23:45:12 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.2.8279.16125</generator>
    <managingEditor>jonathan.dehalleux@gmail.com</managingEditor>
    <webMaster>jonathan.dehalleux@gmail.com</webMaster>
    <item>
      <trackback:ping>http://blog.dotnetwiki.org/Trackback.aspx?guid=575b1084-7ec4-4913-a6fe-869df8a4fb23</trackback:ping>
      <pingback:server>http://blog.dotnetwiki.org/pingback.aspx</pingback:server>
      <pingback:target>http://blog.dotnetwiki.org/PermaLink,guid,575b1084-7ec4-4913-a6fe-869df8a4fb23.aspx</pingback:target>
      <dc:creator>Jonathan de Halleux</dc:creator>
      <wfw:comment>http://blog.dotnetwiki.org/CommentView,guid,575b1084-7ec4-4913-a6fe-869df8a4fb23.aspx</wfw:comment>
      <wfw:commentRss>http://blog.dotnetwiki.org/SyndicationService.asmx/GetEntryCommentsRss?guid=575b1084-7ec4-4913-a6fe-869df8a4fb23</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Ever wanted to host a constraint solver in your web page? It is now possible to host
the <a href="http://rise4fun.com">http://rise4fun.com</a> web site in an iframe (without
the chrome). It just looks like this:
</p>
        <blockquote>
          <p align="left">
&lt;iframe allowtransparency="true" frameborder="0" style="width:600px;height:800px"
src=”http://rise4fun.com/z3?frame=1&amp;menu=0”&gt;&lt;/iframe&gt;
</p>
        </blockquote>
        <p align="left">
You can learn more about this feature at our <a href="http://rise4fun.com/about">documentation
page</a>.
</p>
        <iframe style="width: 600px; height: 800px" src="http://rise4fun.com/z3?frame=1&amp;menu=0" frameborder="0" allowtransparency="allowtransparency">
        </iframe>
        <img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=575b1084-7ec4-4913-a6fe-869df8a4fb23" />
      </body>
      <title>The RiSE4fun widget – ever wanted to have a constraint solver in your blog?</title>
      <guid isPermaLink="false">http://blog.dotnetwiki.org/PermaLink,guid,575b1084-7ec4-4913-a6fe-869df8a4fb23.aspx</guid>
      <link>http://blog.dotnetwiki.org/2010/11/22/TheRiSE4funWidgetEverWantedToHaveAConstraintSolverInYourBlog.aspx</link>
      <pubDate>Mon, 22 Nov 2010 23:45:12 GMT</pubDate>
      <description>&lt;p&gt;
Ever wanted to host a constraint solver in your web page? It is now possible to host
the &lt;a href="http://rise4fun.com"&gt;http://rise4fun.com&lt;/a&gt; web site in an iframe (without
the chrome). It just looks like this:
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p align="left"&gt;
&amp;lt;iframe allowtransparency="true" frameborder="0" style="width:600px;height:800px"
src=”http://rise4fun.com/z3?frame=1&amp;amp;menu=0”&amp;gt;&amp;lt;/iframe&amp;gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p align="left"&gt;
You can learn more about this feature at our &lt;a href="http://rise4fun.com/about"&gt;documentation
page&lt;/a&gt;.
&lt;/p&gt;
&lt;iframe style="width: 600px; height: 800px" src="http://rise4fun.com/z3?frame=1&amp;amp;menu=0" frameborder="0" allowtransparency&gt;
&lt;/iframe&gt;
&lt;img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=575b1084-7ec4-4913-a6fe-869df8a4fb23" /&gt;</description>
      <comments>http://blog.dotnetwiki.org/CommentView,guid,575b1084-7ec4-4913-a6fe-869df8a4fb23.aspx</comments>
      <category>RiSE</category>
      <category>Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.dotnetwiki.org/Trackback.aspx?guid=e773543a-5f34-4b8f-bb64-03cdda2f696c</trackback:ping>
      <pingback:server>http://blog.dotnetwiki.org/pingback.aspx</pingback:server>
      <pingback:target>http://blog.dotnetwiki.org/PermaLink,guid,e773543a-5f34-4b8f-bb64-03cdda2f696c.aspx</pingback:target>
      <dc:creator>Jonathan de Halleux</dc:creator>
      <wfw:comment>http://blog.dotnetwiki.org/CommentView,guid,e773543a-5f34-4b8f-bb64-03cdda2f696c.aspx</wfw:comment>
      <wfw:commentRss>http://blog.dotnetwiki.org/SyndicationService.asmx/GetEntryCommentsRss?guid=e773543a-5f34-4b8f-bb64-03cdda2f696c</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Is there anything else to say than… try it out now at <a href="http://www.pexforfun.com">http://www.pexforfun.com</a> !
</p>
        <p>
          <a href="http://www.pexforfun.com/">
            <img title="image" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/CommunityServer-Blogs-Components-WeblogFiles/00-00-00-98-41-metablogapi/2273.image_5F00_7F597B02.png" width="668" height="443" />
          </a>
        </p>
        <img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=e773543a-5f34-4b8f-bb64-03cdda2f696c" />
      </body>
      <title>www.pexforfun.com–&gt; try Pex in your browser</title>
      <guid isPermaLink="false">http://blog.dotnetwiki.org/PermaLink,guid,e773543a-5f34-4b8f-bb64-03cdda2f696c.aspx</guid>
      <link>http://blog.dotnetwiki.org/2010/06/28/wwwpexforfuncomTryPexInYourBrowser.aspx</link>
      <pubDate>Mon, 28 Jun 2010 21:35:18 GMT</pubDate>
      <description>&lt;p&gt;
Is there anything else to say than… try it out now at &lt;a href="http://www.pexforfun.com"&gt;http://www.pexforfun.com&lt;/a&gt; !
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.pexforfun.com/"&gt;&lt;img title="image" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/CommunityServer-Blogs-Components-WeblogFiles/00-00-00-98-41-metablogapi/2273.image_5F00_7F597B02.png" width="668" height="443"&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=e773543a-5f34-4b8f-bb64-03cdda2f696c" /&gt;</description>
      <comments>http://blog.dotnetwiki.org/CommentView,guid,e773543a-5f34-4b8f-bb64-03cdda2f696c.aspx</comments>
      <category>Pex</category>
      <category>Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.dotnetwiki.org/Trackback.aspx?guid=03301296-7476-4b15-bd61-6ea831730d47</trackback:ping>
      <pingback:server>http://blog.dotnetwiki.org/pingback.aspx</pingback:server>
      <pingback:target>http://blog.dotnetwiki.org/PermaLink,guid,03301296-7476-4b15-bd61-6ea831730d47.aspx</pingback:target>
      <dc:creator>Jonathan de Halleux</dc:creator>
      <wfw:comment>http://blog.dotnetwiki.org/CommentView,guid,03301296-7476-4b15-bd61-6ea831730d47.aspx</wfw:comment>
      <wfw:commentRss>http://blog.dotnetwiki.org/SyndicationService.asmx/GetEntryCommentsRss?guid=03301296-7476-4b15-bd61-6ea831730d47</wfw:commentRss>
      <slash:comments>2</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
We’ve just <a href="http://research.microsoft.com/pex/downloads.aspx" target="_blank">released</a> a
new version of <a href="http://research.microsoft.com/pex/">Pex</a> and <a href="http://research.microsoft.com/moles" target="_blank">Moles</a> v0.92.
This version brings <a href="http://research.microsoft.com/rex" target="_blank">Rex</a> integration
(smarter about regular expressions), <strong>Silverlight support</strong> (Alpha)
and a number of bugs/improvements here and there.
</p>
        <p>
Read all about the new stuff on the <a href="http://research.microsoft.com/en-us/projects/pex/releasenotes.aspx#0_92" target="_blank">release
notes page</a>. Happy Pexing!
</p>
        <img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=03301296-7476-4b15-bd61-6ea831730d47" />
      </body>
      <title>New Pex release 0.92: Rex and Silverlight</title>
      <guid isPermaLink="false">http://blog.dotnetwiki.org/PermaLink,guid,03301296-7476-4b15-bd61-6ea831730d47.aspx</guid>
      <link>http://blog.dotnetwiki.org/2010/06/07/NewPexRelease092RexAndSilverlight.aspx</link>
      <pubDate>Mon, 07 Jun 2010 19:13:52 GMT</pubDate>
      <description>&lt;p&gt;
We’ve just &lt;a href="http://research.microsoft.com/pex/downloads.aspx" target="_blank"&gt;released&lt;/a&gt; a
new version of &lt;a href="http://research.microsoft.com/pex/"&gt;Pex&lt;/a&gt; and &lt;a href="http://research.microsoft.com/moles" target="_blank"&gt;Moles&lt;/a&gt; v0.92.
This version brings &lt;a href="http://research.microsoft.com/rex" target="_blank"&gt;Rex&lt;/a&gt; integration
(smarter about regular expressions), &lt;strong&gt;Silverlight support&lt;/strong&gt; (Alpha)
and a number of bugs/improvements here and there.
&lt;/p&gt;
&lt;p&gt;
Read all about the new stuff on the &lt;a href="http://research.microsoft.com/en-us/projects/pex/releasenotes.aspx#0_92" target="_blank"&gt;release
notes page&lt;/a&gt;. Happy Pexing!
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=03301296-7476-4b15-bd61-6ea831730d47" /&gt;</description>
      <comments>http://blog.dotnetwiki.org/CommentView,guid,03301296-7476-4b15-bd61-6ea831730d47.aspx</comments>
      <category>Moles</category>
      <category>Pex</category>
      <category>Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.dotnetwiki.org/Trackback.aspx?guid=0b1d07f2-5096-4b10-9550-a598875fadee</trackback:ping>
      <pingback:server>http://blog.dotnetwiki.org/pingback.aspx</pingback:server>
      <pingback:target>http://blog.dotnetwiki.org/PermaLink,guid,0b1d07f2-5096-4b10-9550-a598875fadee.aspx</pingback:target>
      <dc:creator>Jonathan de Halleux</dc:creator>
      <wfw:comment>http://blog.dotnetwiki.org/CommentView,guid,0b1d07f2-5096-4b10-9550-a598875fadee.aspx</wfw:comment>
      <wfw:commentRss>http://blog.dotnetwiki.org/SyndicationService.asmx/GetEntryCommentsRss?guid=0b1d07f2-5096-4b10-9550-a598875fadee</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
We just uploaded a new release 0.91 on MSDN, Visual Studio gallery and our research
web site. Learn more about the changes at <a href="http://research.microsoft.com/en-us/projects/pex/releasenotes.aspx#0_91">http://research.microsoft.com/en-us/projects/pex/releasenotes.aspx#0_91</a></p>
        <blockquote>
          <p>
            <a href="http://research.microsoft.com/en-us/projects/pex/downloads.aspx">
              <strong>Get
and download Pex!</strong>
            </a>
          </p>
        </blockquote>
        <img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=0b1d07f2-5096-4b10-9550-a598875fadee" />
      </body>
      <title>Pex 0.91 is out!</title>
      <guid isPermaLink="false">http://blog.dotnetwiki.org/PermaLink,guid,0b1d07f2-5096-4b10-9550-a598875fadee.aspx</guid>
      <link>http://blog.dotnetwiki.org/2010/04/24/Pex091IsOut.aspx</link>
      <pubDate>Sat, 24 Apr 2010 13:51:44 GMT</pubDate>
      <description>&lt;p&gt;
We just uploaded a new release 0.91 on MSDN, Visual Studio gallery and our research
web site. Learn more about the changes at &lt;a href="http://research.microsoft.com/en-us/projects/pex/releasenotes.aspx#0_91"&gt;http://research.microsoft.com/en-us/projects/pex/releasenotes.aspx#0_91&lt;/a&gt; 
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
&lt;a href="http://research.microsoft.com/en-us/projects/pex/downloads.aspx"&gt;&lt;strong&gt;Get
and download Pex!&lt;/strong&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;/blockquote&gt;&lt;img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=0b1d07f2-5096-4b10-9550-a598875fadee" /&gt;</description>
      <comments>http://blog.dotnetwiki.org/CommentView,guid,0b1d07f2-5096-4b10-9550-a598875fadee.aspx</comments>
      <category>Moles</category>
      <category>Pex</category>
      <category>Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.dotnetwiki.org/Trackback.aspx?guid=ec9069cd-89d0-4958-9894-e792aeda0abc</trackback:ping>
      <pingback:server>http://blog.dotnetwiki.org/pingback.aspx</pingback:server>
      <pingback:target>http://blog.dotnetwiki.org/PermaLink,guid,ec9069cd-89d0-4958-9894-e792aeda0abc.aspx</pingback:target>
      <dc:creator>Jonathan de Halleux</dc:creator>
      <wfw:comment>http://blog.dotnetwiki.org/CommentView,guid,ec9069cd-89d0-4958-9894-e792aeda0abc.aspx</wfw:comment>
      <wfw:commentRss>http://blog.dotnetwiki.org/SyndicationService.asmx/GetEntryCommentsRss?guid=ec9069cd-89d0-4958-9894-e792aeda0abc</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
We just <a href="http://research.microsoft.com/en-us/projects/pex/downloads.aspx">released
Pex 0.21.50115.2</a>. This release brings bug fixes, a big renaming from “Stubs” to
“Moles” and improved infrastructure to build behaved types (formerly known as beavers).
</p>
        <p>
          <strong>Bug Fixes</strong>
        </p>
        <ul>
          <li>
The Moles VsHost fails to execute unit tests in different assemblies. 
</li>
          <li>
            <a href="http://research.microsoft.com/pex/">Pex</a> deletes the report folder it
is currently writing to. 
</li>
          <li>
Support for named indexers in Stubs 
</li>
          <li>
Fixed bugs in how Pex reasons about System.Convert.ToBase64, DateTime 
</li>
          <li>
Invalid support for protected members in the stubs generation</li>
        </ul>
        <p>
          <strong>Breaking changes</strong>
        </p>
        <ul>
          <li>
            <strong>The Stubs framework was renamed to Moles framework.</strong> We have decided
to make the Moles the center of the framework and as a consequence, renamed ‘Stubs’
to ‘Moles’. (This does not mean that we encourage writing untestable code, as Mole
help to make it testable. You should still refactor your code to make it testable
whenever possible, and only use Moles when that’s the only choice). The impact is
that 
<ul><li>
Microsoft.Stubs.Framework was renamed to Microsoft.Moles.Framework 
</li><li>
The moles and stubs get generated in subnamespaces ‘.Moles’ rather ‘.Stubs’. 
</li><li>
See below for the list of steps to upgrade your applications.</li></ul></li>
          <li>
            <strong>BaseMembers in Moles have been deprecated</strong>: this helper is not useful
as it can be acheive in a better way through a constructor. We decided to remove it
to reduce code size. The second reason is that BaseMembers would only work for types
inside of the same assembly, which might seem inconsistent. 
</li>
          <li>
            <strong>PexGoal.Reached is replaced by PexAssert.ReachEventually().</strong> The PexGoal
class has been integrated into PexAssert through the ReachEventually method which
should be used with the [PexAssertReachEventually] attribute. 
</li>
          <li>
            <strong>PexChoose simplified</strong>: we’ve simplified the PexChoose API; you can
now get auxiliary test inputs with a single method call: PexChoose.Value&lt;T&gt;(“foo”).</li>
        </ul>
        <p>
          <strong>Migrating from previous version of Pex</strong>
        </p>
        <p>
Since we’ve renamed Stubs to Moles, any existing .stubx files will not work anymore.
</p>
        <p>
Take a deep breath, and apply the following steps to adapt your projects:
</p>
        <ul>
          <li>
change the project reference from Microsoft.Stubs.Framework.dll to Microsoft.Moles.Framework.dll 
</li>
          <li>
rename all .stubx files to .moles, and 
<ul><li>
rename the top <strong>&lt;Stubs</strong> xml element to <strong>&lt;Moles</strong>. 
</li><li>
Change the XSD namespace to <a href="http://schemas.microsoft.com/moles/2010/">http://schemas.microsoft.com/moles/2010/</a></li><li>
Right click on the .moles file in the Solution Explorer and change the Custom Tool
Name to ‘MolesGenerator’. 
</li><li>
Delete all the nested files under the .moles files</li></ul></li>
          <li>
Remove references to any compiled .Stubs.dll files in your project 
</li>
          <li>
In general, remove all .Stubs.dll, .Stubs.xml files from your projects. 
</li>
          <li>
Rename .Stubs namespace suffixes to .Moles. 
</li>
          <li>
replace all [HostType(“Pex”)] attribute with [HostType(“Moles”)] 
</li>
          <li>
in PexAssemblyInfo.cs, 
<ul><li>
rename using Microsoft.Pex.Framework.Stubs to Microsoft.Pex.Framework.Moles 
</li><li>
rename [assembly: PexChooseAsStubFallbackBehavior] to [assembly: PexChooseAsBehavedCurrentBehavior] 
</li><li>
rename [assembly: PexChooseAsStubFallbackBehavior] to [assembly: PexChooseAsMoleCurrentBehavior]</li></ul></li>
          <li>
In general, the ‘Fallback’ prefix has been dropped in the following methods: 
<ul><li>
rename FallbackAsNotImplemented() to BehaveAsNotImplemented() 
</li><li>
rename class MoleFallbackBehavior to MoleBehaviors 
</li><li>
rename class StubFallbackBehavior to BehavedBehavors</li></ul></li>
        </ul>
        <img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=ec9069cd-89d0-4958-9894-e792aeda0abc" />
      </body>
      <title>Pex 0.21.50115.2: Bugs fixes and Stubs renamed to Moles</title>
      <guid isPermaLink="false">http://blog.dotnetwiki.org/PermaLink,guid,ec9069cd-89d0-4958-9894-e792aeda0abc.aspx</guid>
      <link>http://blog.dotnetwiki.org/2010/01/16/Pex021501152BugsFixesAndStubsRenamedToMoles.aspx</link>
      <pubDate>Sat, 16 Jan 2010 04:14:07 GMT</pubDate>
      <description>&lt;p&gt;
We just &lt;a href="http://research.microsoft.com/en-us/projects/pex/downloads.aspx"&gt;released
Pex 0.21.50115.2&lt;/a&gt;. This release brings bug fixes, a big renaming from “Stubs” to
“Moles” and improved infrastructure to build behaved types (formerly known as beavers).
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Bug Fixes&lt;/strong&gt;
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
The Moles VsHost fails to execute unit tests in different assemblies. 
&lt;li&gt;
&lt;a href="http://research.microsoft.com/pex/"&gt;Pex&lt;/a&gt; deletes the report folder it
is currently writing to. 
&lt;li&gt;
Support for named indexers in Stubs 
&lt;li&gt;
Fixed bugs in how Pex reasons about System.Convert.ToBase64, DateTime 
&lt;li&gt;
Invalid support for protected members in the stubs generation&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
&lt;strong&gt;Breaking changes&lt;/strong&gt;
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The Stubs framework was renamed to Moles framework.&lt;/strong&gt; We have decided
to make the Moles the center of the framework and as a consequence, renamed ‘Stubs’
to ‘Moles’. (This does not mean that we encourage writing untestable code, as Mole
help to make it testable. You should still refactor your code to make it testable
whenever possible, and only use Moles when that’s the only choice). The impact is
that 
&lt;ul&gt;
&lt;li&gt;
Microsoft.Stubs.Framework was renamed to Microsoft.Moles.Framework 
&lt;li&gt;
The moles and stubs get generated in subnamespaces ‘.Moles’ rather ‘.Stubs’. 
&lt;li&gt;
See below for the list of steps to upgrade your applications.&lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt;
&lt;strong&gt;BaseMembers in Moles have been deprecated&lt;/strong&gt;: this helper is not useful
as it can be acheive in a better way through a constructor. We decided to remove it
to reduce code size. The second reason is that BaseMembers would only work for types
inside of the same assembly, which might seem inconsistent. 
&lt;li&gt;
&lt;strong&gt;PexGoal.Reached is replaced by PexAssert.ReachEventually().&lt;/strong&gt; The PexGoal
class has been integrated into PexAssert through the ReachEventually method which
should be used with the [PexAssertReachEventually] attribute. 
&lt;li&gt;
&lt;strong&gt;PexChoose simplified&lt;/strong&gt;: we’ve simplified the PexChoose API; you can
now get auxiliary test inputs with a single method call: PexChoose.Value&amp;lt;T&amp;gt;(“foo”).&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
&lt;strong&gt;Migrating from previous version of Pex&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
Since we’ve renamed Stubs to Moles, any existing .stubx files will not work anymore.
&lt;/p&gt;
&lt;p&gt;
Take a deep breath, and apply the following steps to adapt your projects:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
change the project reference from Microsoft.Stubs.Framework.dll to Microsoft.Moles.Framework.dll 
&lt;li&gt;
rename all .stubx files to .moles, and 
&lt;ul&gt;
&lt;li&gt;
rename the top &lt;strong&gt;&amp;lt;Stubs&lt;/strong&gt; xml element to &lt;strong&gt;&amp;lt;Moles&lt;/strong&gt;. 
&lt;li&gt;
Change the XSD namespace to &lt;a href="http://schemas.microsoft.com/moles/2010/"&gt;http://schemas.microsoft.com/moles/2010/&lt;/a&gt; 
&lt;li&gt;
Right click on the .moles file in the Solution Explorer and change the Custom Tool
Name to ‘MolesGenerator’. 
&lt;li&gt;
Delete all the nested files under the .moles files&lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt;
Remove references to any compiled .Stubs.dll files in your project 
&lt;li&gt;
In general, remove all .Stubs.dll, .Stubs.xml files from your projects. 
&lt;li&gt;
Rename .Stubs namespace suffixes to .Moles. 
&lt;li&gt;
replace all [HostType(“Pex”)] attribute with [HostType(“Moles”)] 
&lt;li&gt;
in PexAssemblyInfo.cs, 
&lt;ul&gt;
&lt;li&gt;
rename using Microsoft.Pex.Framework.Stubs to Microsoft.Pex.Framework.Moles 
&lt;li&gt;
rename [assembly: PexChooseAsStubFallbackBehavior] to [assembly: PexChooseAsBehavedCurrentBehavior] 
&lt;li&gt;
rename [assembly: PexChooseAsStubFallbackBehavior] to [assembly: PexChooseAsMoleCurrentBehavior]&lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt;
In general, the ‘Fallback’ prefix has been dropped in the following methods: 
&lt;ul&gt;
&lt;li&gt;
rename FallbackAsNotImplemented() to BehaveAsNotImplemented() 
&lt;li&gt;
rename class MoleFallbackBehavior to MoleBehaviors 
&lt;li&gt;
rename class StubFallbackBehavior to BehavedBehavors&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=ec9069cd-89d0-4958-9894-e792aeda0abc" /&gt;</description>
      <comments>http://blog.dotnetwiki.org/CommentView,guid,ec9069cd-89d0-4958-9894-e792aeda0abc.aspx</comments>
      <category>Moles</category>
      <category>Pex</category>
      <category>Stubs</category>
      <category>Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.dotnetwiki.org/Trackback.aspx?guid=9dce05c9-217b-45a7-a8d1-2dce2792116d</trackback:ping>
      <pingback:server>http://blog.dotnetwiki.org/pingback.aspx</pingback:server>
      <pingback:target>http://blog.dotnetwiki.org/PermaLink,guid,9dce05c9-217b-45a7-a8d1-2dce2792116d.aspx</pingback:target>
      <dc:creator>Jonathan de Halleux</dc:creator>
      <wfw:comment>http://blog.dotnetwiki.org/CommentView,guid,9dce05c9-217b-45a7-a8d1-2dce2792116d.aspx</wfw:comment>
      <wfw:commentRss>http://blog.dotnetwiki.org/SyndicationService.asmx/GetEntryCommentsRss?guid=9dce05c9-217b-45a7-a8d1-2dce2792116d</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
It is holiday time so I could spend some time to play with <a href="http://ccimetadata.codeplex.com/">CCI</a>.
The result is a bunch of interesting assembly mutators. Let’s start with the first
one: automatic rich assertion messages.
</p>
        <p>
          <strong>The problem with Assertions</strong>
        </p>
        <p>
When an assertion fails, the error message is usually insufficient to understand failure.
The problem is that we are usually lacking the values in the expression to immediately
diagnose the problem. Consider this example,
</p>
        <blockquote>
          <p>
Assert.True(x == 123);
</p>
        </blockquote>
        <p>
When this assertion fails, one would like to know what was the value of ‘x’. Of course,
that value of x is not serialized in the test output since the assertion method only
sees the ‘false’ value. What we would really like to get is something like ‘x (64)
!= 123’. A number of techniques have been developed to work around this issue.
</p>
        <p>
          <strong>Solution #1: Specialized Assertions</strong>
        </p>
        <p>
A common approach is to provide specialized assertion methods with enhanced logging.
For example, a special method to test equality:
</p>
        <blockquote>
          <p>
Assert.Equal(x , 123);
</p>
        </blockquote>
        <p>
When this assertion fails, the Equal method has the value of both sides of the equality
and can render it in the message: ‘expected 123, actual 64’. Unfortunately, expressions
are more readable when you write them, and specialized assertions cannot cover all
scenarios. This takes us to the next solution.
</p>
        <p>
          <strong>Solution #2: Expression Trees at Run time</strong>
        </p>
        <p>
          <a href="http://themechanicalbride.blogspot.com/">Jafar Husain</a> wrote <a href="http://themechanicalbride.blogspot.com/2009/06/better-unit-tests-with-testassert-for.html">a
very interresting post</a> on how to use Expression Trees to generate rich assertion
messages (this is <a href="http://www.gallio.org/api/html/M_MbUnit_Framework_AssertEx_That.htm">also
supported in MbUnit</a>).
</p>
        <blockquote>
          <p>
Assert.True(<a href="http://msdn.microsoft.com/en-us/library/bb397687.aspx">() =&gt;</a> x
== 123);
</p>
        </blockquote>
        <p>
Instead of taking a bool, the assert method takes an expression tree (Expression&lt;Func&lt;bool&gt;&gt;).
Thus, when the expression evaluates to false, the expression tree can be traversed
to extract interesting values (such as x) and generate a rich log of the failure. 
</p>
        <p>
Unfortunately, there is a major drawback to this technique: what used to be 3 MSIL
instructions (ldloc.1, ldc.i4 123, ceq) to evaluate the condition becomes hundreds
of methods calls, millions of instructions executed through the System.Linq namespaces.
This performance is a overhead on the <a href="http://research.microsoft.com/pex/">Pex</a> whitebox
analysis.
</p>
        <p>
Jafar considers unit test frameworks as an extension of the compiler and uses expression
trees to access what the compiler knows: expression and statements. Following his
idea takes us to the CCI based solution.
</p>
        <p>
          <strong>Solution #3: Assembly Rewritter at Compile time</strong>
        </p>
        <p>
In the previous approach, expression trees were used to build a little compiler <strong>at
runtime</strong>. A better solution would be to rewrite the expressions <strong>at
compile time. </strong>In other words, we want to build an assembly mutator that takes
the original method call and appends code that generates the logging:
</p>
        <blockquote>
          <p>
Assert.True(x == 123<strong>, String.Format(“x == 123 where x = ‘{0}’”, x)</strong>);
</p>
        </blockquote>
        <p>
The assembly mutator extracts the expression sources from the pdb (‘x == 123’), collect
the local/variable/field references by traversing the expression tree, generate a
String.Format friendly message and replace the assertion method call to the assertion
method that takes the additional string.
</p>
        <p>
          <strong>Hello </strong>
          <a href="http://ccimetadata.codeplex.com/">
            <strong>Common Compiler
Infrastructure</strong>
          </a>
          <strong> (CCI) and CciSharp</strong>
        </p>
        <p>
Of course, to rewrite an assembly at compile time, we need a framework that can read
and write MSIL, decompile expressions etc… This is where CCI (from <a href="http://ccimetadata.codeplex.com">http://ccimetadata.codeplex.com</a> )
comes. It allows to manipulate .NET assemblies using an object model and save them
back to disk. The <a href="http://ccisamples.codeplex.com/wikipage?title=AssertMessage&amp;referringTitle=CciSharp">assertion
message mutator</a> is just one of other mutations that have been or will be implemented
as part of <a href="http://ccisamples.codeplex.com/wikipage?title=CciSharp"><strong>CciSharp</strong></a><strong>,
a post compiler for .NET that is built on top of CCI</strong>.
</p>
        <p>
(There’s already a bunch of useful operators in CciSharp that i’ve been coding over
the holidays: assigning the value of auto-properties, making auto-properties readonly
or lazy (or weakly lazy), or even implementing the DependencyProperty stuff automatically.
We’ll talk about this later.)
</p>
        <p>
          <strong>Where can I find it?</strong>
        </p>
        <p>
          <strong>CciSharp</strong> binaries and sources are avaiable on <a title="http://ccisamples.codeplex.com/wikipage?title=CciSharp" href="http://ccisamples.codeplex.com/wikipage?title=CciSharp">http://ccisamples.codeplex.com/wikipage?title=CciSharp</a>.
Grab it!
</p>
        <img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=9dce05c9-217b-45a7-a8d1-2dce2792116d" />
      </body>
      <title>Rich Assertion Messages using CCI (and without expression trees)</title>
      <guid isPermaLink="false">http://blog.dotnetwiki.org/PermaLink,guid,9dce05c9-217b-45a7-a8d1-2dce2792116d.aspx</guid>
      <link>http://blog.dotnetwiki.org/2009/12/31/RichAssertionMessagesUsingCCIAndWithoutExpressionTrees.aspx</link>
      <pubDate>Thu, 31 Dec 2009 04:45:27 GMT</pubDate>
      <description>&lt;p&gt;
It is holiday time so I could spend some time to play with &lt;a href="http://ccimetadata.codeplex.com/"&gt;CCI&lt;/a&gt;.
The result is a bunch of interesting assembly mutators. Let’s start with the first
one: automatic rich assertion messages.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;The problem with Assertions&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
When an assertion fails, the error message is usually insufficient to understand failure.
The problem is that we are usually lacking the values in the expression to immediately
diagnose the problem. Consider this example,
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
Assert.True(x == 123);
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
When this assertion fails, one would like to know what was the value of ‘x’. Of course,
that value of x is not serialized in the test output since the assertion method only
sees the ‘false’ value. What we would really like to get is something like ‘x (64)
!= 123’. A number of techniques have been developed to work around this issue.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Solution #1: Specialized Assertions&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
A common approach is to provide specialized assertion methods with enhanced logging.
For example, a special method to test equality:
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
Assert.Equal(x , 123);
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
When this assertion fails, the Equal method has the value of both sides of the equality
and can render it in the message: ‘expected 123, actual 64’. Unfortunately, expressions
are more readable when you write them, and specialized assertions cannot cover all
scenarios. This takes us to the next solution.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Solution #2: Expression Trees at Run time&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://themechanicalbride.blogspot.com/"&gt;Jafar Husain&lt;/a&gt; wrote &lt;a href="http://themechanicalbride.blogspot.com/2009/06/better-unit-tests-with-testassert-for.html"&gt;a
very interresting post&lt;/a&gt; on how to use Expression Trees to generate rich assertion
messages (this is &lt;a href="http://www.gallio.org/api/html/M_MbUnit_Framework_AssertEx_That.htm"&gt;also
supported in MbUnit&lt;/a&gt;).
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
Assert.True(&lt;a href="http://msdn.microsoft.com/en-us/library/bb397687.aspx"&gt;() =&amp;gt;&lt;/a&gt; x
== 123);
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
Instead of taking a bool, the assert method takes an expression tree (Expression&amp;lt;Func&amp;lt;bool&amp;gt;&amp;gt;).
Thus, when the expression evaluates to false, the expression tree can be traversed
to extract interesting values (such as x) and generate a rich log of the failure. 
&lt;/p&gt;
&lt;p&gt;
Unfortunately, there is a major drawback to this technique: what used to be 3 MSIL
instructions (ldloc.1, ldc.i4 123, ceq) to evaluate the condition becomes hundreds
of methods calls, millions of instructions executed through the System.Linq namespaces.
This performance is a overhead on the &lt;a href="http://research.microsoft.com/pex/"&gt;Pex&lt;/a&gt; whitebox
analysis.
&lt;/p&gt;
&lt;p&gt;
Jafar considers unit test frameworks as an extension of the compiler and uses expression
trees to access what the compiler knows: expression and statements. Following his
idea takes us to the CCI based solution.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Solution #3: Assembly Rewritter at Compile time&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
In the previous approach, expression trees were used to build a little compiler &lt;strong&gt;at
runtime&lt;/strong&gt;. A better solution would be to rewrite the expressions &lt;strong&gt;at
compile time. &lt;/strong&gt;In other words, we want to build an assembly mutator that takes
the original method call and appends code that generates the logging:
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
Assert.True(x == 123&lt;strong&gt;, String.Format(“x == 123 where x = ‘{0}’”, x)&lt;/strong&gt;);
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
The assembly mutator extracts the expression sources from the pdb (‘x == 123’), collect
the local/variable/field references by traversing the expression tree, generate a
String.Format friendly message and replace the assertion method call to the assertion
method that takes the additional string.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Hello &lt;/strong&gt;&lt;a href="http://ccimetadata.codeplex.com/"&gt;&lt;strong&gt;Common Compiler
Infrastructure&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt; (CCI) and CciSharp&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
Of course, to rewrite an assembly at compile time, we need a framework that can read
and write MSIL, decompile expressions etc… This is where CCI (from &lt;a href="http://ccimetadata.codeplex.com"&gt;http://ccimetadata.codeplex.com&lt;/a&gt; )
comes. It allows to manipulate .NET assemblies using an object model and save them
back to disk. The &lt;a href="http://ccisamples.codeplex.com/wikipage?title=AssertMessage&amp;amp;referringTitle=CciSharp"&gt;assertion
message mutator&lt;/a&gt; is just one of other mutations that have been or will be implemented
as part of &lt;a href="http://ccisamples.codeplex.com/wikipage?title=CciSharp"&gt;&lt;strong&gt;CciSharp&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt;,
a post compiler for .NET that is built on top of CCI&lt;/strong&gt;.
&lt;/p&gt;
&lt;p&gt;
(There’s already a bunch of useful operators in CciSharp that i’ve been coding over
the holidays: assigning the value of auto-properties, making auto-properties readonly
or lazy (or weakly lazy), or even implementing the DependencyProperty stuff automatically.
We’ll talk about this later.)
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Where can I find it?&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;CciSharp&lt;/strong&gt; binaries and sources are avaiable on &lt;a title="http://ccisamples.codeplex.com/wikipage?title=CciSharp" href="http://ccisamples.codeplex.com/wikipage?title=CciSharp"&gt;http://ccisamples.codeplex.com/wikipage?title=CciSharp&lt;/a&gt;.
Grab it!
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=9dce05c9-217b-45a7-a8d1-2dce2792116d" /&gt;</description>
      <comments>http://blog.dotnetwiki.org/CommentView,guid,9dce05c9-217b-45a7-a8d1-2dce2792116d.aspx</comments>
      <category>CCI</category>
      <category>CciSharp</category>
      <category>Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.dotnetwiki.org/Trackback.aspx?guid=f9e9f9df-45a8-4b13-bdb9-742e0aa676ac</trackback:ping>
      <pingback:server>http://blog.dotnetwiki.org/pingback.aspx</pingback:server>
      <pingback:target>http://blog.dotnetwiki.org/PermaLink,guid,f9e9f9df-45a8-4b13-bdb9-742e0aa676ac.aspx</pingback:target>
      <dc:creator>Jonathan de Halleux</dc:creator>
      <wfw:comment>http://blog.dotnetwiki.org/CommentView,guid,f9e9f9df-45a8-4b13-bdb9-742e0aa676ac.aspx</wfw:comment>
      <wfw:commentRss>http://blog.dotnetwiki.org/SyndicationService.asmx/GetEntryCommentsRss?guid=f9e9f9df-45a8-4b13-bdb9-742e0aa676ac</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I’ll be presenting the latest development on using Moles and Pex to unit test SharePoint
2010 (or 2007) Services at the <a href="http://www.devconnections.com/shows/NED2010SP/" target="_blank">SharePoint
Connnections in Amsterdam, 18th-19th of January</a>.
</p>
        <blockquote>
          <p>
            <b>MSC26: Pex - Unit Testing of SharePoint Services that Rocks!</b>
            <br />
SharePoint Services are challenging for unit testing because it is not possible execute
the SharePoint Service without being connected to a live SharePoint site. For that
reason, most of the unit tests written for SharePoint are actually integration tests
as they need a live system to run. In this session, we show how to use Pex, an automated
test generation tool for .NET, to test SharePoint Services in isolation. From a parameterized
unit test, Pex generates a suite of closed unit tests with high code coverage. Pex
also contains a stubbing framework, Moles, that allows to detour any .NET method to
user-defined delegates, e.g. replace any call to the SharePoint Object Model by a
user-defined delegate. 
</p>
        </blockquote>
        <img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=f9e9f9df-45a8-4b13-bdb9-742e0aa676ac" />
      </body>
      <title>Pex at the SharePoint Connections in Amsterdam, 18th-19th January</title>
      <guid isPermaLink="false">http://blog.dotnetwiki.org/PermaLink,guid,f9e9f9df-45a8-4b13-bdb9-742e0aa676ac.aspx</guid>
      <link>http://blog.dotnetwiki.org/2009/12/04/PexAtTheSharePointConnectionsInAmsterdam18th19thJanuary.aspx</link>
      <pubDate>Fri, 04 Dec 2009 06:45:36 GMT</pubDate>
      <description>&lt;p&gt;
I’ll be presenting the latest development on using Moles and Pex to unit test SharePoint
2010 (or 2007) Services at the &lt;a href="http://www.devconnections.com/shows/NED2010SP/" target="_blank"&gt;SharePoint
Connnections in Amsterdam, 18th-19th of January&lt;/a&gt;.
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
&lt;b&gt;MSC26: Pex - Unit Testing of SharePoint Services that Rocks!&lt;/b&gt;
&lt;br&gt;
SharePoint Services are challenging for unit testing because it is not possible execute
the SharePoint Service without being connected to a live SharePoint site. For that
reason, most of the unit tests written for SharePoint are actually integration tests
as they need a live system to run. In this session, we show how to use Pex, an automated
test generation tool for .NET, to test SharePoint Services in isolation. From a parameterized
unit test, Pex generates a suite of closed unit tests with high code coverage. Pex
also contains a stubbing framework, Moles, that allows to detour any .NET method to
user-defined delegates, e.g. replace any call to the SharePoint Object Model by a
user-defined delegate. 
&lt;/p&gt;
&lt;/blockquote&gt;&lt;img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=f9e9f9df-45a8-4b13-bdb9-742e0aa676ac" /&gt;</description>
      <comments>http://blog.dotnetwiki.org/CommentView,guid,f9e9f9df-45a8-4b13-bdb9-742e0aa676ac.aspx</comments>
      <category>Moles</category>
      <category>Pex</category>
      <category>Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.dotnetwiki.org/Trackback.aspx?guid=68657a76-a7c6-421a-8321-41d201f54778</trackback:ping>
      <pingback:server>http://blog.dotnetwiki.org/pingback.aspx</pingback:server>
      <pingback:target>http://blog.dotnetwiki.org/PermaLink,guid,68657a76-a7c6-421a-8321-41d201f54778.aspx</pingback:target>
      <dc:creator>Jonathan de Halleux</dc:creator>
      <wfw:comment>http://blog.dotnetwiki.org/CommentView,guid,68657a76-a7c6-421a-8321-41d201f54778.aspx</wfw:comment>
      <wfw:commentRss>http://blog.dotnetwiki.org/SyndicationService.asmx/GetEntryCommentsRss?guid=68657a76-a7c6-421a-8321-41d201f54778</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
We have just release a new version of <a href="http://research.microsoft.com/pex" target="_blank">Pex</a> (v0.17.xxx)
which brings <strong>new features for Moles and more bug fixes</strong>.
</p>
        <p>
          <strong>Per-Instance Moles</strong>
        </p>
        <p>
Instance methods may be <em>moled</em> differently per object instance. This is particularly
useful when multiple instances of same type need to behave differently in the same
test. For example, let’s consider a simple class Bar that has a ‘Run’ method to check
that the values are not the same. The important point here is that we would not be
able to deal with such cases if we could not mole the behavior on a per-instance basis.
</p>
        <p>
          <a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.17MoreMolesandBugFixes_D241/image_10.png">
            <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.17MoreMolesandBugFixes_D241/image_thumb_4.png" width="393" height="201" />
          </a>
        </p>
        <p>
In the test case of Run, we create to instance of MBar (the mole type of Bar) and
assign two different delegates to the ‘Value’ getter. Moles are active as soon as
they are attached (unlike the previous version) so we can just call the ‘Run’ method
once the moles are set up. In the code below, we are using the <a href="http://msdn.microsoft.com/en-us/library/bb384062.aspx" target="_blank">C#
Object Initializers</a> feature that allows us to set properties of a newly created
object within curly braces after the ‘new’ call.
</p>
        <p>
          <a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.17MoreMolesandBugFixes_D241/image_12.png">
            <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.17MoreMolesandBugFixes_D241/image_thumb_5.png" width="448" height="249" />
          </a>
        </p>
        <p>
As you may have noticed, we just wrote a parameterized unit test that takes arbitrary
‘left’ and ‘right’ values. We then simply execute Pex to find the relevant values
to cover the ‘Run’ method:
</p>
        <p>
          <a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.17MoreMolesandBugFixes_D241/image_14.png">
            <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.17MoreMolesandBugFixes_D241/image_thumb_6.png" width="425" height="74" />
          </a>
        </p>
        <p>
          <strong>Moles for Constructors</strong>
        </p>
        <p>
In same cases, objects created inside of other methods need to be moled to test the
actual program logic. With moles you can replace any constructor with your own delegate,
which you can use to attach a mole to the newly created instance. Let’s see this with
another simple example where a  ‘Run’ method creates and uses an instance of
a ‘Foo’ class.
</p>
        <p>
          <a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.17MoreMolesandBugFixes_D241/image_4.png">
            <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.17MoreMolesandBugFixes_D241/image_thumb_1.png" width="363" height="263" />
          </a>
        </p>
        <p>
Let’s say we would like to mole the call to the ‘Value’ property to return any other
value. To do so, we would need to attach a mole to a future instance of Foo, and then
mole the Value property getter. This is exactly what is done in the method below.
</p>
        <p>
          <a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.17MoreMolesandBugFixes_D241/image_6.png">
            <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.17MoreMolesandBugFixes_D241/image_thumb_2.png" width="293" height="231" />
          </a>
        </p>
        <p>
We then run Pex to find that 2 different values are needed to trigger all code paths.
</p>
        <p>
          <a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.17MoreMolesandBugFixes_D241/image_8.png">
            <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.17MoreMolesandBugFixes_D241/image_thumb_3.png" width="427" height="85" />
          </a> 
</p>
        <p>
          <strong>Mole interface binding</strong>
        </p>
        <p>
All the member implementations of an interface may be moled at once using the new
‘Bind’ method. This smells like duck typing with type safety as the Bind methods are
strongly typed. For example, we want to mole a collection type (Foes) to return a
custom list of Foo elements (which need to be moled too). The goal is to test the
sum method…
</p>
        <p>
          <a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.17MoreMolesandBugFixes_D241/image_16.png">
            <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.17MoreMolesandBugFixes_D241/image_thumb.png" width="309" height="232" />
          </a>
        </p>
        <p>
In the parameterized unit test case, we create an instance of Vector, which implements
IEnumerable&lt;int&gt;. Then, <strong>we can simply bind the ‘values’ array to the
mole </strong>to make Vector iterate over the array. The call to Bind will mole all
methods of MVector that are implemented by the ‘values’ parameters, effectively redirecting
all the enumeration requests to the ‘values’ array.
</p>
        <p>
          <a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.17MoreMolesandBugFixes_D241/image_22.png">
            <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.17MoreMolesandBugFixes_D241/image_thumb_9.png" width="586" height="216" />
          </a>
        </p>
        <p>
When Pex runs through the sample, it correctly understand the data flow of the parameters
through the moles and finds inputs that break the ‘sum &gt;= 0’ assertion:
</p>
        <p>
          <a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.17MoreMolesandBugFixes_D241/image_20.png">
            <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.17MoreMolesandBugFixes_D241/image_thumb_8.png" width="603" height="150" />
          </a>
        </p>
        <p>
          <strong>Compilation of Stubs assemblies</strong>
        </p>
        <p>
When one needs stubs or moles for a system assembly, it does not really make sense
to re-generate the stubs each time the solution is loaded. To that end, Stubs ships
with a command line tool that lets you easily compile a stubs assembly into a .dll:
</p>
        <blockquote>
          <p>
            <strong>stubs.exe mscorlib</strong>
          </p>
        </blockquote>
        <p>
Once the stubs assembly is compiled, i.e. mscorlib.Stubs.dll is created, you simply
have to add a reference to it in your test project to start using it. In future versions
of Pex, we will provide a better experience to support this scenario.
</p>
        <p>
          <strong>Bug Fixes</strong>
        </p>
        <ul>
          <li>
A number of fixes from the <a href="http://social.msdn.microsoft.com/Forums/en/pex/threads" target="_blank">issues
reported through the forums</a> and bugs reported to <a href="mailto:pexbug@microsoft.com">pexbug@microsoft.com</a> .</li>
        </ul>
        <p>
          <strong>Breaking Changes</strong>
        </p>
        <ul>
          <li>
The code generation of Moles was significantly changed. This might mean that you will
have to recompile your solution, and adapt all existing uses of Moles.</li>
        </ul>
        <img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=68657a76-a7c6-421a-8321-41d201f54778" />
      </body>
      <title>Pex v0.17.41006.7 : More Moles and Bug Fixes</title>
      <guid isPermaLink="false">http://blog.dotnetwiki.org/PermaLink,guid,68657a76-a7c6-421a-8321-41d201f54778.aspx</guid>
      <link>http://blog.dotnetwiki.org/2009/10/07/PexV017410067MoreMolesAndBugFixes.aspx</link>
      <pubDate>Wed, 07 Oct 2009 22:02:46 GMT</pubDate>
      <description>&lt;p&gt;
We have just release a new version of &lt;a href="http://research.microsoft.com/pex" target="_blank"&gt;Pex&lt;/a&gt; (v0.17.xxx)
which brings &lt;strong&gt;new features for Moles and more bug fixes&lt;/strong&gt;.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Per-Instance Moles&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
Instance methods may be &lt;em&gt;moled&lt;/em&gt; differently per object instance. This is particularly
useful when multiple instances of same type need to behave differently in the same
test. For example, let’s consider a simple class Bar that has a ‘Run’ method to check
that the values are not the same. The important point here is that we would not be
able to deal with such cases if we could not mole the behavior on a per-instance basis.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.17MoreMolesandBugFixes_D241/image_10.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.17MoreMolesandBugFixes_D241/image_thumb_4.png" width="393" height="201"&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
In the test case of Run, we create to instance of MBar (the mole type of Bar) and
assign two different delegates to the ‘Value’ getter. Moles are active as soon as
they are attached (unlike the previous version) so we can just call the ‘Run’ method
once the moles are set up. In the code below, we are using the &lt;a href="http://msdn.microsoft.com/en-us/library/bb384062.aspx" target="_blank"&gt;C#
Object Initializers&lt;/a&gt; feature that allows us to set properties of a newly created
object within curly braces after the ‘new’ call.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.17MoreMolesandBugFixes_D241/image_12.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.17MoreMolesandBugFixes_D241/image_thumb_5.png" width="448" height="249"&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
As you may have noticed, we just wrote a parameterized unit test that takes arbitrary
‘left’ and ‘right’ values. We then simply execute Pex to find the relevant values
to cover the ‘Run’ method:
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.17MoreMolesandBugFixes_D241/image_14.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.17MoreMolesandBugFixes_D241/image_thumb_6.png" width="425" height="74"&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Moles for Constructors&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
In same cases, objects created inside of other methods need to be moled to test the
actual program logic. With moles you can replace any constructor with your own delegate,
which you can use to attach a mole to the newly created instance. Let’s see this with
another simple example where a&amp;nbsp; ‘Run’ method creates and uses an instance of
a ‘Foo’ class.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.17MoreMolesandBugFixes_D241/image_4.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.17MoreMolesandBugFixes_D241/image_thumb_1.png" width="363" height="263"&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
Let’s say we would like to mole the call to the ‘Value’ property to return any other
value. To do so, we would need to attach a mole to a future instance of Foo, and then
mole the Value property getter. This is exactly what is done in the method below.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.17MoreMolesandBugFixes_D241/image_6.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.17MoreMolesandBugFixes_D241/image_thumb_2.png" width="293" height="231"&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
We then run Pex to find that 2 different values are needed to trigger all code paths.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.17MoreMolesandBugFixes_D241/image_8.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.17MoreMolesandBugFixes_D241/image_thumb_3.png" width="427" height="85"&gt;&lt;/a&gt;&amp;nbsp;
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Mole interface binding&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
All the member implementations of an interface may be moled at once using the new
‘Bind’ method. This smells like duck typing with type safety as the Bind methods are
strongly typed. For example, we want to mole a collection type (Foes) to return a
custom list of Foo elements (which need to be moled too). The goal is to test the
sum method…
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.17MoreMolesandBugFixes_D241/image_16.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.17MoreMolesandBugFixes_D241/image_thumb.png" width="309" height="232"&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
In the parameterized unit test case, we create an instance of Vector, which implements
IEnumerable&amp;lt;int&amp;gt;. Then, &lt;strong&gt;we can simply bind the ‘values’ array to the
mole &lt;/strong&gt;to make Vector iterate over the array. The call to Bind will mole all
methods of MVector that are implemented by the ‘values’ parameters, effectively redirecting
all the enumeration requests to the ‘values’ array.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.17MoreMolesandBugFixes_D241/image_22.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.17MoreMolesandBugFixes_D241/image_thumb_9.png" width="586" height="216"&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
When Pex runs through the sample, it correctly understand the data flow of the parameters
through the moles and finds inputs that break the ‘sum &amp;gt;= 0’ assertion:
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.17MoreMolesandBugFixes_D241/image_20.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.17MoreMolesandBugFixes_D241/image_thumb_8.png" width="603" height="150"&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Compilation of Stubs assemblies&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
When one needs stubs or moles for a system assembly, it does not really make sense
to re-generate the stubs each time the solution is loaded. To that end, Stubs ships
with a command line tool that lets you easily compile a stubs assembly into a .dll:
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
&lt;strong&gt;stubs.exe mscorlib&lt;/strong&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
Once the stubs assembly is compiled, i.e. mscorlib.Stubs.dll is created, you simply
have to add a reference to it in your test project to start using it. In future versions
of Pex, we will provide a better experience to support this scenario.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Bug Fixes&lt;/strong&gt;
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
A number of fixes from the &lt;a href="http://social.msdn.microsoft.com/Forums/en/pex/threads" target="_blank"&gt;issues
reported through the forums&lt;/a&gt; and bugs reported to &lt;a href="mailto:pexbug@microsoft.com"&gt;pexbug@microsoft.com&lt;/a&gt; .&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
&lt;strong&gt;Breaking Changes&lt;/strong&gt;
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
The code generation of Moles was significantly changed. This might mean that you will
have to recompile your solution, and adapt all existing uses of Moles.&lt;/li&gt;
&lt;/ul&gt;
&lt;img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=68657a76-a7c6-421a-8321-41d201f54778" /&gt;</description>
      <comments>http://blog.dotnetwiki.org/CommentView,guid,68657a76-a7c6-421a-8321-41d201f54778.aspx</comments>
      <category>Pex</category>
      <category>Stubs</category>
      <category>Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.dotnetwiki.org/Trackback.aspx?guid=ce09b012-e538-4c77-83b3-83d9f62cf259</trackback:ping>
      <pingback:server>http://blog.dotnetwiki.org/pingback.aspx</pingback:server>
      <pingback:target>http://blog.dotnetwiki.org/PermaLink,guid,ce09b012-e538-4c77-83b3-83d9f62cf259.aspx</pingback:target>
      <dc:creator>Jonathan de Halleux</dc:creator>
      <wfw:comment>http://blog.dotnetwiki.org/CommentView,guid,ce09b012-e538-4c77-83b3-83d9f62cf259.aspx</wfw:comment>
      <wfw:commentRss>http://blog.dotnetwiki.org/SyndicationService.asmx/GetEntryCommentsRss?guid=ce09b012-e538-4c77-83b3-83d9f62cf259</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
We just <a href="http://research.microsoft.com/en-us/projects/pex/downloads.aspx" target="_blank">released</a> v0.16.40915.5
of Pex. The major highlights of this release are <strong>Moles, a lightweight detour
framework, and better support for test frameworks</strong>.
</p>
        <ul>
          <li>
            <strong>Moles, a lightweight detour framework: </strong>Moles is a new extension of
the <a href="http://research.microsoft.com/stubs" target="_blank">Stubs framework</a>:
it lets you replace any .NET method (including static methods) with your own delegate.(sample
outdated) 
<br />
Want read more about this sample? here is <a href="http://research.microsoft.com/en-us/projects/stubs/moles.aspx" target="_blank">the
full walkthrough</a>.<br />
The moles framework is not yet feature complete; moles does not support constructors
and external methods. Some types of mscorlib cannot be moled as they interact too
deeply with the CLR. 
</li>
          <li>
            <strong>Pex gets its own HostType: </strong>A HostType is a feature for the Visual
Studio Unit Test framework that lets specific unit tests be run under specialized
hosts such as Asp.Net or VisualStudio iself. In order to create reproducible test
cases using Moles, we had to implement a HostType that lets tests be run under the
Pex profiler. This is very exciting because it also opens the door for many uses of
Pex such as fault injection, dynamic checking, etc… in future versions of <a href="http://research.microsoft.com/pex" target="_blank">Pex</a>.
When generating test cases with Pex, all the necessary annotations will be created
automatically. To turn the Pex HostType on hand-written (non-parameterized) unit tests,
simply add <strong>[HostType(“Pex”)]</strong> on your test case.<br /><a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pex0.16_733B/image_20.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pex0.16_733B/image_thumb_9.png" width="242" height="108" /></a><strong><br /></strong>This feature only works with Visual Studio Unit Test 2008. 
</li>
          <li>
            <strong>Select your test framework</strong>: the first time you invoke ‘Pex’ on your
code, Pex pops up a dialog to select your test framework of choice. You can select
which test framework should be used by default and, more importantly, where it can
be found on disk. 
<br /><a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pex0.16_733B/image_4.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pex0.16_733B/image_thumb_1.png" width="604" height="248" /></a><br />
If you do not use any test framework, Pex ships with what we call the <strong>“Direct
test framework”</strong>: in this “framework”, all methods are considered as unit
tests without any annotation or dependencies.<br /><a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pex0.16_733B/image_6.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pex0.16_733B/image_thumb_2.png" width="566" height="198" /></a><br />
These settings are stored in the registry and Pex should not bug you again. If you
want to clear these settings, go to ‘Tools –&gt; Options –&gt; Pex –&gt; General’ and clear
the TestFramework and TestFrameworkDirectory fields:<br /><a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pex0.16_733B/image_16.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pex0.16_733B/image_thumb_7.png" width="604" height="356" /></a><br /></li>
          <li>
            <strong>Thumbs up and down</strong>: We’ve added thumbs up and down buttons in the
Pex result view. <strong>We are always looking for feedback on Pex</strong>, so don’t
hesitate to click them when Pex deserves a thumbs up or a thumbs down.<br /><a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pex0.16_733B/image_18.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pex0.16_733B/image_thumb_8.png" width="604" height="76" /></a></li>
          <li>
            <strong>Performance</strong>: Many other performance improvements under the hood which
should avoid Pex hog too much memory in long running scenarios. 
</li>
          <li>
            <strong>Miscellanous improvements and bug fixes</strong>: 
<ul><li><strong>Support for String.GetHashCode():</strong> we now faithfully encode the hashing
function of strings, which means Pex can deal with dictionaries where the key is a
string. 
</li><li>
Fewer “Object Creation” messages that are not actually relevant. 
</li><li>
In VS 2010, the package used to trigger an exception when the solution loaded. This
issue is now fixed. 
</li><li>
In Visual Studio when an assembly had to be resolved manually (i.e. that little dialog
asking you where an assembly is), Pex remembers that choice and does not bug you anymore. 
</li><li>
And many other bugs that <a href="http://social.msdn.microsoft.com/Forums/en/pex/threads" target="_blank">were
reported through the forums</a>.</li></ul></li>
        </ul>
        <img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=ce09b012-e538-4c77-83b3-83d9f62cf259" />
      </body>
      <title>Pex 0.16.40915.5: Moles, a lightweight Detour Framework, and better support for Test Frameworks</title>
      <guid isPermaLink="false">http://blog.dotnetwiki.org/PermaLink,guid,ce09b012-e538-4c77-83b3-83d9f62cf259.aspx</guid>
      <link>http://blog.dotnetwiki.org/2009/09/16/Pex016409155MolesALightweightDetourFrameworkAndBetterSupportForTestFrameworks.aspx</link>
      <pubDate>Wed, 16 Sep 2009 21:16:33 GMT</pubDate>
      <description>&lt;p&gt;
We just &lt;a href="http://research.microsoft.com/en-us/projects/pex/downloads.aspx" target="_blank"&gt;released&lt;/a&gt; v0.16.40915.5
of Pex. The major highlights of this release are &lt;strong&gt;Moles, a lightweight detour
framework, and better support for test frameworks&lt;/strong&gt;.
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Moles, a lightweight detour framework: &lt;/strong&gt;Moles is a new extension of
the &lt;a href="http://research.microsoft.com/stubs" target="_blank"&gt;Stubs framework&lt;/a&gt;:
it lets you replace any .NET method (including static methods) with your own delegate.(sample
outdated) 
&lt;br&gt;
Want read more about this sample? here is &lt;a href="http://research.microsoft.com/en-us/projects/stubs/moles.aspx" target="_blank"&gt;the
full walkthrough&lt;/a&gt;.&lt;br&gt;
The moles framework is not yet feature complete; moles does not support constructors
and external methods. Some types of mscorlib cannot be moled as they interact too
deeply with the CLR. 
&lt;li&gt;
&lt;strong&gt;Pex gets its own HostType: &lt;/strong&gt;A HostType is a feature for the Visual
Studio Unit Test framework that lets specific unit tests be run under specialized
hosts such as Asp.Net or VisualStudio iself. In order to create reproducible test
cases using Moles, we had to implement a HostType that lets tests be run under the
Pex profiler. This is very exciting because it also opens the door for many uses of
Pex such as fault injection, dynamic checking, etc… in future versions of &lt;a href="http://research.microsoft.com/pex" target="_blank"&gt;Pex&lt;/a&gt;.
When generating test cases with Pex, all the necessary annotations will be created
automatically. To turn the Pex HostType on hand-written (non-parameterized) unit tests,
simply add &lt;strong&gt;[HostType(“Pex”)]&lt;/strong&gt; on your test case.&lt;br&gt;
&lt;a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pex0.16_733B/image_20.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pex0.16_733B/image_thumb_9.png" width="242" height="108"&gt;&lt;/a&gt;&lt;strong&gt; 
&lt;br&gt;
&lt;/strong&gt;This feature only works with Visual Studio Unit Test 2008. 
&lt;li&gt;
&lt;strong&gt;Select your test framework&lt;/strong&gt;: the first time you invoke ‘Pex’ on your
code, Pex pops up a dialog to select your test framework of choice. You can select
which test framework should be used by default and, more importantly, where it can
be found on disk. 
&lt;br&gt;
&lt;a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pex0.16_733B/image_4.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pex0.16_733B/image_thumb_1.png" width="604" height="248"&gt;&lt;/a&gt; 
&lt;br&gt;
If you do not use any test framework, Pex ships with what we call the &lt;strong&gt;“Direct
test framework”&lt;/strong&gt;: in this “framework”, all methods are considered as unit
tests without any annotation or dependencies.&lt;br&gt;
&lt;a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pex0.16_733B/image_6.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pex0.16_733B/image_thumb_2.png" width="566" height="198"&gt;&lt;/a&gt; 
&lt;br&gt;
These settings are stored in the registry and Pex should not bug you again. If you
want to clear these settings, go to ‘Tools –&gt; Options –&gt; Pex –&gt; General’ and clear
the TestFramework and TestFrameworkDirectory fields:&lt;br&gt;
&lt;a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pex0.16_733B/image_16.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pex0.16_733B/image_thumb_7.png" width="604" height="356"&gt;&lt;/a&gt; 
&lt;br&gt;
&lt;li&gt;
&lt;strong&gt;Thumbs up and down&lt;/strong&gt;: We’ve added thumbs up and down buttons in the
Pex result view. &lt;strong&gt;We are always looking for feedback on Pex&lt;/strong&gt;, so don’t
hesitate to click them when Pex deserves a thumbs up or a thumbs down.&lt;br&gt;
&lt;a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pex0.16_733B/image_18.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pex0.16_733B/image_thumb_8.png" width="604" height="76"&gt;&lt;/a&gt; 
&lt;li&gt;
&lt;strong&gt;Performance&lt;/strong&gt;: Many other performance improvements under the hood which
should avoid Pex hog too much memory in long running scenarios. 
&lt;li&gt;
&lt;strong&gt;Miscellanous improvements and bug fixes&lt;/strong&gt;: 
&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Support for String.GetHashCode():&lt;/strong&gt; we now faithfully encode the hashing
function of strings, which means Pex can deal with dictionaries where the key is a
string. 
&lt;li&gt;
Fewer “Object Creation” messages that are not actually relevant. 
&lt;li&gt;
In VS 2010, the package used to trigger an exception when the solution loaded. This
issue is now fixed. 
&lt;li&gt;
In Visual Studio when an assembly had to be resolved manually (i.e. that little dialog
asking you where an assembly is), Pex remembers that choice and does not bug you anymore. 
&lt;li&gt;
And many other bugs that &lt;a href="http://social.msdn.microsoft.com/Forums/en/pex/threads" target="_blank"&gt;were
reported through the forums&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=ce09b012-e538-4c77-83b3-83d9f62cf259" /&gt;</description>
      <comments>http://blog.dotnetwiki.org/CommentView,guid,ce09b012-e538-4c77-83b3-83d9f62cf259.aspx</comments>
      <category>Code Contracts</category>
      <category>Pex</category>
      <category>Stubs</category>
      <category>Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.dotnetwiki.org/Trackback.aspx?guid=04c198f2-ca64-4e84-8fd5-e11fd04c4ad1</trackback:ping>
      <pingback:server>http://blog.dotnetwiki.org/pingback.aspx</pingback:server>
      <pingback:target>http://blog.dotnetwiki.org/PermaLink,guid,04c198f2-ca64-4e84-8fd5-e11fd04c4ad1.aspx</pingback:target>
      <dc:creator>Jonathan de Halleux</dc:creator>
      <wfw:comment>http://blog.dotnetwiki.org/CommentView,guid,04c198f2-ca64-4e84-8fd5-e11fd04c4ad1.aspx</wfw:comment>
      <wfw:commentRss>http://blog.dotnetwiki.org/SyndicationService.asmx/GetEntryCommentsRss?guid=04c198f2-ca64-4e84-8fd5-e11fd04c4ad1</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
          <a href="http://en.wikipedia.org/wiki/Inversion_of_control" target="_blank">Inversion
of Control</a> (IoC) is a very important practice to make code testable. It usually
relies on defining interfaces for each external dependency. Various frameworks and
techniques, i.e. <a href="http://en.wikipedia.org/wiki/Dependency_injection" target="_blank">Dependency
Injection</a> (DI), <a href="http://en.wikipedia.org/wiki/Service_locator_pattern" target="_blank">Service
Locator</a>, exist to bind the interface implementations to the components. In this
blog post, we will *not* focus on this aspects of IoC but rather on it’s main building
block: <em>interfaces</em>. This article describes how <strong>Contracts for interfaces
improves IoC… <em>regardless of which DI framework/technique you are using.</em></strong>The
Contracts for interfaces are a feature of the <a href="http://research.microsoft.com/contracts" target="_blank">Code
Contracts for .Net</a> tool.
</p>
        <h3>Interfaces are not Contracts
</h3>
        <p>
Interfaces are often referred to as <em>Contracts</em> in the context of IoC: they
define the methods and properties that a service should implement and are a key factor
in achieving the loose coupling: one can build the code against the interface and
can plug in various implementation with no risks.
</p>
        <p>
Unfortunately, <a href="http://blogs.msdn.com/kcwalina/archive/2004/10/24/246947.aspx" target="_blank">interfaces
are not contracts</a>: while they specify how the methods signatures should be, <strong>interfaces
do not specify the functional behavior</strong>. To illustrate this, let’s take a
look at a well-known simple interface of the BCL:
</p>
        <p>
          <a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/DependencyInjectionContractsforInterface_5E38/image_4.png">
            <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/DependencyInjectionContractsforInterface_5E38/image_thumb_1.png" width="398" height="64" />
          </a>
        </p>
        <p>
For this interface, I could naively implement as follows:
</p>
        <p>
          <a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/DependencyInjectionContractsforInterface_5E38/image_6.png">
            <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/DependencyInjectionContractsforInterface_5E38/image_thumb_2.png" width="406" height="93" />
          </a>
        </p>
        <p>
While my implementation is really really wrong, it fulfills all requirements of the
IServiceProvider interface: a method GetService that returns object. Of course, if
I would have read the MSDN documentation of GetService, I would have known that object
should implement serviceType. Unfortunately, the interface did not tell me anything
about that and compilers don’t understand MSDN documentation either. 
</p>
        <p>
          <a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/DependencyInjectionContractsforInterface_5E38/image_2.png">
            <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/DependencyInjectionContractsforInterface_5E38/image_thumb.png" width="386" height="267" />
          </a> 
</p>
        <h3>Contracts for interfaces
</h3>
        <p>
Code Contracts provides <a href="http://msdn.microsoft.com/en-us/library/system.diagnostics.contracts(VS.100).aspx" target="_blank">an
API for design-by-contracts</a> (pre-condition, post-condition, etc…). It also supports
defining contracts for interface or abstract classes. <strong>Contracts for interfaces
can be used to specify the functional behavior of interface members.</strong> While
they also serve as documentation, those contracts can also be turned into runtime
checks or leveraged by static checkers. 
</p>
        <p>
          <a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/DependencyInjectionContractsforInterface_5E38/image_8.png">
            <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/DependencyInjectionContractsforInterface_5E38/image_thumb_3.png" width="627" height="318" />
          </a>
        </p>
        <p>
        </p>
        <p>
        </p>
        <ul>
          <li>
since interface members cannot have method bodies, the contracts are stored in a ‘buddy’
type. 
</li>
          <li>
The [<a href="http://msdn.microsoft.com/en-us/library/system.diagnostics.contracts.contractclassattribute(VS.100).aspx" target="_blank">ContractClass</a>]/[<a href="http://msdn.microsoft.com/en-us/library/system.diagnostics.contracts.contractclassforattribute(VS.100).aspx" target="_blank">ContractClassFor</a>]
attributes bind the interface type and the contract type together. 
</li>
          <li>
            <a href="http://msdn.microsoft.com/en-us/library/dd412873(VS.100).aspx" target="_blank">Contract.Result&lt;object&gt;()</a> is
a helper method to refer to the result value, since this is not supported in C#. 
</li>
        </ul>
        <p>
Let’s take a closer look at the body of IServiceProviderContract.GetService. It contains
a pre-condition (Requires) that the serviceType should not be null and a post-condition
(Ensures) that the return value should be null or should be assignable to the serviceType:
</p>
        <p>
          <a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/DependencyInjectionContractsforInterface_5E38/image_10.png">
            <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/DependencyInjectionContractsforInterface_5E38/image_thumb_4.png" width="561" height="96" />
          </a>
        </p>
        <p>
In this simple case, the contracts captured precisely the specification that we found
in MSDN. The critical difference is that they are stored in a format that compilers
and runtimes know pretty well: MSIL byte code. The benefits are huge:
</p>
        <ul>
          <li>
documentation: the contracts are stored in a programming-language agnostic format
that mined and rendered for your favorite programming language, 
</li>
          <li>
static checking: static analysis tools can (and will) use contract to try to find
bugs before you execute the code, or prove that it is correct with respect to the
contracts, 
</li>
          <li>
            <strong>runtime checking: the runtime checker will instrument all implementations
of the interface with the interface contracts <em>automatically</em>. </strong>Once
you’ve specified how an interface should behave, you do not have to repeat yourself
when re-implementing it, the re-writer takes care of that. 
</li>
          <li>
automated white box testing:<strong></strong>tools like <a href="http://research.microsoft.com/pex" target="_blank">Pex</a> can
leverage the runtime contract checks to try to find inputs that violate the contracts. 
</li>
          <li>
            <strong>IoC/DI framework agnostic: </strong>It does not matter which DI framework
you use, as soon as you use interface, you could also provide contracts for it. 
</li>
        </ul>
        <img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=04c198f2-ca64-4e84-8fd5-e11fd04c4ad1" />
      </body>
      <title>Specifying Inversion of Control through Contracts for Interfaces</title>
      <guid isPermaLink="false">http://blog.dotnetwiki.org/PermaLink,guid,04c198f2-ca64-4e84-8fd5-e11fd04c4ad1.aspx</guid>
      <link>http://blog.dotnetwiki.org/2009/06/03/SpecifyingInversionOfControlThroughContractsForInterfaces.aspx</link>
      <pubDate>Wed, 03 Jun 2009 00:14:03 GMT</pubDate>
      <description>&lt;p&gt;
&lt;a href="http://en.wikipedia.org/wiki/Inversion_of_control" target="_blank"&gt;Inversion
of Control&lt;/a&gt; (IoC) is a very important practice to make code testable. It usually
relies on defining interfaces for each external dependency. Various frameworks and
techniques, i.e. &lt;a href="http://en.wikipedia.org/wiki/Dependency_injection" target="_blank"&gt;Dependency
Injection&lt;/a&gt; (DI), &lt;a href="http://en.wikipedia.org/wiki/Service_locator_pattern" target="_blank"&gt;Service
Locator&lt;/a&gt;, exist to bind the interface implementations to the components. In this
blog post, we will *not* focus on this aspects of IoC but rather on it’s main building
block: &lt;em&gt;interfaces&lt;/em&gt;. This article describes how &lt;strong&gt;Contracts for interfaces
improves IoC… &lt;em&gt;regardless of which DI framework/technique you are using.&lt;/em&gt; &lt;/strong&gt;The
Contracts for interfaces are a feature of the &lt;a href="http://research.microsoft.com/contracts" target="_blank"&gt;Code
Contracts for .Net&lt;/a&gt; tool.
&lt;/p&gt;
&lt;h3&gt;Interfaces are not Contracts
&lt;/h3&gt;
&lt;p&gt;
Interfaces are often referred to as &lt;em&gt;Contracts&lt;/em&gt; in the context of IoC: they
define the methods and properties that a service should implement and are a key factor
in achieving the loose coupling: one can build the code against the interface and
can plug in various implementation with no risks.
&lt;/p&gt;
&lt;p&gt;
Unfortunately, &lt;a href="http://blogs.msdn.com/kcwalina/archive/2004/10/24/246947.aspx" target="_blank"&gt;interfaces
are not contracts&lt;/a&gt;: while they specify how the methods signatures should be, &lt;strong&gt;interfaces
do not specify the functional behavior&lt;/strong&gt;. To illustrate this, let’s take a
look at a well-known simple interface of the BCL:
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/DependencyInjectionContractsforInterface_5E38/image_4.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/DependencyInjectionContractsforInterface_5E38/image_thumb_1.png" width="398" height="64" /&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
For this interface, I could naively implement as follows:
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/DependencyInjectionContractsforInterface_5E38/image_6.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/DependencyInjectionContractsforInterface_5E38/image_thumb_2.png" width="406" height="93" /&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
While my implementation is really really wrong, it fulfills all requirements of the
IServiceProvider interface: a method GetService that returns object. Of course, if
I would have read the MSDN documentation of GetService, I would have known that object
should implement serviceType. Unfortunately, the interface did not tell me anything
about that and compilers don’t understand MSDN documentation either. 
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/DependencyInjectionContractsforInterface_5E38/image_2.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/DependencyInjectionContractsforInterface_5E38/image_thumb.png" width="386" height="267" /&gt;&lt;/a&gt;&amp;#160;
&lt;/p&gt;
&lt;h3&gt;Contracts for interfaces
&lt;/h3&gt;
&lt;p&gt;
Code Contracts provides &lt;a href="http://msdn.microsoft.com/en-us/library/system.diagnostics.contracts(VS.100).aspx" target="_blank"&gt;an
API for design-by-contracts&lt;/a&gt; (pre-condition, post-condition, etc…). It also supports
defining contracts for interface or abstract classes. &lt;strong&gt;Contracts for interfaces
can be used to specify the functional behavior of interface members.&lt;/strong&gt; While
they also serve as documentation, those contracts can also be turned into runtime
checks or leveraged by static checkers. 
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/DependencyInjectionContractsforInterface_5E38/image_8.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/DependencyInjectionContractsforInterface_5E38/image_thumb_3.png" width="627" height="318" /&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
since interface members cannot have method bodies, the contracts are stored in a ‘buddy’
type. 
&lt;/li&gt;
&lt;li&gt;
The [&lt;a href="http://msdn.microsoft.com/en-us/library/system.diagnostics.contracts.contractclassattribute(VS.100).aspx" target="_blank"&gt;ContractClass&lt;/a&gt;]/[&lt;a href="http://msdn.microsoft.com/en-us/library/system.diagnostics.contracts.contractclassforattribute(VS.100).aspx" target="_blank"&gt;ContractClassFor&lt;/a&gt;]
attributes bind the interface type and the contract type together. 
&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://msdn.microsoft.com/en-us/library/dd412873(VS.100).aspx" target="_blank"&gt;Contract.Result&amp;lt;object&amp;gt;()&lt;/a&gt; is
a helper method to refer to the result value, since this is not supported in C#. 
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
Let’s take a closer look at the body of IServiceProviderContract.GetService. It contains
a pre-condition (Requires) that the serviceType should not be null and a post-condition
(Ensures) that the return value should be null or should be assignable to the serviceType:
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/DependencyInjectionContractsforInterface_5E38/image_10.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/DependencyInjectionContractsforInterface_5E38/image_thumb_4.png" width="561" height="96" /&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
In this simple case, the contracts captured precisely the specification that we found
in MSDN. The critical difference is that they are stored in a format that compilers
and runtimes know pretty well: MSIL byte code. The benefits are huge:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
documentation: the contracts are stored in a programming-language agnostic format
that mined and rendered for your favorite programming language, 
&lt;/li&gt;
&lt;li&gt;
static checking: static analysis tools can (and will) use contract to try to find
bugs before you execute the code, or prove that it is correct with respect to the
contracts, 
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;runtime checking: the runtime checker will instrument all implementations
of the interface with the interface contracts &lt;em&gt;automatically&lt;/em&gt;. &lt;/strong&gt;Once
you’ve specified how an interface should behave, you do not have to repeat yourself
when re-implementing it, the re-writer takes care of that. 
&lt;/li&gt;
&lt;li&gt;
automated white box testing:&lt;strong&gt; &lt;/strong&gt;tools like &lt;a href="http://research.microsoft.com/pex" target="_blank"&gt;Pex&lt;/a&gt; can
leverage the runtime contract checks to try to find inputs that violate the contracts. 
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;IoC/DI framework agnostic: &lt;/strong&gt;It does not matter which DI framework
you use, as soon as you use interface, you could also provide contracts for it. 
&lt;/li&gt;
&lt;/ul&gt;
&lt;img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=04c198f2-ca64-4e84-8fd5-e11fd04c4ad1" /&gt;</description>
      <comments>http://blog.dotnetwiki.org/CommentView,guid,04c198f2-ca64-4e84-8fd5-e11fd04c4ad1.aspx</comments>
      <category>Code Contracts</category>
      <category>Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.dotnetwiki.org/Trackback.aspx?guid=7c424777-4325-431a-9173-f306c3e29abd</trackback:ping>
      <pingback:server>http://blog.dotnetwiki.org/pingback.aspx</pingback:server>
      <pingback:target>http://blog.dotnetwiki.org/PermaLink,guid,7c424777-4325-431a-9173-f306c3e29abd.aspx</pingback:target>
      <dc:creator>Jonathan de Halleux</dc:creator>
      <wfw:comment>http://blog.dotnetwiki.org/CommentView,guid,7c424777-4325-431a-9173-f306c3e29abd.aspx</wfw:comment>
      <wfw:commentRss>http://blog.dotnetwiki.org/SyndicationService.asmx/GetEntryCommentsRss?guid=7c424777-4325-431a-9173-f306c3e29abd</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
If you’ve been thinking about presenting <a href="http://research.microsoft.com/pex" target="_blank">Pex</a> to
your co-workers or your local .NET community, you can use our slide decks at <a title="http://research.microsoft.com/en-us/projects/pex/documentation.aspx" href="http://research.microsoft.com/en-us/projects/pex/documentation.aspx">http://research.microsoft.com/en-us/projects/pex/documentation.aspx</a> .
The slide decks are there to help you, don’t hesitate to shuffle them, cut them or
pick whatever you need in them (and of course, tell us about it).
</p>
        <p>
Cheers, Peli.
</p>
        <img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=7c424777-4325-431a-9173-f306c3e29abd" />
      </body>
      <title>Presenting Pex to your co-workers? Use our slide decks…</title>
      <guid isPermaLink="false">http://blog.dotnetwiki.org/PermaLink,guid,7c424777-4325-431a-9173-f306c3e29abd.aspx</guid>
      <link>http://blog.dotnetwiki.org/2009/05/26/PresentingPexToYourCoworkersUseOurSlideDecks.aspx</link>
      <pubDate>Tue, 26 May 2009 05:54:47 GMT</pubDate>
      <description>&lt;p&gt;
If you’ve been thinking about presenting &lt;a href="http://research.microsoft.com/pex" target="_blank"&gt;Pex&lt;/a&gt; to
your co-workers or your local .NET community, you can use our slide decks at &lt;a title="http://research.microsoft.com/en-us/projects/pex/documentation.aspx" href="http://research.microsoft.com/en-us/projects/pex/documentation.aspx"&gt;http://research.microsoft.com/en-us/projects/pex/documentation.aspx&lt;/a&gt; .
The slide decks are there to help you, don’t hesitate to shuffle them, cut them or
pick whatever you need in them (and of course, tell us about it).
&lt;/p&gt;
&lt;p&gt;
Cheers, Peli.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=7c424777-4325-431a-9173-f306c3e29abd" /&gt;</description>
      <comments>http://blog.dotnetwiki.org/CommentView,guid,7c424777-4325-431a-9173-f306c3e29abd.aspx</comments>
      <category>Pex</category>
      <category>Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.dotnetwiki.org/Trackback.aspx?guid=bbda23d8-9f56-4ce8-96b1-b222841d2093</trackback:ping>
      <pingback:server>http://blog.dotnetwiki.org/pingback.aspx</pingback:server>
      <pingback:target>http://blog.dotnetwiki.org/PermaLink,guid,bbda23d8-9f56-4ce8-96b1-b222841d2093.aspx</pingback:target>
      <dc:creator>Jonathan de Halleux</dc:creator>
      <wfw:comment>http://blog.dotnetwiki.org/CommentView,guid,bbda23d8-9f56-4ce8-96b1-b222841d2093.aspx</wfw:comment>
      <wfw:commentRss>http://blog.dotnetwiki.org/SyndicationService.asmx/GetEntryCommentsRss?guid=bbda23d8-9f56-4ce8-96b1-b222841d2093</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
This is a cool new feature that builds on <a href="http://research.microsoft.com/stubs" target="_blank">Stubs</a>, <a href="http://research.microsoft.com/pex" target="_blank">Pex</a> 
and Code <a href="http://research.microsoft.com/contracts" target="_blank">Contracts</a> 
(in fact, it is really just a consequence of the interaction of the 3 technologies):
stubs that act as parameterized models while complying to the interface contracts.
</p>
        <ul>
          <li>
Code Contracts provide contracts for interfaces. All implementations of that particular
interface will be instrumented with the runtime checks by the rewriter <b>at compile
time</b>. 
</li>
          <li>
Stubs is really just a code generator that provides a minimal implementation of interfaces
and relies on the C# compiler to build the code. Therefore, if a stub implements an
interface with contracts, it will automatically be instrumented by the runtime rewriter
as well. 
</li>
          <li>
Pex choices (i.e. dynamic parameter generation) may be used as the fallback behavior
of stubs. In other words, whenever a stubbed method without user-defined behavior
is called, Pex gets to pick the return value. Since the stub implementation itself
may have been instrumented with contracts, we’ve added special handling code so that <b>post-condition
violation coming from a stub are considered as an assumption</b>. This effectively
forces Pex to generate values that comply with the interface contracts. 
</li>
        </ul>
        <p>
Let’s see how this works with an example. The IFoo interface contains a single ‘GetName’
method. This method has one precondition, i should be positive and one postcondition,
the result is non null. Since interfaces cannot have a method body, we use a ‘buddy’
class to express those contracts and bind both classes using the [ContractClass] and
[ContractClassFor] attributes:
</p>
        <p>
          <a href="file:///C:\Users\jhalleux\AppData\Local\Temp\WindowsLiveWriter-429641856\supfiles4A3CCC8\image%5b15%5d.png">
            <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image002" border="0" alt="clip_image002" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/StubsasParameterizedModeldwithCodeContra_D4E6/clip_image002_92a5f4fd-8fb6-46ca-9bc9-1f3638fa9af8.gif" width="557" height="291" />
          </a>
        </p>
        <p>
We then turn on runtime rewriting for both the project under test and the test project
(Project properties –&gt; Code Contracts –&gt; Runtime checks). As a result, the stub
of IFoo automatically gets instrumented with the contracts. We can clearly see that
from the generated code of GetName in Reflector below:
</p>
        <p>
          <a href="file:///C:\Users\jhalleux\AppData\Local\Temp\WindowsLiveWriter-429641856\supfiles4A3CCC8\image%5b19%5d.png">
            <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image004" border="0" alt="clip_image004" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/StubsasParameterizedModeldwithCodeContra_D4E6/clip_image004_e18deaf5-0c62-467a-9d37-14ad9817b8b6.gif" width="572" height="278" />
          </a>
        </p>
        <p>
The last step is to write a test for IFoo. In this case, we write a parameterized
unit test that takes an IFoo instance and an integer, then simply call GetName.
</p>
        <p>
          <a href="file:///C:\Users\jhalleux\AppData\Local\Temp\WindowsLiveWriter-429641856\supfiles4A3CCC8\image%5b30%5d.png">
            <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image006" border="0" alt="clip_image006" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/StubsasParameterizedModeldwithCodeContra_D4E6/clip_image006_2e49dd05-8b33-49fa-a84d-e1c2e2a1af1a.gif" width="337" height="150" />
          </a>
        </p>
        <p>
Since the contracts of IFoo.GetName specifies that the result should not be null,
we should not see any assertion violation in this test. Moreover, we should see a
test for the precondition considered as an expected exception:
</p>
        <p>
          <a href="file:///C:\Users\jhalleux\AppData\Local\Temp\WindowsLiveWriter-429641856\supfiles4A3CCC8\image%5b29%5d.png">
            <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image008" border="0" alt="clip_image008" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/StubsasParameterizedModeldwithCodeContra_D4E6/clip_image008_5f0984a3-2fca-4b3f-a226-3e724802109d.gif" width="622" height="82" />
          </a>
        </p>
        <p>
Note that all those behaviors rely on extensions to Pex that the wizard automatically
inserted in the PexAssemblyInfo.cs file:
</p>
        <p>
          <a href="file:///C:\Users\jhalleux\AppData\Local\Temp\WindowsLiveWriter-429641856\supfiles4A3CCC8\image%5b34%5d.png">
            <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image010" border="0" alt="clip_image010" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/StubsasParameterizedModeldwithCodeContra_D4E6/clip_image010_4b1ce042-a76c-490a-a4a3-90613d0f528d.gif" width="518" height="115" />
          </a>
        </p>
        <p>
where
</p>
        <ul>
          <li>
[PexAssumeContractEnsuresFailureAtStubSurface] filters post-condition violations in
stubs as assumptions, 
</li>
          <li>
[PexChooseAsStubFallbackBehavior] installs Pex choices as the fallback behavior of
stubs, 
</li>
          <li>
[PexStubsPackage] load the stubs package (this is a standard procedure for Pex packages), 
</li>
          <li>
[PexUseStubsFromTestAssembly] specifies to Pex that it should considers stub types
when trying to test interfaces or abstract classes. 
</li>
        </ul>
        <img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=bbda23d8-9f56-4ce8-96b1-b222841d2093" />
      </body>
      <title>Stubs as Parameterized Models with Contracts</title>
      <guid isPermaLink="false">http://blog.dotnetwiki.org/PermaLink,guid,bbda23d8-9f56-4ce8-96b1-b222841d2093.aspx</guid>
      <link>http://blog.dotnetwiki.org/2009/05/02/StubsAsParameterizedModelsWithContracts.aspx</link>
      <pubDate>Sat, 02 May 2009 17:19:16 GMT</pubDate>
      <description>&lt;p&gt;
This is a cool new feature that builds on &lt;a href="http://research.microsoft.com/stubs" target="_blank"&gt;Stubs&lt;/a&gt;, &lt;a href="http://research.microsoft.com/pex" target="_blank"&gt;Pex&lt;/a&gt;&amp;#160;
and Code &lt;a href="http://research.microsoft.com/contracts" target="_blank"&gt;Contracts&lt;/a&gt;&amp;#160;
(in fact, it is really just a consequence of the interaction of the 3 technologies):
stubs that act as parameterized models while complying to the interface contracts.
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
Code Contracts provide contracts for interfaces. All implementations of that particular
interface will be instrumented with the runtime checks by the rewriter &lt;b&gt;at compile
time&lt;/b&gt;. 
&lt;/li&gt;
&lt;li&gt;
Stubs is really just a code generator that provides a minimal implementation of interfaces
and relies on the C# compiler to build the code. Therefore, if a stub implements an
interface with contracts, it will automatically be instrumented by the runtime rewriter
as well. 
&lt;/li&gt;
&lt;li&gt;
Pex choices (i.e. dynamic parameter generation) may be used as the fallback behavior
of stubs. In other words, whenever a stubbed method without user-defined behavior
is called, Pex gets to pick the return value. Since the stub implementation itself
may have been instrumented with contracts, we’ve added special handling code so that &lt;b&gt;post-condition
violation coming from a stub are considered as an assumption&lt;/b&gt;. This effectively
forces Pex to generate values that comply with the interface contracts. 
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
Let’s see how this works with an example. The IFoo interface contains a single ‘GetName’
method. This method has one precondition, i should be positive and one postcondition,
the result is non null. Since interfaces cannot have a method body, we use a ‘buddy’
class to express those contracts and bind both classes using the [ContractClass] and
[ContractClassFor] attributes:
&lt;/p&gt;
&lt;p&gt;
&lt;a href="file:///C:\Users\jhalleux\AppData\Local\Temp\WindowsLiveWriter-429641856\supfiles4A3CCC8\image%5b15%5d.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image002" border="0" alt="clip_image002" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/StubsasParameterizedModeldwithCodeContra_D4E6/clip_image002_92a5f4fd-8fb6-46ca-9bc9-1f3638fa9af8.gif" width="557" height="291" /&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
We then turn on runtime rewriting for both the project under test and the test project
(Project properties –&amp;gt; Code Contracts –&amp;gt; Runtime checks). As a result, the stub
of IFoo automatically gets instrumented with the contracts. We can clearly see that
from the generated code of GetName in Reflector below:
&lt;/p&gt;
&lt;p&gt;
&lt;a href="file:///C:\Users\jhalleux\AppData\Local\Temp\WindowsLiveWriter-429641856\supfiles4A3CCC8\image%5b19%5d.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image004" border="0" alt="clip_image004" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/StubsasParameterizedModeldwithCodeContra_D4E6/clip_image004_e18deaf5-0c62-467a-9d37-14ad9817b8b6.gif" width="572" height="278" /&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
The last step is to write a test for IFoo. In this case, we write a parameterized
unit test that takes an IFoo instance and an integer, then simply call GetName.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="file:///C:\Users\jhalleux\AppData\Local\Temp\WindowsLiveWriter-429641856\supfiles4A3CCC8\image%5b30%5d.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image006" border="0" alt="clip_image006" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/StubsasParameterizedModeldwithCodeContra_D4E6/clip_image006_2e49dd05-8b33-49fa-a84d-e1c2e2a1af1a.gif" width="337" height="150" /&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
Since the contracts of IFoo.GetName specifies that the result should not be null,
we should not see any assertion violation in this test. Moreover, we should see a
test for the precondition considered as an expected exception:
&lt;/p&gt;
&lt;p&gt;
&lt;a href="file:///C:\Users\jhalleux\AppData\Local\Temp\WindowsLiveWriter-429641856\supfiles4A3CCC8\image%5b29%5d.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image008" border="0" alt="clip_image008" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/StubsasParameterizedModeldwithCodeContra_D4E6/clip_image008_5f0984a3-2fca-4b3f-a226-3e724802109d.gif" width="622" height="82" /&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
Note that all those behaviors rely on extensions to Pex that the wizard automatically
inserted in the PexAssemblyInfo.cs file:
&lt;/p&gt;
&lt;p&gt;
&lt;a href="file:///C:\Users\jhalleux\AppData\Local\Temp\WindowsLiveWriter-429641856\supfiles4A3CCC8\image%5b34%5d.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image010" border="0" alt="clip_image010" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/StubsasParameterizedModeldwithCodeContra_D4E6/clip_image010_4b1ce042-a76c-490a-a4a3-90613d0f528d.gif" width="518" height="115" /&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
where
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
[PexAssumeContractEnsuresFailureAtStubSurface] filters post-condition violations in
stubs as assumptions, 
&lt;/li&gt;
&lt;li&gt;
[PexChooseAsStubFallbackBehavior] installs Pex choices as the fallback behavior of
stubs, 
&lt;/li&gt;
&lt;li&gt;
[PexStubsPackage] load the stubs package (this is a standard procedure for Pex packages), 
&lt;/li&gt;
&lt;li&gt;
[PexUseStubsFromTestAssembly] specifies to Pex that it should considers stub types
when trying to test interfaces or abstract classes. 
&lt;/li&gt;
&lt;/ul&gt;
&lt;img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=bbda23d8-9f56-4ce8-96b1-b222841d2093" /&gt;</description>
      <comments>http://blog.dotnetwiki.org/CommentView,guid,bbda23d8-9f56-4ce8-96b1-b222841d2093.aspx</comments>
      <category>Code Contracts</category>
      <category>Pex</category>
      <category>Stubs</category>
      <category>Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.dotnetwiki.org/Trackback.aspx?guid=18226b58-0d8f-484e-a07d-12f3ebae8c1e</trackback:ping>
      <pingback:server>http://blog.dotnetwiki.org/pingback.aspx</pingback:server>
      <pingback:target>http://blog.dotnetwiki.org/PermaLink,guid,18226b58-0d8f-484e-a07d-12f3ebae8c1e.aspx</pingback:target>
      <dc:creator>Jonathan de Halleux</dc:creator>
      <wfw:comment>http://blog.dotnetwiki.org/CommentView,guid,18226b58-0d8f-484e-a07d-12f3ebae8c1e.aspx</wfw:comment>
      <wfw:commentRss>http://blog.dotnetwiki.org/SyndicationService.asmx/GetEntryCommentsRss?guid=18226b58-0d8f-484e-a07d-12f3ebae8c1e</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
We just made a <a href="http://research.microsoft.com/pex/downloads.aspx">quick release</a> to
fix another installer issue related to missing packages. Along the way, we’ve added
an <strong>Exploration Tree View</strong> and <strong>Partial Stubs</strong> support.
</p>
        <p>
          <strong>Exploration Tree View</strong>
        </p>
        <p>
The exploration tree view displays the list of explorations to be executed, running
and finished. It serves as a progress indicator but also as a smooth result explorer.
When browsing through the tree, <a href="http://research.microsoft.com/pex" target="_blank">Pex</a> will
synchronize the exploration result view and the tree view.
</p>
        <p>
The tree view populates each namespace with the fixture types and exploration methods,
and provide a visual feedback on the progress of Pex.
</p>
        <p>
          <a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pex0.12.404ExplorationTreeViewPartialSt_D1CB/image_4.png">
            <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pex0.12.404ExplorationTreeViewPartialSt_D1CB/image_thumb_1.png" width="390" height="363" />
          </a>
        </p>
        <p>
When you browse through the exploration and generated test nodes, Pex automatically
synchronizes the exploration result display. This makes it really easy to start from
an high-level view of the failures and drill into a particular generated test, with
stack trace and parameter table.
</p>
        <p>
          <a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pex0.12.404ExplorationTreeViewPartialSt_D1CB/image_2.png">
            <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pex0.12.404ExplorationTreeViewPartialSt_D1CB/image_thumb.png" width="604" height="212" />
          </a>
        </p>
        <p>
          <strong>
          </strong>
        </p>
        <p>
          <strong>Partial Stubs</strong>
        </p>
        <p>
Stubs lets you call the base class implementation of methods as a fallback behavior.
This functionality is commonly referred as <a href="http://ayende.com/Wiki/Default.aspx?Page=Rhino+Mocks+Partial+Mocks">Partial
Mocks</a> or Partial Stubs and is useful to test abstract classes in isolation. Stubs
inheriting from classes have a “CallBase” property that can turn this mode on and
off.
</p>
        <p>
Let see this with the <a href="http://ayende.com/Wiki/Default.aspx?Page=Rhino+Mocks+Partial+Mocks">RhinoMocks
example on partial mocks</a>. Given an abstract ProcessorBase class,
</p>
        <p>
          <a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pex0.12.404ExplorationTreeViewPartialSt_D1CB/image_10.png">
            <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pex0.12.404ExplorationTreeViewPartialSt_D1CB/image_thumb_4.png" width="337" height="207" />
          </a>
        </p>
        <p>
we write a test for the Inc method. To do so, we provide a stub implementation of
Add that simply increment a counter.
</p>
        <p>
          <a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pex0.12.404ExplorationTreeViewPartialSt_D1CB/image_8.png">
            <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pex0.12.404ExplorationTreeViewPartialSt_D1CB/image_thumb_3.png" width="558" height="210" />
          </a>
        </p>
        <p>
          <strong>Miscellaneous</strong>
        </p>
        <ul>
          <li>
PexAssume/PexAssert.EnumIsDefine checks that a particular value is defined in an enum. 
</li>
          <li>
Missing OpenMP files in the installer break Pex.</li>
        </ul>
        <p>
          <strong>
          </strong>
        </p>
        <p>
          <strong>Poll:</strong> should we skip 0.13 and go straight for 0.14? :)
</p>
        <img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=18226b58-0d8f-484e-a07d-12f3ebae8c1e" />
      </body>
      <title>Pex 0.12.40430.3 : Exploration Tree View, Partial Stubs…</title>
      <guid isPermaLink="false">http://blog.dotnetwiki.org/PermaLink,guid,18226b58-0d8f-484e-a07d-12f3ebae8c1e.aspx</guid>
      <link>http://blog.dotnetwiki.org/2009/05/01/Pex012404303ExplorationTreeViewPartialStubs.aspx</link>
      <pubDate>Fri, 01 May 2009 20:28:25 GMT</pubDate>
      <description>&lt;p&gt;
We just made a &lt;a href="http://research.microsoft.com/pex/downloads.aspx"&gt;quick release&lt;/a&gt; to
fix another installer issue related to missing packages. Along the way, we’ve added
an &lt;strong&gt;Exploration Tree View&lt;/strong&gt; and &lt;strong&gt;Partial Stubs&lt;/strong&gt; support.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Exploration Tree View&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
The exploration tree view displays the list of explorations to be executed, running
and finished. It serves as a progress indicator but also as a smooth result explorer.
When browsing through the tree, &lt;a href="http://research.microsoft.com/pex" target="_blank"&gt;Pex&lt;/a&gt; will
synchronize the exploration result view and the tree view.
&lt;/p&gt;
&lt;p&gt;
The tree view populates each namespace with the fixture types and exploration methods,
and provide a visual feedback on the progress of Pex.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pex0.12.404ExplorationTreeViewPartialSt_D1CB/image_4.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pex0.12.404ExplorationTreeViewPartialSt_D1CB/image_thumb_1.png" width="390" height="363" /&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
When you browse through the exploration and generated test nodes, Pex automatically
synchronizes the exploration result display. This makes it really easy to start from
an high-level view of the failures and drill into a particular generated test, with
stack trace and parameter table.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pex0.12.404ExplorationTreeViewPartialSt_D1CB/image_2.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pex0.12.404ExplorationTreeViewPartialSt_D1CB/image_thumb.png" width="604" height="212" /&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Partial Stubs&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
Stubs lets you call the base class implementation of methods as a fallback behavior.
This functionality is commonly referred as &lt;a href="http://ayende.com/Wiki/Default.aspx?Page=Rhino+Mocks+Partial+Mocks"&gt;Partial
Mocks&lt;/a&gt; or Partial Stubs and is useful to test abstract classes in isolation. Stubs
inheriting from classes have a “CallBase” property that can turn this mode on and
off.
&lt;/p&gt;
&lt;p&gt;
Let see this with the &lt;a href="http://ayende.com/Wiki/Default.aspx?Page=Rhino+Mocks+Partial+Mocks"&gt;RhinoMocks
example on partial mocks&lt;/a&gt;. Given an abstract ProcessorBase class,
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pex0.12.404ExplorationTreeViewPartialSt_D1CB/image_10.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pex0.12.404ExplorationTreeViewPartialSt_D1CB/image_thumb_4.png" width="337" height="207" /&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
we write a test for the Inc method. To do so, we provide a stub implementation of
Add that simply increment a counter.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pex0.12.404ExplorationTreeViewPartialSt_D1CB/image_8.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pex0.12.404ExplorationTreeViewPartialSt_D1CB/image_thumb_3.png" width="558" height="210" /&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Miscellaneous&lt;/strong&gt;
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
PexAssume/PexAssert.EnumIsDefine checks that a particular value is defined in an enum. 
&lt;/li&gt;
&lt;li&gt;
Missing OpenMP files in the installer break Pex.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
&lt;strong&gt;&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Poll:&lt;/strong&gt; should we skip 0.13 and go straight for 0.14? :)
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=18226b58-0d8f-484e-a07d-12f3ebae8c1e" /&gt;</description>
      <comments>http://blog.dotnetwiki.org/CommentView,guid,18226b58-0d8f-484e-a07d-12f3ebae8c1e.aspx</comments>
      <category>Pex</category>
      <category>RiSE</category>
      <category>Stubs</category>
      <category>Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.dotnetwiki.org/Trackback.aspx?guid=9997ca8f-c91a-4581-839f-459e8dc06dc0</trackback:ping>
      <pingback:server>http://blog.dotnetwiki.org/pingback.aspx</pingback:server>
      <pingback:target>http://blog.dotnetwiki.org/PermaLink,guid,9997ca8f-c91a-4581-839f-459e8dc06dc0.aspx</pingback:target>
      <dc:creator>Jonathan de Halleux</dc:creator>
      <wfw:comment>http://blog.dotnetwiki.org/CommentView,guid,9997ca8f-c91a-4581-839f-459e8dc06dc0.aspx</wfw:comment>
      <wfw:commentRss>http://blog.dotnetwiki.org/SyndicationService.asmx/GetEntryCommentsRss?guid=9997ca8f-c91a-4581-839f-459e8dc06dc0</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I recorded a <a href="http://channel9.msdn.com/posts/Peli/The-Synergy-of-Code-Contracts-and-Pex/" target="_blank">Channel9
video</a> last week with <a href="http://research.microsoft.com/~maf" target="_blank">Manuel
Fahndrich</a> on the interaction of Code <a href="http://research.microsoft.com/contracts" target="_blank">Contracts</a> and <a href="http://research.microsoft.com/pex" target="_blank">Pex</a>.
The demo gives a glimpse at the nice interaction between Contracts (design by contracts)
and Pex (automated white box).
</p>
        <p>
Code Contracts gives you a great way to specify what your code is supposed to do.
These contracts can be leverage for documentation, static checkers but also – tada
– by Pex! Contracts can also be turned into runtime checks which Pex will try to explore.
Pex will try to cover the post conditions, assertions or in other words, find inputs
that violates of your contracts etc… By adding contracts to your code, you give Pex
a ‘direction’ to search for (and a test oracle).
</p>
        <p>
          <a href="http://channel9.msdn.com/posts/Peli/The-Synergy-of-Code-Contracts-and-Pex/">
            <img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/DesignByContractsmeetsAutomatedWhiteboxT_1261F/image_3.png" width="328" height="247" />
          </a> 
</p>
        <p>
Drop me a note if you want more of those movies.
</p>
        <img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=9997ca8f-c91a-4581-839f-459e8dc06dc0" />
      </body>
      <title>Design By Contracts meets Automated White box Testing</title>
      <guid isPermaLink="false">http://blog.dotnetwiki.org/PermaLink,guid,9997ca8f-c91a-4581-839f-459e8dc06dc0.aspx</guid>
      <link>http://blog.dotnetwiki.org/2009/04/26/DesignByContractsMeetsAutomatedWhiteBoxTesting.aspx</link>
      <pubDate>Sun, 26 Apr 2009 04:09:57 GMT</pubDate>
      <description>&lt;p&gt;
I recorded a &lt;a href="http://channel9.msdn.com/posts/Peli/The-Synergy-of-Code-Contracts-and-Pex/" target="_blank"&gt;Channel9
video&lt;/a&gt; last week with &lt;a href="http://research.microsoft.com/~maf" target="_blank"&gt;Manuel
Fahndrich&lt;/a&gt; on the interaction of Code &lt;a href="http://research.microsoft.com/contracts" target="_blank"&gt;Contracts&lt;/a&gt; and &lt;a href="http://research.microsoft.com/pex" target="_blank"&gt;Pex&lt;/a&gt;.
The demo gives a glimpse at the nice interaction between Contracts (design by contracts)
and Pex (automated white box).
&lt;/p&gt;
&lt;p&gt;
Code Contracts gives you a great way to specify what your code is supposed to do.
These contracts can be leverage for documentation, static checkers but also – tada
– by Pex! Contracts can also be turned into runtime checks which Pex will try to explore.
Pex will try to cover the post conditions, assertions or in other words, find inputs
that violates of your contracts etc… By adding contracts to your code, you give Pex
a ‘direction’ to search for (and a test oracle).
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://channel9.msdn.com/posts/Peli/The-Synergy-of-Code-Contracts-and-Pex/"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/DesignByContractsmeetsAutomatedWhiteboxT_1261F/image_3.png" width="328" height="247" /&gt;&lt;/a&gt;&amp;#160;
&lt;/p&gt;
&lt;p&gt;
Drop me a note if you want more of those movies.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=9997ca8f-c91a-4581-839f-459e8dc06dc0" /&gt;</description>
      <comments>http://blog.dotnetwiki.org/CommentView,guid,9997ca8f-c91a-4581-839f-459e8dc06dc0.aspx</comments>
      <category>Code Contracts</category>
      <category>Pex</category>
      <category>Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.dotnetwiki.org/Trackback.aspx?guid=7c5d5cdd-7ebc-4a3d-8fb1-e84b187343b2</trackback:ping>
      <pingback:server>http://blog.dotnetwiki.org/pingback.aspx</pingback:server>
      <pingback:target>http://blog.dotnetwiki.org/PermaLink,guid,7c5d5cdd-7ebc-4a3d-8fb1-e84b187343b2.aspx</pingback:target>
      <dc:creator>Jonathan de Halleux</dc:creator>
      <wfw:comment>http://blog.dotnetwiki.org/CommentView,guid,7c5d5cdd-7ebc-4a3d-8fb1-e84b187343b2.aspx</wfw:comment>
      <wfw:commentRss>http://blog.dotnetwiki.org/SyndicationService.asmx/GetEntryCommentsRss?guid=7c5d5cdd-7ebc-4a3d-8fb1-e84b187343b2</wfw:commentRss>
      <slash:comments>3</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
          <em>Update: Andrew Kazyrevich does the final performance analysis <a href="http://codevanced.net/post/The-Shark-Fin-Pex.aspx">on
his blog</a>. 
<br />
Update II: Stubs v0.12.40430.3 has Partial Stubs support. </em>
        </p>
        <p>
          <a href="http://research.microsoft.com/stubs" target="_blank">Stubs</a> is a lightweight
stub framework that is entirely based on delegates. We designed it so that it brings
as little overhead as possible, and as a side effect, is very effective with <a href="http://research.microsoft.com/pex" target="_blank">Pex</a>.
In this post, we’ll see how Stubs ‘look’ with respect to other frameworks.
</p>
        <p>
          <strong>Mocking-framework-compare</strong>
        </p>
        <p>
          <a href="http://codevanced.net/" target="_blank">Andrew Kazyrevich</a> started a project, <a href="http://code.google.com/p/mocking-frameworks-compare/" target="_blank">mocking-framework-compare</a>,
back in February that compared Moq, Rhino, NMock2 and Isolator. This is project compares
various ‘mocking scenarios’ across all those frameworks. I added Stubs to the mix
today (go <a href="http://code.google.com/p/mocking-frameworks-compare/source/detail?r=56" target="_blank">check
it out</a>).
</p>
        <p>
          <strong>What’s a stub in Stubs anyway?</strong>
        </p>
        <p>
Before we dig in, let’s recap how stubs look like when you use Stubs. Stubs uses delegate
to ‘hook’ behavior to interface members.<strong></strong>For every stubbed interface
and class, code is generated at a compile time: One stub class per interface and class.
For each stubbed method, the stub class contains a field. The field has a delegate
type matching the stubbed method. As a user, you can simply attach delegates (or lambdas)
to the delegate fields to assign<strong>*any*</strong> behavior to each member. If
no user delegate is set, Stubs calls into a fallback behavior, i.e. throw or return
the default value. 
</p>
        <p>
Let’s take one of the examples of mocking-framework-compare. The little snippet below
‘stubs’ the TouchIron() method (which is implemented explicitly) by attaching a lambda
to the TouchIron field.
</p>
        <p>
          <a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/StubsComparisonwithothermockingframework_1415A/image_2.png">
            <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/StubsComparisonwithothermockingframework_1415A/image_thumb.png" width="324" height="133" />
          </a>
        </p>
        <p>
SIHand is the stub type of the IHand interface, it was generated at compile time by
the Stubs framework. Its implementation looks more or less like this:
</p>
        <p>
          <a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/StubsComparisonwithothermockingframework_1415A/image_4.png">
            <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/StubsComparisonwithothermockingframework_1415A/image_thumb_1.png" width="504" height="289" />
          </a>
        </p>
        <p>
The TouchIron methods really shows the main idea behind stubs: delegates are all we
need to move behavior around.
</p>
        <p>
          <strong>Stubs: relying on the language rather than an API</strong>
        </p>
        <p>
One of the differences of Stubs with other mock frameworks is that Stubs does not
have any API: delegates, lambdas, closures are all features of the programming language,
while assertions are part of the test framework. This is quite obvious when we compare
the BrainTest <a href="http://code.google.com/p/mocking-frameworks-compare/source/browse/trunk/Tests/CS/MoqTests/BrainTests.cs" target="_blank">using
Moq</a> and <a href="http://code.google.com/p/mocking-frameworks-compare/source/browse/trunk/Tests/CS/StubsTests/BrainTests.cs" target="_blank">using
Stubs</a>. In this test, we need to ensure that the brain coordinates the hand and
the mouth so that when a hot iron is touched by the hand, the mouth yells.
</p>
        <ul>
          <li>
Moq: we set up the hand to throw a burn exception when touched with a hot iron and
verify that the mouth yelled. 
</li>
        </ul>
        <p>
          <a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/StubsComparisonwithothermockingframework_1415A/image_6.png">
            <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/StubsComparisonwithothermockingframework_1415A/image_thumb_2.png" width="612" height="172" />
          </a>
        </p>
        <p>
Moq, as Rhino or Isolate, make smart use of expression trees and/or Reflection.Emit
to define the mock behavior. The expectation and behavior are set through nice and
fluent APIs which really make the developer’s life’s easier.
</p>
        <ul>
          <li>
Stubs: same scenario, we attach a lambda that throws if iron is hot and use a local
to verify that Yell is called (this local is pushed to the heap by the compiler). 
</li>
        </ul>
        <p>
          <a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/StubsComparisonwithothermockingframework_1415A/image_8.png">
            <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/StubsComparisonwithothermockingframework_1415A/image_thumb_3.png" width="402" height="316" />
          </a> 
</p>
        <p>
Stubs does not have any API. It relies on lambdas (to define new behaviors), closures
(to track side effects) which are given for free by the C# 3.0 language.
</p>
        <p>
          <strong>Performance numbers</strong>
        </p>
        <p>
Does performance matter when it comes to mocking? It’s not really the question we
are trying to answer here. We’re just looking at the benchmark results from the mocking-framework-compare
project. When looking at the results, you’ll notice that Stubs may be 100x to 1000x
faster than other frameworks. This is no surprise since stubs boil down to a virtual
method call, while other frameworks do much more work (in fact we expected worse numbers).
</p>
        <blockquote>
          <p>
Data units of msec resolution = 0.394937 usec 
</p>
          <p>
Mocking methods. 
<br />
Moq      : 100 repeats:  37.471 +- 12%   msec 
<br />
Rhino    : 100 repeats:  38.030 +- 7%    msec 
<br />
NMock2   : 100 repeats:  24.035 +- 4%    msec 
<br /><strong>Stubs    : 100 repeats:   0.115 +- 8%   
msec </strong></p>
          <p>
Mocking events. 
<br />
Moq      : 100 repeats:  86.913 +- 7%   
msec 
<br />
Rhino    : 100 repeats:  61.142 +- 6%    msec 
<br />
NMock2   : 100 repeats:  27.378 +- 6%    msec 
<br /><strong>Stubs    : 100 repeats:   0.071 +- 6%   
msec </strong></p>
          <p>
Mocking properties. 
<br />
Moq      : 100 repeats:  82.434 +- 6%   
msec 
<br />
Rhino    : 100 repeats:  47.471 +- 5%    msec 
<br />
NMock2   : 100 repeats:  11.334 +- 10%   msec 
<br /><strong>Stubs    : 100 repeats:   0.042 +- 15%  
msec </strong></p>
          <p>
Mocking method arguments. 
<br />
Moq      : 100 repeats: 142.668 +- 4%    msec 
<br />
Rhino    : 100 repeats:  45.118 +- 5%    msec 
<br />
NMock2   : 100 repeats:  22.344 +- 7%    msec 
<br /><strong>Stubs    : 100 repeats:   0.078 +- 4%   
msec </strong></p>
          <p>
Partial mocks. 
<br />
Moq      : 100 repeats: 117.581 +- 5%    msec 
<br />
Rhino    : 100 repeats:  58.827 +- 6%    msec 
<br /><strong>Stubs : 100 repeats:   0.054 +- 6%    msec </strong></p>
          <p>
Recursive mocks. 
<br />
Moq      : 100 repeats:  92.482 +- 4%   
msec 
<br />
Rhino    : 100 repeats:  40.921 +- 3%    msec 
<br /><strong>Stubs    : 100 repeats:   0.493 +- 18%  
msec 
<br /></strong>Press any key to continue . . .
</p>
          <p>
          </p>
          <p>
          </p>
        </blockquote>
        <strong>
        </strong>
        <p>
          <strong>Stubs limitations</strong>
        </p>
        <p>
There are many areas where the Stubs fall short or simply don’t support it. Currently,
Stubs are only emitted for C# and <a href="http://ayende.com/Wiki/Default.aspx?Page=Rhino+Mocks+Partial+Mocks" target="_blank">partial
mocks</a> will be supported in the next version.
</p>
        <p>
          <strong>Getting started with Stubs</strong>
        </p>
        <p>
Stubs comes with the <a href="http://research.microsoft.com/pex/downloads.aspx" target="_blank">Pex
installer</a>. If you’re interested on using it, check out the <a href="http://research.microsoft.com/en-us/projects/stubs/gettingstarted.aspx" target="_blank">getting
started page</a> on the Stubs project page where you can also download the full Stubs
primer.
</p>
        <p>
Cheers, Peli
</p>
        <img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=7c5d5cdd-7ebc-4a3d-8fb1-e84b187343b2" />
      </body>
      <title>Stubs: Comparison with other mocking frameworks</title>
      <guid isPermaLink="false">http://blog.dotnetwiki.org/PermaLink,guid,7c5d5cdd-7ebc-4a3d-8fb1-e84b187343b2.aspx</guid>
      <link>http://blog.dotnetwiki.org/2009/04/23/StubsComparisonWithOtherMockingFrameworks.aspx</link>
      <pubDate>Thu, 23 Apr 2009 04:58:03 GMT</pubDate>
      <description>&lt;p&gt;
&lt;em&gt;Update: Andrew Kazyrevich does the final performance analysis &lt;a href="http://codevanced.net/post/The-Shark-Fin-Pex.aspx"&gt;on
his blog&lt;/a&gt;. 
&lt;br /&gt;
Update II: Stubs v0.12.40430.3 has Partial Stubs support. &lt;/em&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://research.microsoft.com/stubs" target="_blank"&gt;Stubs&lt;/a&gt; is a lightweight
stub framework that is entirely based on delegates. We designed it so that it brings
as little overhead as possible, and as a side effect, is very effective with &lt;a href="http://research.microsoft.com/pex" target="_blank"&gt;Pex&lt;/a&gt;.
In this post, we’ll see how Stubs ‘look’ with respect to other frameworks.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Mocking-framework-compare&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://codevanced.net/" target="_blank"&gt;Andrew Kazyrevich&lt;/a&gt; started a project, &lt;a href="http://code.google.com/p/mocking-frameworks-compare/" target="_blank"&gt;mocking-framework-compare&lt;/a&gt;,
back in February that compared Moq, Rhino, NMock2 and Isolator. This is project compares
various ‘mocking scenarios’ across all those frameworks. I added Stubs to the mix
today (go &lt;a href="http://code.google.com/p/mocking-frameworks-compare/source/detail?r=56" target="_blank"&gt;check
it out&lt;/a&gt;).
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;What’s a stub in Stubs anyway?&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
Before we dig in, let’s recap how stubs look like when you use Stubs. Stubs uses delegate
to ‘hook’ behavior to interface members.&lt;strong&gt; &lt;/strong&gt;For every stubbed interface
and class, code is generated at a compile time: One stub class per interface and class.
For each stubbed method, the stub class contains a field. The field has a delegate
type matching the stubbed method. As a user, you can simply attach delegates (or lambdas)
to the delegate fields to assign&lt;strong&gt;*any*&lt;/strong&gt; behavior to each member. If
no user delegate is set, Stubs calls into a fallback behavior, i.e. throw or return
the default value. 
&lt;/p&gt;
&lt;p&gt;
Let’s take one of the examples of mocking-framework-compare. The little snippet below
‘stubs’ the TouchIron() method (which is implemented explicitly) by attaching a lambda
to the TouchIron field.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/StubsComparisonwithothermockingframework_1415A/image_2.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/StubsComparisonwithothermockingframework_1415A/image_thumb.png" width="324" height="133" /&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
SIHand is the stub type of the IHand interface, it was generated at compile time by
the Stubs framework. Its implementation looks more or less like this:
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/StubsComparisonwithothermockingframework_1415A/image_4.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/StubsComparisonwithothermockingframework_1415A/image_thumb_1.png" width="504" height="289" /&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
The TouchIron methods really shows the main idea behind stubs: delegates are all we
need to move behavior around.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Stubs: relying on the language rather than an API&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
One of the differences of Stubs with other mock frameworks is that Stubs does not
have any API: delegates, lambdas, closures are all features of the programming language,
while assertions are part of the test framework. This is quite obvious when we compare
the BrainTest &lt;a href="http://code.google.com/p/mocking-frameworks-compare/source/browse/trunk/Tests/CS/MoqTests/BrainTests.cs" target="_blank"&gt;using
Moq&lt;/a&gt; and &lt;a href="http://code.google.com/p/mocking-frameworks-compare/source/browse/trunk/Tests/CS/StubsTests/BrainTests.cs" target="_blank"&gt;using
Stubs&lt;/a&gt;. In this test, we need to ensure that the brain coordinates the hand and
the mouth so that when a hot iron is touched by the hand, the mouth yells.
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
Moq: we set up the hand to throw a burn exception when touched with a hot iron and
verify that the mouth yelled. 
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
&lt;a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/StubsComparisonwithothermockingframework_1415A/image_6.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/StubsComparisonwithothermockingframework_1415A/image_thumb_2.png" width="612" height="172" /&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
Moq, as Rhino or Isolate, make smart use of expression trees and/or Reflection.Emit
to define the mock behavior. The expectation and behavior are set through nice and
fluent APIs which really make the developer’s life’s easier.
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
Stubs: same scenario, we attach a lambda that throws if iron is hot and use a local
to verify that Yell is called (this local is pushed to the heap by the compiler). 
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
&lt;a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/StubsComparisonwithothermockingframework_1415A/image_8.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/StubsComparisonwithothermockingframework_1415A/image_thumb_3.png" width="402" height="316" /&gt;&lt;/a&gt;&amp;#160;
&lt;/p&gt;
&lt;p&gt;
Stubs does not have any API. It relies on lambdas (to define new behaviors), closures
(to track side effects) which are given for free by the C# 3.0 language.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Performance numbers&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
Does performance matter when it comes to mocking? It’s not really the question we
are trying to answer here. We’re just looking at the benchmark results from the mocking-framework-compare
project. When looking at the results, you’ll notice that Stubs may be 100x to 1000x
faster than other frameworks. This is no surprise since stubs boil down to a virtual
method call, while other frameworks do much more work (in fact we expected worse numbers).
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
Data units of msec resolution = 0.394937 usec 
&lt;/p&gt;
&lt;p&gt;
Mocking methods. 
&lt;br /&gt;
Moq&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; : 100 repeats:&amp;#160; 37.471 +- 12%&amp;#160;&amp;#160; msec 
&lt;br /&gt;
Rhino&amp;#160;&amp;#160;&amp;#160; : 100 repeats:&amp;#160; 38.030 +- 7%&amp;#160;&amp;#160;&amp;#160; msec 
&lt;br /&gt;
NMock2&amp;#160;&amp;#160; : 100 repeats:&amp;#160; 24.035 +- 4%&amp;#160;&amp;#160;&amp;#160; msec 
&lt;br /&gt;
&lt;strong&gt;Stubs&amp;#160;&amp;#160;&amp;#160; : 100 repeats:&amp;#160;&amp;#160; 0.115 +- 8%&amp;#160;&amp;#160;&amp;#160;
msec &lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
Mocking events. 
&lt;br /&gt;
Moq&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; : 100 repeats:&amp;#160; 86.913 +- 7%&amp;#160;&amp;#160;&amp;#160;
msec 
&lt;br /&gt;
Rhino&amp;#160;&amp;#160;&amp;#160; : 100 repeats:&amp;#160; 61.142 +- 6%&amp;#160;&amp;#160;&amp;#160; msec 
&lt;br /&gt;
NMock2&amp;#160;&amp;#160; : 100 repeats:&amp;#160; 27.378 +- 6%&amp;#160;&amp;#160;&amp;#160; msec 
&lt;br /&gt;
&lt;strong&gt;Stubs&amp;#160;&amp;#160;&amp;#160; : 100 repeats:&amp;#160;&amp;#160; 0.071 +- 6%&amp;#160;&amp;#160;&amp;#160;
msec &lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
Mocking properties. 
&lt;br /&gt;
Moq&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; : 100 repeats:&amp;#160; 82.434 +- 6%&amp;#160;&amp;#160;&amp;#160;
msec 
&lt;br /&gt;
Rhino&amp;#160;&amp;#160;&amp;#160; : 100 repeats:&amp;#160; 47.471 +- 5%&amp;#160;&amp;#160;&amp;#160; msec 
&lt;br /&gt;
NMock2&amp;#160;&amp;#160; : 100 repeats:&amp;#160; 11.334 +- 10%&amp;#160;&amp;#160; msec 
&lt;br /&gt;
&lt;strong&gt;Stubs&amp;#160;&amp;#160;&amp;#160; : 100 repeats:&amp;#160;&amp;#160; 0.042 +- 15%&amp;#160;&amp;#160;
msec &lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
Mocking method arguments. 
&lt;br /&gt;
Moq&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; : 100 repeats: 142.668 +- 4%&amp;#160;&amp;#160;&amp;#160; msec 
&lt;br /&gt;
Rhino&amp;#160;&amp;#160;&amp;#160; : 100 repeats:&amp;#160; 45.118 +- 5%&amp;#160;&amp;#160;&amp;#160; msec 
&lt;br /&gt;
NMock2&amp;#160;&amp;#160; : 100 repeats:&amp;#160; 22.344 +- 7%&amp;#160;&amp;#160;&amp;#160; msec 
&lt;br /&gt;
&lt;strong&gt;Stubs&amp;#160;&amp;#160;&amp;#160; : 100 repeats:&amp;#160;&amp;#160; 0.078 +- 4%&amp;#160;&amp;#160;&amp;#160;
msec &lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
Partial mocks. 
&lt;br /&gt;
Moq&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; : 100 repeats: 117.581 +- 5%&amp;#160;&amp;#160;&amp;#160; msec 
&lt;br /&gt;
Rhino&amp;#160;&amp;#160;&amp;#160; : 100 repeats:&amp;#160; 58.827 +- 6%&amp;#160;&amp;#160;&amp;#160; msec 
&lt;br /&gt;
&lt;strong&gt;Stubs : 100 repeats:&amp;#160;&amp;#160; 0.054 +- 6%&amp;#160;&amp;#160;&amp;#160; msec &lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
Recursive mocks. 
&lt;br /&gt;
Moq&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; : 100 repeats:&amp;#160; 92.482 +- 4%&amp;#160;&amp;#160;&amp;#160;
msec 
&lt;br /&gt;
Rhino&amp;#160;&amp;#160;&amp;#160; : 100 repeats:&amp;#160; 40.921 +- 3%&amp;#160;&amp;#160;&amp;#160; msec 
&lt;br /&gt;
&lt;strong&gt;Stubs&amp;#160;&amp;#160;&amp;#160; : 100 repeats:&amp;#160;&amp;#160; 0.493 +- 18%&amp;#160;&amp;#160;
msec 
&lt;br /&gt;
&lt;/strong&gt;Press any key to continue . . .
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;/blockquote&gt; &lt;strong&gt;&lt;/strong&gt; 
&lt;p&gt;
&lt;strong&gt;Stubs limitations&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
There are many areas where the Stubs fall short or simply don’t support it. Currently,
Stubs are only emitted for C# and &lt;a href="http://ayende.com/Wiki/Default.aspx?Page=Rhino+Mocks+Partial+Mocks" target="_blank"&gt;partial
mocks&lt;/a&gt; will be supported in the next version.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Getting started with Stubs&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
Stubs comes with the &lt;a href="http://research.microsoft.com/pex/downloads.aspx" target="_blank"&gt;Pex
installer&lt;/a&gt;. If you’re interested on using it, check out the &lt;a href="http://research.microsoft.com/en-us/projects/stubs/gettingstarted.aspx" target="_blank"&gt;getting
started page&lt;/a&gt; on the Stubs project page where you can also download the full Stubs
primer.
&lt;/p&gt;
&lt;p&gt;
Cheers, Peli
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=7c5d5cdd-7ebc-4a3d-8fb1-e84b187343b2" /&gt;</description>
      <comments>http://blog.dotnetwiki.org/CommentView,guid,7c5d5cdd-7ebc-4a3d-8fb1-e84b187343b2.aspx</comments>
      <category>Pex</category>
      <category>Stubs</category>
      <category>Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.dotnetwiki.org/Trackback.aspx?guid=83fde01f-786e-456d-b1c4-c8dd52448e69</trackback:ping>
      <pingback:server>http://blog.dotnetwiki.org/pingback.aspx</pingback:server>
      <pingback:target>http://blog.dotnetwiki.org/PermaLink,guid,83fde01f-786e-456d-b1c4-c8dd52448e69.aspx</pingback:target>
      <dc:creator>Jonathan de Halleux</dc:creator>
      <wfw:comment>http://blog.dotnetwiki.org/CommentView,guid,83fde01f-786e-456d-b1c4-c8dd52448e69.aspx</wfw:comment>
      <wfw:commentRss>http://blog.dotnetwiki.org/SyndicationService.asmx/GetEntryCommentsRss?guid=83fde01f-786e-456d-b1c4-c8dd52448e69</wfw:commentRss>
      <slash:comments>12</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <blockquote>
          <p>
            <a href="http://research.microsoft.com/en-us/projects/pex/downloads.aspx" target="_blank">
              <strong>Download
the new version!</strong>
            </a>
          </p>
        </blockquote>
        <p>
Today we’ve released a new release of <a href="http://research.microsoft.com/pex">Pex</a> on
DevLabs and on our academic downloads. This highlights of this release are: <a href="http://www.nunit.org" target="_blank">NUnit</a>, <a href="http://www.mbunit.com/" target="_blank">MbUnit</a> and <a href="http://xunit.codeplex.com/" target="_blank">xUnit.net</a> support
out of the box, <strong>writing <a href="http://research.microsoft.com/pex">parameterized
unit tests</a> in VisualBasic.NET and F#, better <a href="http://research.microsoft.com/contracts">Code
Contracts</a> support</strong>. As always, if we encourage you to send us feedback,
bugs, stories on our forums at <a href="http://social.msdn.microsoft.com/Forums/en-US/pex/threads/">http://social.msdn.microsoft.com/Forums/en-US/pex/threads/</a> .
</p>
        <p>
          <strong>
            <a href="http://www.nunit.org" target="_blank">NUnit</a>, <a href="http://www.mbunit.com" target="_blank">MbUnit</a> and <a href="http://www.codeplex.com/xunit">xUnit.net</a> supported
out of the box</strong>
        </p>
        <p>
Pex now supports MSTest, NUnit, MbUnit and xUnit.net out of the box. Pex will automatically
detect which framework you are using by inspecting the assembly reference list, and
automatically save the generated tests decorated with the correct attributes for that
framework. 
</p>
        <p>
          <a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.10.40408.0NUnitMbUnitx.NETFandmore_14623/image_22.png">
            <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.10.40408.0NUnitMbUnitx.NETFandmore_14623/image_thumb_10.png" width="604" height="351" />
          </a>
        </p>
        <p>
The default test framework can also be specified through the global options (Tools
–&gt; Options –&gt; Pex –&gt; enter the test framework name in TestFramework).
</p>
        <p>
          <a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.10.40408.0NUnitMbUnitx.NETFandmore_14623/image_16.png">
            <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.10.40408.0NUnitMbUnitx.NETFandmore_14623/image_thumb_7.png" width="604" height="257" />
          </a>
        </p>
        <p>
 
</p>
        <p>
          <strong>Writing Parameterized Unit Tests in VisualBasic.NET</strong>
        </p>
        <p>
While the Pex white box analysis engine works at the MSIL level, Pex only emits C#
code for now. In previous releases, this limitation made it impossible to use Pex
parameterized unit tests from non-C# code. In this release, we have worked around
this problem by automatically saving the generated tests in a ‘satellite’ C# project.
</p>
        <p>
Let’s see this with an example. The screenshot below shows a single VisualBasic.NET
test project with a Pex parameterized unit test:
</p>
        <p>
          <a href="file:///C:\Users\jhalleux\AppData\Local\Temp\WindowsLiveWriter-429641856\supfiles659CA51\image3.png">
            <b>
              <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image002" border="0" alt="clip_image002" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.10.40408.0NUnitMbUnitx.NETFandmore_14623/clip_image002_0dd1c4a7-6732-42b8-b6fe-76f7499e6a66.gif" width="604" height="237" />
            </b>
          </a>
        </p>
        <p>
We can right-click in the HelloTest.Hello method and select “Run Pex Explorations”:
</p>
        <p>
          <a href="file:///C:\Users\jhalleux\AppData\Local\Temp\WindowsLiveWriter-429641856\supfiles659CA51\image7.png">
            <b>
              <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image004" border="0" alt="clip_image004" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.10.40408.0NUnitMbUnitx.NETFandmore_14623/clip_image004_31e0ecca-a518-4ec3-9ad4-3fef0bb58593.gif" width="305" height="91" />
            </b>
          </a>
        </p>
        <p>
At this point, Pex will start exploring the test in the background as usual. This
is where the new support comes in: When a generated test comes back to Visual Studio,
Pex will save it in a separate C# project automatically (after asking you where to
drop the new project):
</p>
        <p>
          <a href="file:///C:\Users\jhalleux\AppData\Local\Temp\WindowsLiveWriter-429641856\supfiles659CA51\image19.png">
            <b>
              <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image006" border="0" alt="clip_image006" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.10.40408.0NUnitMbUnitx.NETFandmore_14623/clip_image006_324456a0-a756-4cfe-b9cb-e7eb8a9db8a0.gif" width="604" height="391" />
            </b>
          </a>
        </p>
        <p>
The generated tests are now ready to be run just as any other unit tests!
</p>
        <p>
          <strong>Writing Parameterized Unit Tests from F#</strong>
        </p>
        <p>
Similarly to VisualBasic.NET, we’ve made improvements in our infrastructure to enable
writing parameterized unit tests in <strong>F#</strong>. Let’s see this with a familiar
example. We have a single F# library that has xUnit.net unit tests and reference Microsoft.Pex.Framework
(project Library2 below). In that project, we add a parameterized unit test (hello_test):
</p>
        <p>
          <a href="file:///C:\Users\jhalleux\AppData\Local\Temp\WindowsLiveWriter-429641856\supfiles659CA51\image32%5b1%5d.png">
            <b>
              <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image008" border="0" alt="clip_image008" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.10.40408.0NUnitMbUnitx.NETFandmore_14623/clip_image008_709d95c3-12c4-4805-b549-2982d5341032.gif" width="604" height="235" />
            </b>
          </a>
        </p>
        <p>
We can right-click on the <strong>test method name</strong> and Pex will start the
exploration of that test in the background. Because of the limitations of the F# project
system, you <strong>absolutely</strong> need to right-click on the<strong> method
name</strong> in F# if you want contextual test selection to work. Because the project
is already referencing xunit.dll, Pex will also automatically detect that you are
using xUnit.net and use that framework. When the first test case comes back to VisualStudio,
Pex saves it in a separate C# project:
</p>
        <p>
          <a href="file:///C:\Users\jhalleux\AppData\Local\Temp\WindowsLiveWriter-429641856\supfiles659CA51\image39.png">
            <b>
              <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image010" border="0" alt="clip_image010" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.10.40408.0NUnitMbUnitx.NETFandmore_14623/clip_image010_69f5c0e9-4c03-4251-a79a-38c7ffbb3a19.gif" width="604" height="341" />
            </b>
          </a>
        </p>
        <p>
The tests are saved in the generated test project and ready to be run by your favorite
test runner!
</p>
        <p>
          <strong>PexObserve: Observing values, Asserting values</strong>
        </p>
        <p>
We’ve completely re-factored the way values can be logged on the table or saved as
assertions in the generated tests. The following example shows various ways to log
and assert values:
</p>
        <p>
          <a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.10.40408.0NUnitMbUnitx.NETFandmore_14623/image_8.png">
            <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.10.40408.0NUnitMbUnitx.NETFandmore_14623/image_thumb_3.png" width="451" height="228" />
          </a>
        </p>
        <p>
In the Observe method, we use the return value and out parameter output to automatically
log and assert those values. Additionally, we add “view input” on the fly to the parameter
table through the ValueForViewing method, and we add “check input” to be asserted
through the ValueAtEndOfTest method. After running Pex, we get the following results:
</p>
        <p>
          <a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.10.40408.0NUnitMbUnitx.NETFandmore_14623/image_10.png">
            <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.10.40408.0NUnitMbUnitx.NETFandmore_14623/image_thumb_4.png" width="373" height="63" />
          </a>
        </p>
        <p>
As expected, input, ‘view input’, output and result show up in the parameter table.
</p>
        <p>
          <a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.10.40408.0NUnitMbUnitx.NETFandmore_14623/image_12.png">
            <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.10.40408.0NUnitMbUnitx.NETFandmore_14623/image_thumb_5.png" width="599" height="266" />
          </a>
        </p>
        <p>
In the generated test, we see assertions for the return value, out parameters and
other values passed through the ValueAtEndOfTest method.
</p>
        <p>
          <strong>
            <a href="http://research.microsoft.com/contracts">Code Contracts</a> : Reproducible
generated tests</strong>
        </p>
        <p>
When Pex generates a unit test that relied on a runtime contract, Pex also adds a
check to the unit test which validates that the contracts have been injected into
the code by the contracts rewriter. If the code is not rewritten when re-executing
the unit test, it is marked as inconclusive. You will appreciate this behavior when
you run your unit tests both in Release and in Debug builds, which usually differ
in how contracts get injected.
</p>
        <p>
          <a href="file:///C:\Users\jhalleux\AppData\Local\Temp\WindowsLiveWriter-429641856\supfiles659CA51\image43.png">
            <b>
              <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image012" border="0" alt="clip_image012" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.10.40408.0NUnitMbUnitx.NETFandmore_14623/clip_image012_32c70deb-1184-48c7-a1cd-16e1c19559d3.gif" width="604" height="177" />
            </b>
          </a>
        </p>
        <p>
          <strong>Code Contracts:  Automatic filtering of the contract violations</strong>
        </p>
        <p>
When Pex generates a test that violates a Code Contract pre-condition (i.e. Contract.Requires),
there are basically two scenarios: the precondition was on top of the stack and should
be considered as an expected exception; or it is a nested exception and should be
considered as a bug. Pex provides a default exception filtering that implements this
behavior.
</p>
        <p>
          <strong>
            <a href="http://research.microsoft.com/stubs" target="_blank">Stubs</a>: simplified
syntax</strong>
        </p>
        <p>
We’ve considerably simplified the syntax of stubs by removing the ‘this’ parameter
from the stub delegate definition. Let’s illustrate this with a test that stubs the
‘ReadAllText’ method of a fictitious ‘IFileSystem’ interface.  
</p>
        <p>
          <img src="http://research.microsoft.com/en-us/projects/stubs/stubexample.png" />
        </p>
        <p>
          <strong>
            <a href="http://research.microsoft.com/stubs" target="_blank">Stubs</a>: generic
methods</strong>
        </p>
        <p>
The <a href="http://research.microsoft.com/stubs" target="_blank">Stubs</a> framework
now supports stubbing generic methods by providing particular instantiations of that
method. In the following example, the generic Bar&lt;T&gt; method is stubbed for the
particular Bar&lt;int&gt; instantiation:
</p>
        <p>
          <a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.10.40408.0NUnitMbUnitx.NETFandmore_14623/image_2.png">
            <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.10.40408.0NUnitMbUnitx.NETFandmore_14623/image_thumb.png" width="435" height="119" />
          </a>
        </p>
        <p>
          <strong>Stubs and Pex: Pex will choose the stubs behavior by default</strong>
        </p>
        <p>
We provide a new custom attribute, PexChooseAsStubFallbackBehaviorAttribute, that
hooks Pex choices to the Stub fallback behavior. To illustrate what this means, let’s
modify slightly the example above by removing the stub of ReadAllText:
</p>
        <p>
          <a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.10.40408.0NUnitMbUnitx.NETFandmore_14623/image_4.png">
            <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.10.40408.0NUnitMbUnitx.NETFandmore_14623/image_thumb_1.png" width="455" height="216" />
          </a>
        </p>
        <p>
If this test was to be run without the PexChooseAsStubFallbackBehavior attribute,
it would throw a StubNotImplementedException. However, with the PexChooseAsStubFallbackBehavior
attribute, the fallback behavior calls into PexChoose to ask Pex for a new string.
In this example in particular, on each call to ReadAllText, Pex will generate a new
string for the result. You can see this string as a new parameter to the parameterized
unit test. Therefore, when we run this test under Pex, we see different behavior happening,
including the “hello world” file:
</p>
        <p>
          <a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.10.40408.0NUnitMbUnitx.NETFandmore_14623/image_6.png">
            <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.10.40408.0NUnitMbUnitx.NETFandmore_14623/image_thumb_2.png" width="584" height="90" />
          </a>
        </p>
        <p>
Note that all the necessary attributes are added at the assembly level by the Pex
Wizard.
</p>
        <p>
          <strong>Miscellanous bug fixes and improvements</strong>
        </p>
        <ul>
          <li>
[fixed] Dialogs do not render correctly under high DPI 
</li>
          <li>
When a generic parameterized unit tests does not have any generic argument instantiations,
Pex makes a guess for you. 
</li>
          <li>
When a test parameter is an interface or an abstract class, Pex now searches the known
assemblies for implementations and concrete classes. In particular, that means that
Pex will often automatically use the automatically generated Stubs implementations
for interfaces or abstract classes. 
</li>
          <li>
Static parameterized unit tests are supported (if static tests are supported by your
test framework) 
</li>
          <li>
Better solving of decimal and floating point constraints. We will report on the details
later. 
</li>
        </ul>
        <p>
          <strong>Breaking Changes</strong>
        </p>
        <ul>
          <li>
The PexFactoryClassAttribute is no longer needed and has been removed. Now, Pex will
pick up object factory methods marked with the PexFactoryMethodAttribute from any <i>static
class</i> in the test project containing the parameterized unit tests. If the generated
tests are stored in a separate project, that project is not searched. 
</li>
          <li>
The PexStore API has been renamed to PexObserve. 
</li>
          <li>
Pex is compatible with Code Contracts versions strictly newer than v1.1.20309.13.
Unfortunately, v1.1.20309.13 is the currently available version of Code <a href="http://research.microsoft.com/contracts" target="_blank">Contracts</a>.
The Code Contracts team is planning on a release soon. 
</li>
        </ul>
        <p>
 
</p>
        <p>
Happy Pexing!
</p>
        <img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=83fde01f-786e-456d-b1c4-c8dd52448e69" />
      </body>
      <title>Pex v0.10.40408.0: NUnit, MbUnit, xUnit.Net, Visual Basic.NET, F# and more…</title>
      <guid isPermaLink="false">http://blog.dotnetwiki.org/PermaLink,guid,83fde01f-786e-456d-b1c4-c8dd52448e69.aspx</guid>
      <link>http://blog.dotnetwiki.org/2009/04/10/PexV010404080NUnitMbUnitXUnitNetVisualBasicNETFAndMore.aspx</link>
      <pubDate>Fri, 10 Apr 2009 19:06:31 GMT</pubDate>
      <description>&lt;blockquote&gt; 
&lt;p&gt;
&lt;a href="http://research.microsoft.com/en-us/projects/pex/downloads.aspx" target="_blank"&gt;&lt;strong&gt;Download
the new version!&lt;/strong&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
Today we’ve released a new release of &lt;a href="http://research.microsoft.com/pex"&gt;Pex&lt;/a&gt; on
DevLabs and on our academic downloads. This highlights of this release are: &lt;a href="http://www.nunit.org" target="_blank"&gt;NUnit&lt;/a&gt;, &lt;a href="http://www.mbunit.com/" target="_blank"&gt;MbUnit&lt;/a&gt; and &lt;a href="http://xunit.codeplex.com/" target="_blank"&gt;xUnit.net&lt;/a&gt; support
out of the box, &lt;strong&gt;writing &lt;a href="http://research.microsoft.com/pex"&gt;parameterized
unit tests&lt;/a&gt; in VisualBasic.NET and F#, better &lt;a href="http://research.microsoft.com/contracts"&gt;Code
Contracts&lt;/a&gt; support&lt;/strong&gt;. As always, if we encourage you to send us feedback,
bugs, stories on our forums at &lt;a href="http://social.msdn.microsoft.com/Forums/en-US/pex/threads/"&gt;http://social.msdn.microsoft.com/Forums/en-US/pex/threads/&lt;/a&gt; .
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;&lt;a href="http://www.nunit.org" target="_blank"&gt;NUnit&lt;/a&gt;, &lt;a href="http://www.mbunit.com" target="_blank"&gt;MbUnit&lt;/a&gt; and &lt;a href="http://www.codeplex.com/xunit"&gt;xUnit.net&lt;/a&gt; supported
out of the box&lt;/strong&gt; 
&lt;/p&gt;
&lt;p&gt;
Pex now supports MSTest, NUnit, MbUnit and xUnit.net out of the box. Pex will automatically
detect which framework you are using by inspecting the assembly reference list, and
automatically save the generated tests decorated with the correct attributes for that
framework. 
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.10.40408.0NUnitMbUnitx.NETFandmore_14623/image_22.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.10.40408.0NUnitMbUnitx.NETFandmore_14623/image_thumb_10.png" width="604" height="351" /&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
The default test framework can also be specified through the global options (Tools
–&amp;gt; Options –&amp;gt; Pex –&amp;gt; enter the test framework name in TestFramework).
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.10.40408.0NUnitMbUnitx.NETFandmore_14623/image_16.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.10.40408.0NUnitMbUnitx.NETFandmore_14623/image_thumb_7.png" width="604" height="257" /&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
&amp;#160;
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Writing Parameterized Unit Tests in VisualBasic.NET&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
While the Pex white box analysis engine works at the MSIL level, Pex only emits C#
code for now. In previous releases, this limitation made it impossible to use Pex
parameterized unit tests from non-C# code. In this release, we have worked around
this problem by automatically saving the generated tests in a ‘satellite’ C# project.
&lt;/p&gt;
&lt;p&gt;
Let’s see this with an example. The screenshot below shows a single VisualBasic.NET
test project with a Pex parameterized unit test:
&lt;/p&gt;
&lt;p&gt;
&lt;a href="file:///C:\Users\jhalleux\AppData\Local\Temp\WindowsLiveWriter-429641856\supfiles659CA51\image3.png"&gt;&lt;b&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image002" border="0" alt="clip_image002" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.10.40408.0NUnitMbUnitx.NETFandmore_14623/clip_image002_0dd1c4a7-6732-42b8-b6fe-76f7499e6a66.gif" width="604" height="237" /&gt;&lt;/b&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
We can right-click in the HelloTest.Hello method and select “Run Pex Explorations”:
&lt;/p&gt;
&lt;p&gt;
&lt;a href="file:///C:\Users\jhalleux\AppData\Local\Temp\WindowsLiveWriter-429641856\supfiles659CA51\image7.png"&gt;&lt;b&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image004" border="0" alt="clip_image004" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.10.40408.0NUnitMbUnitx.NETFandmore_14623/clip_image004_31e0ecca-a518-4ec3-9ad4-3fef0bb58593.gif" width="305" height="91" /&gt;&lt;/b&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
At this point, Pex will start exploring the test in the background as usual. This
is where the new support comes in: When a generated test comes back to Visual Studio,
Pex will save it in a separate C# project automatically (after asking you where to
drop the new project):
&lt;/p&gt;
&lt;p&gt;
&lt;a href="file:///C:\Users\jhalleux\AppData\Local\Temp\WindowsLiveWriter-429641856\supfiles659CA51\image19.png"&gt;&lt;b&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image006" border="0" alt="clip_image006" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.10.40408.0NUnitMbUnitx.NETFandmore_14623/clip_image006_324456a0-a756-4cfe-b9cb-e7eb8a9db8a0.gif" width="604" height="391" /&gt;&lt;/b&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
The generated tests are now ready to be run just as any other unit tests!
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Writing Parameterized Unit Tests from F#&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
Similarly to VisualBasic.NET, we’ve made improvements in our infrastructure to enable
writing parameterized unit tests in &lt;strong&gt;F#&lt;/strong&gt;. Let’s see this with a familiar
example. We have a single F# library that has xUnit.net unit tests and reference Microsoft.Pex.Framework
(project Library2 below). In that project, we add a parameterized unit test (hello_test):
&lt;/p&gt;
&lt;p&gt;
&lt;a href="file:///C:\Users\jhalleux\AppData\Local\Temp\WindowsLiveWriter-429641856\supfiles659CA51\image32%5b1%5d.png"&gt;&lt;b&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image008" border="0" alt="clip_image008" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.10.40408.0NUnitMbUnitx.NETFandmore_14623/clip_image008_709d95c3-12c4-4805-b549-2982d5341032.gif" width="604" height="235" /&gt;&lt;/b&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
We can right-click on the &lt;strong&gt;test method name&lt;/strong&gt; and Pex will start the
exploration of that test in the background. Because of the limitations of the F# project
system, you &lt;strong&gt;absolutely&lt;/strong&gt; need to right-click on the&lt;strong&gt; method
name&lt;/strong&gt; in F# if you want contextual test selection to work. Because the project
is already referencing xunit.dll, Pex will also automatically detect that you are
using xUnit.net and use that framework. When the first test case comes back to VisualStudio,
Pex saves it in a separate C# project:
&lt;/p&gt;
&lt;p&gt;
&lt;a href="file:///C:\Users\jhalleux\AppData\Local\Temp\WindowsLiveWriter-429641856\supfiles659CA51\image39.png"&gt;&lt;b&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image010" border="0" alt="clip_image010" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.10.40408.0NUnitMbUnitx.NETFandmore_14623/clip_image010_69f5c0e9-4c03-4251-a79a-38c7ffbb3a19.gif" width="604" height="341" /&gt;&lt;/b&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
The tests are saved in the generated test project and ready to be run by your favorite
test runner!
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;PexObserve: Observing values, Asserting values&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
We’ve completely re-factored the way values can be logged on the table or saved as
assertions in the generated tests. The following example shows various ways to log
and assert values:
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.10.40408.0NUnitMbUnitx.NETFandmore_14623/image_8.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.10.40408.0NUnitMbUnitx.NETFandmore_14623/image_thumb_3.png" width="451" height="228" /&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
In the Observe method, we use the return value and out parameter output to automatically
log and assert those values. Additionally, we add “view input” on the fly to the parameter
table through the ValueForViewing method, and we add “check input” to be asserted
through the ValueAtEndOfTest method. After running Pex, we get the following results:
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.10.40408.0NUnitMbUnitx.NETFandmore_14623/image_10.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.10.40408.0NUnitMbUnitx.NETFandmore_14623/image_thumb_4.png" width="373" height="63" /&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
As expected, input, ‘view input’, output and result show up in the parameter table.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.10.40408.0NUnitMbUnitx.NETFandmore_14623/image_12.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.10.40408.0NUnitMbUnitx.NETFandmore_14623/image_thumb_5.png" width="599" height="266" /&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
In the generated test, we see assertions for the return value, out parameters and
other values passed through the ValueAtEndOfTest method.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;&lt;a href="http://research.microsoft.com/contracts"&gt;Code Contracts&lt;/a&gt; : Reproducible
generated tests&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
When Pex generates a unit test that relied on a runtime contract, Pex also adds a
check to the unit test which validates that the contracts have been injected into
the code by the contracts rewriter. If the code is not rewritten when re-executing
the unit test, it is marked as inconclusive. You will appreciate this behavior when
you run your unit tests both in Release and in Debug builds, which usually differ
in how contracts get injected.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="file:///C:\Users\jhalleux\AppData\Local\Temp\WindowsLiveWriter-429641856\supfiles659CA51\image43.png"&gt;&lt;b&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image012" border="0" alt="clip_image012" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.10.40408.0NUnitMbUnitx.NETFandmore_14623/clip_image012_32c70deb-1184-48c7-a1cd-16e1c19559d3.gif" width="604" height="177" /&gt;&lt;/b&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Code Contracts:&amp;#160; Automatic filtering of the contract violations&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
When Pex generates a test that violates a Code Contract pre-condition (i.e. Contract.Requires),
there are basically two scenarios: the precondition was on top of the stack and should
be considered as an expected exception; or it is a nested exception and should be
considered as a bug. Pex provides a default exception filtering that implements this
behavior.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;&lt;a href="http://research.microsoft.com/stubs" target="_blank"&gt;Stubs&lt;/a&gt;: simplified
syntax&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
We’ve considerably simplified the syntax of stubs by removing the ‘this’ parameter
from the stub delegate definition. Let’s illustrate this with a test that stubs the
‘ReadAllText’ method of a fictitious ‘IFileSystem’ interface.&amp;#160; 
&lt;/p&gt;
&lt;p&gt;
&lt;img src="http://research.microsoft.com/en-us/projects/stubs/stubexample.png" /&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;&lt;a href="http://research.microsoft.com/stubs" target="_blank"&gt;Stubs&lt;/a&gt;: generic
methods&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
The &lt;a href="http://research.microsoft.com/stubs" target="_blank"&gt;Stubs&lt;/a&gt; framework
now supports stubbing generic methods by providing particular instantiations of that
method. In the following example, the generic Bar&amp;lt;T&amp;gt; method is stubbed for the
particular Bar&amp;lt;int&amp;gt; instantiation:
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.10.40408.0NUnitMbUnitx.NETFandmore_14623/image_2.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.10.40408.0NUnitMbUnitx.NETFandmore_14623/image_thumb.png" width="435" height="119" /&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Stubs and Pex: Pex will choose the stubs behavior by default&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
We provide a new custom attribute, PexChooseAsStubFallbackBehaviorAttribute, that
hooks Pex choices to the Stub fallback behavior. To illustrate what this means, let’s
modify slightly the example above by removing the stub of ReadAllText:
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.10.40408.0NUnitMbUnitx.NETFandmore_14623/image_4.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.10.40408.0NUnitMbUnitx.NETFandmore_14623/image_thumb_1.png" width="455" height="216" /&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
If this test was to be run without the PexChooseAsStubFallbackBehavior attribute,
it would throw a StubNotImplementedException. However, with the PexChooseAsStubFallbackBehavior
attribute, the fallback behavior calls into PexChoose to ask Pex for a new string.
In this example in particular, on each call to ReadAllText, Pex will generate a new
string for the result. You can see this string as a new parameter to the parameterized
unit test. Therefore, when we run this test under Pex, we see different behavior happening,
including the “hello world” file:
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.10.40408.0NUnitMbUnitx.NETFandmore_14623/image_6.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/content/binary/WindowsLiveWriter/Pexv0.10.40408.0NUnitMbUnitx.NETFandmore_14623/image_thumb_2.png" width="584" height="90" /&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
Note that all the necessary attributes are added at the assembly level by the Pex
Wizard.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Miscellanous bug fixes and improvements&lt;/strong&gt;
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
[fixed] Dialogs do not render correctly under high DPI 
&lt;/li&gt;
&lt;li&gt;
When a generic parameterized unit tests does not have any generic argument instantiations,
Pex makes a guess for you. 
&lt;/li&gt;
&lt;li&gt;
When a test parameter is an interface or an abstract class, Pex now searches the known
assemblies for implementations and concrete classes. In particular, that means that
Pex will often automatically use the automatically generated Stubs implementations
for interfaces or abstract classes. 
&lt;/li&gt;
&lt;li&gt;
Static parameterized unit tests are supported (if static tests are supported by your
test framework) 
&lt;/li&gt;
&lt;li&gt;
Better solving of decimal and floating point constraints. We will report on the details
later. 
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
&lt;strong&gt;Breaking Changes&lt;/strong&gt;
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
The PexFactoryClassAttribute is no longer needed and has been removed. Now, Pex will
pick up object factory methods marked with the PexFactoryMethodAttribute from any &lt;i&gt;static
class&lt;/i&gt; in the test project containing the parameterized unit tests. If the generated
tests are stored in a separate project, that project is not searched. 
&lt;/li&gt;
&lt;li&gt;
The PexStore API has been renamed to PexObserve. 
&lt;/li&gt;
&lt;li&gt;
Pex is compatible with Code Contracts versions strictly newer than v1.1.20309.13.
Unfortunately, v1.1.20309.13 is the currently available version of Code &lt;a href="http://research.microsoft.com/contracts" target="_blank"&gt;Contracts&lt;/a&gt;.
The Code Contracts team is planning on a release soon. 
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
&amp;#160;
&lt;/p&gt;
&lt;p&gt;
Happy Pexing!
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=83fde01f-786e-456d-b1c4-c8dd52448e69" /&gt;</description>
      <comments>http://blog.dotnetwiki.org/CommentView,guid,83fde01f-786e-456d-b1c4-c8dd52448e69.aspx</comments>
      <category>Code Contracts</category>
      <category>MbUnit</category>
      <category>Pex</category>
      <category>RiSE</category>
      <category>Stubs</category>
      <category>Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.dotnetwiki.org/Trackback.aspx?guid=28bacbcd-71a1-4915-bfdc-cba598b25584</trackback:ping>
      <pingback:server>http://blog.dotnetwiki.org/pingback.aspx</pingback:server>
      <pingback:target>http://blog.dotnetwiki.org/PermaLink,guid,28bacbcd-71a1-4915-bfdc-cba598b25584.aspx</pingback:target>
      <dc:creator>Jonathan de Halleux</dc:creator>
      <wfw:comment>http://blog.dotnetwiki.org/CommentView,guid,28bacbcd-71a1-4915-bfdc-cba598b25584.aspx</wfw:comment>
      <wfw:commentRss>http://blog.dotnetwiki.org/SyndicationService.asmx/GetEntryCommentsRss?guid=28bacbcd-71a1-4915-bfdc-cba598b25584</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
 
</p>
        <p>
Ben presented a talk on <a href="http://research.microsoft.com/pex" target="_blank">Pex</a> a
couple months ago at <a href="http://www.developerday.co.uk/">DDD7</a>. The video
of the talk is now up on the web. Check it out!
</p>
        <p>
          <a href="http://blog.benhall.me.uk/2009/02/ddd7-session-video-microsoft-pex-future.html">
            <img style="border-bottom: 0px; border-left: 0px; display: inline; margin-left: 0px; border-top: 0px; margin-right: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/images/BenHallonPexDDD7VideoPosted_75FC/image.png" width="244" height="212" />
          </a>
        </p>
        <img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=28bacbcd-71a1-4915-bfdc-cba598b25584" />
      </body>
      <title>Ben Hall on Pex – DDD7 Video Posted</title>
      <guid isPermaLink="false">http://blog.dotnetwiki.org/PermaLink,guid,28bacbcd-71a1-4915-bfdc-cba598b25584.aspx</guid>
      <link>http://blog.dotnetwiki.org/2009/02/03/BenHallOnPexDDD7VideoPosted.aspx</link>
      <pubDate>Tue, 03 Feb 2009 16:38:53 GMT</pubDate>
      <description>&lt;p&gt;
&amp;#160;
&lt;/p&gt;
&lt;p&gt;
Ben presented a talk on &lt;a href="http://research.microsoft.com/pex" target="_blank"&gt;Pex&lt;/a&gt; a
couple months ago at &lt;a href="http://www.developerday.co.uk/"&gt;DDD7&lt;/a&gt;. The video
of the talk is now up on the web. Check it out!
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.benhall.me.uk/2009/02/ddd7-session-video-microsoft-pex-future.html"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; margin-left: 0px; border-top: 0px; margin-right: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/images/BenHallonPexDDD7VideoPosted_75FC/image.png" width="244" height="212" /&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=28bacbcd-71a1-4915-bfdc-cba598b25584" /&gt;</description>
      <comments>http://blog.dotnetwiki.org/CommentView,guid,28bacbcd-71a1-4915-bfdc-cba598b25584.aspx</comments>
      <category>Pex</category>
      <category>Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.dotnetwiki.org/Trackback.aspx?guid=e327a7c9-a2f6-43ce-b96f-57ec42b2c467</trackback:ping>
      <pingback:server>http://blog.dotnetwiki.org/pingback.aspx</pingback:server>
      <pingback:target>http://blog.dotnetwiki.org/PermaLink,guid,e327a7c9-a2f6-43ce-b96f-57ec42b2c467.aspx</pingback:target>
      <dc:creator>Jonathan de Halleux</dc:creator>
      <wfw:comment>http://blog.dotnetwiki.org/CommentView,guid,e327a7c9-a2f6-43ce-b96f-57ec42b2c467.aspx</wfw:comment>
      <wfw:commentRss>http://blog.dotnetwiki.org/SyndicationService.asmx/GetEntryCommentsRss?guid=e327a7c9-a2f6-43ce-b96f-57ec42b2c467</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <title>Named Formats Pex Testimonium</title>
      <guid isPermaLink="false">http://blog.dotnetwiki.org/PermaLink,guid,e327a7c9-a2f6-43ce-b96f-57ec42b2c467.aspx</guid>
      <link>http://blog.dotnetwiki.org/2009/01/16/NamedFormatsPexTestimonium.aspx</link>
      <pubDate>Fri, 16 Jan 2009 00:38:25 GMT</pubDate>
      <description>&lt;p&gt;
&lt;a target="_blank" href="http://www.haacked.com/"&gt;Phil Haacked&lt;/a&gt; has started an &lt;a target="_blank" href="http://www.haacked.com/archive/2009/01/14/named-formats-redux.aspx"&gt;interresting
discussion&lt;/a&gt; on the implementation of a &lt;strong&gt;named formatter &lt;/strong&gt;whose format
strings are of the form {name} instead of {0}. In fact, he started with 3 implementations
and his reader submitted 2 more! Since Phil was kind enough to package all the implementations
(and the unit tests) in a solution, I took the liberty to run &lt;a target="_blank" href="http://research.microsoft.com/pex"&gt;Pex&lt;/a&gt; on
them and see if there was any issue lurking out. The goal of this post is not really
to show that those implementation are correct or not, but give you an idea where Pex
could be applicable in your code.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;But wait, it’s already Unit Tested!&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
Indeed, Phil diligently wrote a unit test suite that covers many different use cases.
Does it mean you are done with testing? The named format syntax can be tricky since
may involve escaping curly braces… What is the named format syntax anyway? It’s pretty
obvious that it should let you write something like “{Name} is {Status}” instead of
“{0} is {1}” but that still pretty vague. In particular, what are the syntactic rules
for escaping curly braces (what’s the meaning {{}{}{{}}}, etc…) or is there even a
grammar describing named formats. 
&lt;/p&gt;
&lt;p&gt;
As it is often the case, there is no clear and definitive answer – at least I could
not find it –. In this post, I’ll show 2 techniques that can be used here out of the
many &lt;a href="http://research.microsoft.com/en-us/projects/pex/pexpatterns.pdf"&gt;patterns&lt;/a&gt; for
Pex which we documented.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Technique 1: Explore for runtime errors (&lt;/strong&gt;&lt;a target="_blank" href="http://research.microsoft.com/en-us/projects/pex/pexpatterns.pdf"&gt;pattern
2.12 – parameterized stub&lt;/a&gt;&lt;strong&gt;)&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
The most basic way of running Pex is to simply apply Pex on a method without adding
any assertions. At this point, you are looking for NullReferenceException, IndexOutOfRanceException
or other violations of lower level APIs. Although this kind of testing won’t help
you answer whether your code is correct, it may give you back instances where it is
wrong. For example, by passing any format and object into the format method in a parameterized
unit test; the code below is a parameterized unit test for which Pex can generate
relevant inputs:
&lt;/p&gt;
&lt;blockquote&gt; &lt;pre class="code"&gt;[&lt;span style="color: #2b91af"&gt;PexMethod&lt;/span&gt;] &lt;span style="color: blue"&gt;public
void &lt;/span&gt;HenriFormat(&lt;span style="color: blue"&gt;string &lt;/span&gt;format, &lt;span style="color: blue"&gt;object &lt;/span&gt;o)
{ format.HenriFormat(o); }&lt;/pre&gt;
&lt;/blockquote&gt; &lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
Note that it took a couple runs of Pex to figure the right set of assemblies to be
instrumented. Hopefully, this process is mostly automated and you simply have to apply
the changes that Pex suggests.
&lt;/p&gt;
&lt;p&gt;
The screenshot below shows the input generated by Pex. Intuitively, I would think
that FormatException is expected and part of properly rejecting invalid inputs. However,
there are ArgumentOutOfRanceExceptions triggered inside of the DataBinder code that
probably should intercepted earlier. If this implementation uses the DataBinder code,
it should make sure that it only gets acceptable values. Whether those issues are
worth fixing is a tricky question: maybe the programmer intended to let this behavior
happen.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.dotnetwiki.org/images/NamedFormatsPexus_D1D0/image.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/images/NamedFormatsPexus_D1D0/image_thumb.png" width="798" height="451" /&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Technique 2: Compare different implementations (&lt;a target="_blank" href="http://research.microsoft.com/en-us/projects/pex/pexpatterns.pdf"&gt;pattern
2.7 – same observable behavior&lt;/a&gt;)&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
A second approach is to compare the output of the different implementations. Since
all the implementations implement the same (underspecified) named format specification,
their behavior should match with respect to any input. This property makes it really
easy to write really powerful &lt;a target="_blank" href="http://research.microsoft.com/pex"&gt;parameterized
unit tests&lt;/a&gt;. For example, the following test asserts that the Haacked and Hanselman
implementation have the same observable behavior, i.e. return the same string or throw
the same exception type (PexAssert.AreBehaviorEqual takes care of asserting this):
&lt;/p&gt;
&lt;blockquote&gt; &lt;pre class="code"&gt;[&lt;span style="color: #2b91af"&gt;PexMethod&lt;/span&gt;] &lt;span style="color: blue"&gt;public
void &lt;/span&gt;HaackVsHansel(&lt;span style="color: blue"&gt;string &lt;/span&gt;format, &lt;span style="color: blue"&gt;object &lt;/span&gt;o)
{ &lt;span style="color: #2b91af"&gt;PexAssert&lt;/span&gt;.AreBehaviorsEqual( () =&gt; format.HaackFormat(o),
() =&gt; format.HanselFormat(o) ); }&lt;/pre&gt;
&lt;/blockquote&gt; &lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
Again, this kind of testing will not help to answer if the code is correct but it
will give you instance where the different implementations behave differently, which
means one of the two (or even both) have a bug. This is a great technique to test
a new implementation against an old (fully tested) implementation which can be used
as oracle. We run the test and Pex comes back with this list of test cases. For example,
the format string “\0\0{{{{“ leads to different output for both implementation. From
the different outputs, “\0\0{{“ vs “\0\0{{{{“, it seems that curly braces are escaped
differently. If I wanted to dig deeper, I could also simply debug the generated test
case.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.dotnetwiki.org/images/NamedFormatsPexus_D1D0/image_3.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/images/NamedFormatsPexus_D1D0/image_thumb_3.png" width="752" height="448" /&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Comparing All Implementations&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
Now that we’ve seen that Phil and Scott were not agreeing, could we apply this to
the other formatters. I quickly set up a &lt;a target="_blank" href="http://msdn.microsoft.com/en-us/library/bb126445.aspx"&gt;T4
template&lt;/a&gt; to generate the code for all the parameterized unit tests between each
formatters. Note that order matters for Pex: calling A then B might lead to a different
test suite compared to B then A, just because Pex explores the code in different orders.
&lt;/p&gt;
&lt;blockquote&gt; &lt;pre class="code"&gt;&lt;#@ template language="C#" #&gt;

&lt;# string[] formatters = new string[] {
        "Hansel",
        "Henri",
        "James",
        "Oskar",
        "Haack"
    };
#&gt;
using System; using Microsoft.Pex.Framework; using Microsoft.Pex.Framework.Validation;
using Microsoft.VisualStudio.TestTools.UnitTesting; using UnitTests; using StringLib;
namespace UnitTests { [TestClass] [PexClass(MaxConstraintSolverTime = 2, MaxRunsWithoutNewTests
= 200)] public partial class StringFormatterTestsTest { 
&lt;# foreach(string left in formatters) { 
           foreach(string right in formatters) {
               if (left == right) continue;    
        #&gt;&lt;#= left #&gt;&lt;#= right #&gt;&lt;#= left #&gt;&lt;#= right #&gt;
[PexMethod] public void Vs(string format, object o) { PexAssert.AreBehaviorsEqual(
() =&gt; format.Format(o), () =&gt; format.Format(o) ); } 
&lt;#
           } 
        } #&gt;
} }
&lt;/pre&gt;
&lt;/blockquote&gt; &lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
The answer that Pex returns is interresting and scary: &lt;strong&gt;none of the implementation
behaviors match. &lt;/strong&gt;This is somewhat not surprising since they all use radically
different approaches to solve this problem: regex vs string api etc…
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Try it for yourself.&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
The &lt;a target="_blank" href="http://research.microsoft.com/en-us/projects/pex/namedstringformatsolution.zip"&gt;full
modified source is available for download here&lt;/a&gt;. You will need to install Pex from &lt;a href="http://research.microsoft.com/pex/downloads.aspx"&gt;http://research.microsoft.com/pex/downloads.aspx&lt;/a&gt; to
re-execute the parameterized unit tests.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=e327a7c9-a2f6-43ce-b96f-57ec42b2c467" /&gt;</description>
      <comments>http://blog.dotnetwiki.org/CommentView,guid,e327a7c9-a2f6-43ce-b96f-57ec42b2c467.aspx</comments>
      <category>Pex</category>
      <category>Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.dotnetwiki.org/Trackback.aspx?guid=1969e7c2-fd47-43cd-83e9-4f11889dd99d</trackback:ping>
      <pingback:server>http://blog.dotnetwiki.org/pingback.aspx</pingback:server>
      <pingback:target>http://blog.dotnetwiki.org/PermaLink,guid,1969e7c2-fd47-43cd-83e9-4f11889dd99d.aspx</pingback:target>
      <dc:creator>Jonathan de Halleux</dc:creator>
      <wfw:comment>http://blog.dotnetwiki.org/CommentView,guid,1969e7c2-fd47-43cd-83e9-4f11889dd99d.aspx</wfw:comment>
      <wfw:commentRss>http://blog.dotnetwiki.org/SyndicationService.asmx/GetEntryCommentsRss?guid=1969e7c2-fd47-43cd-83e9-4f11889dd99d</wfw:commentRss>
      <slash:comments>2</slash:comments>
      <title>Introduction to the Stubs Framework</title>
      <guid isPermaLink="false">http://blog.dotnetwiki.org/PermaLink,guid,1969e7c2-fd47-43cd-83e9-4f11889dd99d.aspx</guid>
      <link>http://blog.dotnetwiki.org/2009/01/10/IntroductionToTheStubsFramework.aspx</link>
      <pubDate>Sat, 10 Jan 2009 15:41:32 GMT</pubDate>
      <description>&lt;p&gt;
&lt;a target="_blank" href="http://blog.dotnetwiki.org/PexGetsAStubsFrameworkISaidStubsNotMocks.aspx"&gt;Stubs&lt;/a&gt; is
a framework for generating stubs (that &lt;a target="_blank" href="http://blog.dotnetwiki.org/PexGetsAStubsFrameworkISaidStubsNotMocks.aspx"&gt;we
announced earlier with Pex&lt;/a&gt;). The framework generates stubs (not mocks) that are 
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
lightweight, i.e. relying on delegates only, 
&lt;/li&gt;
&lt;li&gt;
strongly typed, i.e. no magic strings – refactoring friendly, 
&lt;/li&gt;
&lt;li&gt;
source code generated, i.e. no dynamic code or expression trees, 
&lt;/li&gt;
&lt;li&gt;
great debugging experience, i.e. break and step through stubs, 
&lt;/li&gt;
&lt;li&gt;
minimalistic API (there is almost none), 
&lt;/li&gt;
&lt;li&gt;
friendly for &lt;a target="_blank" href="http://research.microsoft.com/pex"&gt;Pex&lt;/a&gt;! 
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
More details about Stubs are also available in the &lt;a target="_blank" href="http://research.microsoft.com/en-us/projects/pex/stubs.pdf"&gt;stubs
reference document&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Quick Start&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
Let’s start from a simple interface &lt;font face="Courier New"&gt;IFileSystem &lt;/font&gt;for
which we want to write stubs:
&lt;/p&gt;
&lt;blockquote&gt; &lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public interface &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;IFileSystem &lt;/span&gt;{ &lt;span style="color: blue;"&gt;void &lt;/span&gt;WriteAllText(&lt;span style="color: blue;"&gt;string &lt;/span&gt;fileName, &lt;span style="color: blue;"&gt;string &lt;/span&gt;content); &lt;span style="color: blue;"&gt;string &lt;/span&gt;ReadAllText(&lt;span style="color: blue;"&gt;string &lt;/span&gt;fileName);
}&lt;/pre&gt;
&lt;/blockquote&gt; &lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
&lt;strong&gt;Stubs&lt;/strong&gt; relies solely on delegates to attach behaviors, and leverages
the lambda syntax from C# 3.0 to accomplish pretty much anything:
&lt;/p&gt;
&lt;blockquote&gt; &lt;pre class="code"&gt;&lt;span style="color: green;"&gt;// SFileSystem was generated
by Stubs and stubs IFileSystem &lt;/span&gt;&lt;span style="color: blue;"&gt;var &lt;/span&gt;stub = &lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;SFileSystem&lt;/span&gt;(); &lt;span style="color: green;"&gt;//
always returns “...” &lt;/span&gt;stub.ReadAllText = (me, file) =&gt; &lt;span style="color: rgb(163, 21, 21);"&gt;"..."&lt;/span&gt;; &lt;span style="color: green;"&gt;//
expectations: checks file = “foo.txt” &lt;/span&gt;stub.ReadAllText = (me, file) =&gt; { &lt;span style="color: rgb(43, 145, 175);"&gt;Assert&lt;/span&gt;.AreEqual(&lt;span style="color: rgb(163, 21, 21);"&gt;"foo.txt"&lt;/span&gt;,
file); &lt;span style="color: blue;"&gt;return &lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;"..."&lt;/span&gt;;
}; &lt;span style="color: green;"&gt;// storing side effects in closures: written saves
content &lt;/span&gt;&lt;span style="color: blue;"&gt;string &lt;/span&gt;written = &lt;span style="color: blue;"&gt;null&lt;/span&gt;;
stub.WriteAllText = (me, file, content) =&gt; written = content; &lt;span style="color: green;"&gt;//
hey, we can do whatever we want! &lt;/span&gt;stub.ReadAllText = (me, file) =&gt; { &lt;span style="color: blue;"&gt;if &lt;/span&gt;(file
== &lt;span style="color: blue;"&gt;null&lt;/span&gt;) &lt;span style="color: blue;"&gt;throw new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;ArgumentNullException&lt;/span&gt;(); &lt;span style="color: blue;"&gt;return &lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;"..."&lt;/span&gt;;
};&lt;/pre&gt;
&lt;pre class="code"&gt;&lt;span style="color: green;"&gt;// downcast to the interface to use
it &lt;/span&gt;IFileSystem fs = stubs;&lt;/pre&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
Anything is possible… as long as C# (or your favorite language) allows it.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Anatomy of a Stub&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
Each stubbed method has an associated field delegate that can be set freely (e.g. &lt;font face="Courier New"&gt;WriteAllText &lt;/font&gt;and &lt;font face="Courier New"&gt;ReadAllText&lt;/font&gt;).
If this delegate field is set, it will used when the method is called; otherwize a
default action occurs. Let’s see this with a simplified stub of the &lt;font face="Courier New"&gt;IFileSystem &lt;/font&gt;interface, &lt;font face="Courier New"&gt;SFileSystem&lt;/font&gt;,
which shows how the &lt;font face="Courier New"&gt;IFileSystem.WriteAllText&lt;/font&gt; is implemented:
&lt;/p&gt;
&lt;blockquote&gt; &lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;class &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;SFileSystem &lt;/span&gt;: &lt;span style="color: rgb(43, 145, 175);"&gt;StubBase&lt;/span&gt;&lt;&lt;span style="color: rgb(43, 145, 175);"&gt;SFileSystem&gt;&gt;
, &lt;span style="color: rgb(43, 145, 175);"&gt;IFileSystem &lt;/span&gt;{ &lt;span style="color: blue;"&gt;public &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Action&lt;/span&gt;&lt;&lt;span style="color: rgb(43, 145, 175);"&gt;SFileSystem&gt;, &lt;span style="color: blue;"&gt;string&lt;/span&gt;, &lt;span style="color: blue;"&gt;string&lt;/span&gt;&gt;
WriteAllText; // attach here &lt;span style="color: blue;"&gt;void &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;IFileSystem&lt;/span&gt;.WriteAllText(&lt;span style="color: blue;"&gt;string &lt;/span&gt;fileName, &lt;span style="color: blue;"&gt;string &lt;/span&gt;content)
{ &lt;span style="color: blue;"&gt;var &lt;/span&gt;stub = &lt;span style="color: blue;"&gt;this&lt;/span&gt;.WriteAllText; &lt;span style="color: blue;"&gt;if &lt;/span&gt;(stub
!= &lt;span style="color: blue;"&gt;null&lt;/span&gt;) stub(&lt;span style="color: blue;"&gt;this&lt;/span&gt;,
fileName, content); // your code executed here &lt;span style="color: blue;"&gt;else this&lt;/span&gt;.DefaultStub.VoidResult(&lt;span style="color: blue;"&gt;this&lt;/span&gt;);
} }&lt;/pre&gt;
&lt;/blockquote&gt; &lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
The actual generated code may look more complicated because it contains custom attributes
for debugging, comments and globally qualified types to avoid name clashing:
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.dotnetwiki.org/images/StubspartIGettingStarted_F368/image.png"&gt;&lt;img style="border-width: 0px; display: inline;" title="image" alt="image" src="http://blog.dotnetwiki.org/images/StubspartIGettingStarted_F368/image_thumb.png" width="604" border="0" height="387"&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Code generation: How does it work?&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Stubs&lt;/strong&gt; is a single file generator that pre-generates stubs to source
code. &lt;strong&gt;Stubs&lt;/strong&gt; also monitors the build and regenerates the source code
when a change occurs.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.dotnetwiki.org/images/StubspartIGettingStarted_F368/image_3.png"&gt;&lt;img style="border-width: 0px; display: inline;" title="image" alt="image" src="http://blog.dotnetwiki.org/images/StubspartIGettingStarted_F368/image_thumb_3.png" width="243" border="0" height="277"&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
The stub generation is configured through an XML file (&lt;strong&gt;.stubx&lt;/strong&gt; file)
that contains which code and how it should be generated. The generated code is saved
in a ‘Designer’ file, similarly to other code generation tools (type dataset etc…). 
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.dotnetwiki.org/images/StubspartIGettingStarted_F368/image_4.png"&gt;&lt;img style="border-width: 0px; display: inline;" title="image" alt="image" src="http://blog.dotnetwiki.org/images/StubspartIGettingStarted_F368/image_thumb_4.png" width="476" border="0" height="71"&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Great debugging experience&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
A cool side effect of simply using delegates: you can step through and debug your
stubs! This is usually not the case with mock framework using dynamic code. Below,
you can see that one can set breakpoints in the body of a stub and debug as usual.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.dotnetwiki.org/images/StubspartIGettingStarted_F368/image_5.png"&gt;&lt;img style="border-width: 0px; display: inline;" title="image" alt="image" src="http://blog.dotnetwiki.org/images/StubspartIGettingStarted_F368/image_thumb_5.png" width="687" border="0" height="323"&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Where do I get it?&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
The Stubs framework comes with Pex but you can use it in any unit testing activity.
It provides a simple lightweight alternative to define stub for testing. Pex can be
downloaded from &lt;a href="http://research.microsoft.com/pex"&gt;http://research.microsoft.com/pex&lt;/a&gt; .
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=1969e7c2-fd47-43cd-83e9-4f11889dd99d" /&gt;</description>
      <comments>http://blog.dotnetwiki.org/CommentView,guid,1969e7c2-fd47-43cd-83e9-4f11889dd99d.aspx</comments>
      <category>Pex</category>
      <category>Stubs</category>
      <category>Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.dotnetwiki.org/Trackback.aspx?guid=e52cfaba-ca74-4841-8067-9fd229873685</trackback:ping>
      <pingback:server>http://blog.dotnetwiki.org/pingback.aspx</pingback:server>
      <pingback:target>http://blog.dotnetwiki.org/PermaLink,guid,e52cfaba-ca74-4841-8067-9fd229873685.aspx</pingback:target>
      <dc:creator>Jonathan de Halleux</dc:creator>
      <wfw:comment>http://blog.dotnetwiki.org/CommentView,guid,e52cfaba-ca74-4841-8067-9fd229873685.aspx</wfw:comment>
      <wfw:commentRss>http://blog.dotnetwiki.org/SyndicationService.asmx/GetEntryCommentsRss?guid=e52cfaba-ca74-4841-8067-9fd229873685</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
The <a target="_blank" href="http://www.nist.gov/dads/HTML/kthShortestPath.html"><strong>k-shortest
paths</strong></a> computes the <em>k</em> first shortest path between two vertices
in a directed graph. If you put this in the context of route planning, it gives you <em>k</em> alternative
routes in case the shortest path is blocked by snow :) While the <a target="_blank" href="http://en.wikipedia.org/wiki/Shortest_path_problem">single
source shortest</a> path is well-known and implemented in many languages, there are
not many implementations available for this problem, although it has been extensively
studied. After looking around, I stumbled on a <a target="_blank" href="http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.54.3286">nice
article comparing various approaches</a>. The authors pointed out <a target="_blank" href="http://citeseerx.ist.psu.edu/showciting?cid=463224">an
algorithm from 1959 from Hoffman and Pavley</a> that solved this problem (there are
actually many others). This algorithm looked like a good fit:
</p>
        <ul>
          <li>
it requires a single call to a single-source shortest path algorithm. Other approaches
will require as much as <em>kn</em> calls to a shortest path algorithm.</li>
          <li>
it sounded simple and did not require new specialized data structures.</li>
        </ul>
        <p>
          <strong>I want to try it!</strong>
        </p>
        <p align="left">
The algorithm is available in <a target="_blank" href="http://www.codeplex.com/quickgraph">QuickGraph</a> 3.1.40104.00
and up. You can take a look at it at <a title="http://www.codeplex.com/quickgraph/SourceControl/changeset/view/29858#377982" href="http://www.codeplex.com/quickgraph/SourceControl/changeset/view/29858#377982">http://www.codeplex.com/quickgraph/SourceControl/changeset/view/29858#377982</a>.
To use it on a <em>BidirectionalGraph</em>, 
</p>
        <blockquote>
          <p>
IBidirectionalGraph&lt;TVertex, TEdge&gt; g = …;<br />
foreach(IEnumerable&lt;TEdge&gt; path in g.RankedShortestPathHoffmanPavley(weights,
source, goal, 4))<br />
     …
</p>
        </blockquote>
        <p>
          <strong>A glimpse at the Hoffmann-Pavley algorithm</strong>
        </p>
        <p>
The algorithm works in 2 phases. 
</p>
        <p>
On the first phase, we build a minimum ‘successor’ tree towards the goal vertex. This
tree can be used to build a shortest path from any (reachable) vertex to the goal
vertex. To build this tree, we can simply apply the Dijkstra shortest path algorithm
on the reversed graph. This can be done in a couple lines with QuickGraph.
</p>
        <p>
As for many other <em>k</em>-shortest path algorithm, phase works by building deviation
path and picking the best one. In the case of the Hoffman-Pavley algorithm, it works
as follows: pick the latest shortest path, for each vertex of this path, build a deviation
path (more later) for each out-edge and add it to a priority queue. Then start again:
</p>
        <blockquote>
          <p>
var queue = new PriorityQueue&lt;Path&gt;();<br />
queue.Enqueue(shortestpath);<br />
while(queue.Count &gt; 0) {<br />
    var path = queue.Dequeue();<br />
    foreach(var vertex in path)<br />
        foreach(var edge in graph.OutEdges(vertex))<br />
             queue.Enqueue(CreateDeviation(path,
vertex, edge));<br />
}
</p>
        </blockquote>
        <p>
A deviation path is composed of three parts: 
</p>
        <ol>
          <li>
the initial part of the ‘seeding’ path, i.e. the edges before the deviation edge,</li>
          <li>
the deviation edge</li>
          <li>
the remaining shortest path to the goal. When we build the deviation path, we also
compute it’s weight. A nice property of the deviation paths is that they can be ‘built’
when needed. This saves a lot of space and computation as most deviation paths will
probably not end up in the winning set of paths – instead of storing a full path,
we store a path index and an edge index.</li>
        </ol>
        <p>
That’s it! The details contain more code to deal with self-edges and loops but the
main idea is there. This is definitely a very elegant algorithm!
</p>
        <p>
          <strong>What about testing!</strong>
        </p>
        <p>
Algorithm authors usually illustrate their approach with an example. This is a good
way to get started on a small graph example and ensure that the algorithm works as
the original author expected. This is the kind of unit test I get started with.
</p>
        <p>
The next step is to apply the algorithm to a large number of graph instances. Unfortunately,
I do not have any other k-shortest path algorithm, so the oracle is harder to build
here. Nevertheless, the result of the algorithm, i.e. the shortest path collection,
has a couple of properties that should always be true:
</p>
        <ul>
          <li>
the algorithm produces loopless paths,</li>
          <li>
path <em>k</em> is lower or equal to path <em>k</em> + 1.</li>
        </ul>
        <p>
The problem with this test is that it does not guarantee that some shortest path have
been missed. At this point, I’m a bit puzzled on how to test that.
</p>
        <img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=e52cfaba-ca74-4841-8067-9fd229873685" />
      </body>
      <title>A K-Shortest Path implementation in .Net</title>
      <guid isPermaLink="false">http://blog.dotnetwiki.org/PermaLink,guid,e52cfaba-ca74-4841-8067-9fd229873685.aspx</guid>
      <link>http://blog.dotnetwiki.org/2009/01/05/AKShortestPathImplementationInNet.aspx</link>
      <pubDate>Mon, 05 Jan 2009 07:05:14 GMT</pubDate>
      <description>&lt;p&gt;
The &lt;a target="_blank" href="http://www.nist.gov/dads/HTML/kthShortestPath.html"&gt;&lt;strong&gt;k-shortest
paths&lt;/strong&gt;&lt;/a&gt; computes the &lt;em&gt;k&lt;/em&gt; first shortest path between two vertices
in a directed graph. If you put this in the context of route planning, it gives you &lt;em&gt;k&lt;/em&gt; alternative
routes in case the shortest path is blocked by snow :) While the &lt;a target="_blank" href="http://en.wikipedia.org/wiki/Shortest_path_problem"&gt;single
source shortest&lt;/a&gt; path is well-known and implemented in many languages, there are
not many implementations available for this problem, although it has been extensively
studied. After looking around, I stumbled on a &lt;a target="_blank" href="http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.54.3286"&gt;nice
article comparing various approaches&lt;/a&gt;. The authors pointed out &lt;a target="_blank" href="http://citeseerx.ist.psu.edu/showciting?cid=463224"&gt;an
algorithm from 1959 from Hoffman and Pavley&lt;/a&gt; that solved this problem (there are
actually many others). This algorithm looked like a good fit:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
it requires a single call to a single-source shortest path algorithm. Other approaches
will require as much as &lt;em&gt;kn&lt;/em&gt; calls to a shortest path algorithm.&lt;/li&gt;
&lt;li&gt;
it sounded simple and did not require new specialized data structures.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
&lt;strong&gt;I want to try it!&lt;/strong&gt;
&lt;/p&gt;
&lt;p align="left"&gt;
The algorithm is available in &lt;a target="_blank" href="http://www.codeplex.com/quickgraph"&gt;QuickGraph&lt;/a&gt; 3.1.40104.00
and up. You can take a look at it at &lt;a title="http://www.codeplex.com/quickgraph/SourceControl/changeset/view/29858#377982" href="http://www.codeplex.com/quickgraph/SourceControl/changeset/view/29858#377982"&gt;http://www.codeplex.com/quickgraph/SourceControl/changeset/view/29858#377982&lt;/a&gt;.
To use it on a &lt;em&gt;BidirectionalGraph&lt;/em&gt;, 
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
IBidirectionalGraph&amp;lt;TVertex, TEdge&amp;gt; g = …;&lt;br&gt;
foreach(IEnumerable&amp;lt;TEdge&amp;gt; path in g.RankedShortestPathHoffmanPavley(weights,
source, goal, 4))&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; …
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
&lt;strong&gt;A glimpse at the Hoffmann-Pavley algorithm&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
The algorithm works in 2 phases. 
&lt;/p&gt;
&lt;p&gt;
On the first phase, we build a minimum ‘successor’ tree towards the goal vertex. This
tree can be used to build a shortest path from any (reachable) vertex to the goal
vertex. To build this tree, we can simply apply the Dijkstra shortest path algorithm
on the reversed graph. This can be done in a couple lines with QuickGraph.
&lt;/p&gt;
&lt;p&gt;
As for many other &lt;em&gt;k&lt;/em&gt;-shortest path algorithm, phase works by building deviation
path and picking the best one. In the case of the Hoffman-Pavley algorithm, it works
as follows: pick the latest shortest path, for each vertex of this path, build a deviation
path (more later) for each out-edge and add it to a priority queue. Then start again:
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
var queue = new PriorityQueue&amp;lt;Path&amp;gt;();&lt;br&gt;
queue.Enqueue(shortestpath);&lt;br&gt;
while(queue.Count &amp;gt; 0) {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; var path = queue.Dequeue();&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; foreach(var vertex in path)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; foreach(var edge in graph.OutEdges(vertex))&lt;br&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; queue.Enqueue(CreateDeviation(path,
vertex, edge));&lt;br&gt;
}
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
A deviation path is composed of three parts: 
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
the initial part of the ‘seeding’ path, i.e. the edges before the deviation edge,&lt;/li&gt;
&lt;li&gt;
the deviation edge&lt;/li&gt;
&lt;li&gt;
the remaining shortest path to the goal. When we build the deviation path, we also
compute it’s weight. A nice property of the deviation paths is that they can be ‘built’
when needed. This saves a lot of space and computation as most deviation paths will
probably not end up in the winning set of paths – instead of storing a full path,
we store a path index and an edge index.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
That’s it! The details contain more code to deal with self-edges and loops but the
main idea is there. This is definitely a very elegant algorithm!
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;What about testing!&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
Algorithm authors usually illustrate their approach with an example. This is a good
way to get started on a small graph example and ensure that the algorithm works as
the original author expected. This is the kind of unit test I get started with.
&lt;/p&gt;
&lt;p&gt;
The next step is to apply the algorithm to a large number of graph instances. Unfortunately,
I do not have any other k-shortest path algorithm, so the oracle is harder to build
here. Nevertheless, the result of the algorithm, i.e. the shortest path collection,
has a couple of properties that should always be true:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
the algorithm produces loopless paths,&lt;/li&gt;
&lt;li&gt;
path &lt;em&gt;k&lt;/em&gt; is lower or equal to path &lt;em&gt;k&lt;/em&gt; + 1.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
The problem with this test is that it does not guarantee that some shortest path have
been missed. At this point, I’m a bit puzzled on how to test that.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=e52cfaba-ca74-4841-8067-9fd229873685" /&gt;</description>
      <comments>http://blog.dotnetwiki.org/CommentView,guid,e52cfaba-ca74-4841-8067-9fd229873685.aspx</comments>
      <category>Fun with graphs</category>
      <category>QuickGraph</category>
      <category>Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.dotnetwiki.org/Trackback.aspx?guid=32bd1002-4f51-4702-a6e3-7a8b6c2bde0d</trackback:ping>
      <pingback:server>http://blog.dotnetwiki.org/pingback.aspx</pingback:server>
      <pingback:target>http://blog.dotnetwiki.org/PermaLink,guid,32bd1002-4f51-4702-a6e3-7a8b6c2bde0d.aspx</pingback:target>
      <dc:creator>Jonathan de Halleux</dc:creator>
      <wfw:comment>http://blog.dotnetwiki.org/CommentView,guid,32bd1002-4f51-4702-a6e3-7a8b6c2bde0d.aspx</wfw:comment>
      <wfw:commentRss>http://blog.dotnetwiki.org/SyndicationService.asmx/GetEntryCommentsRss?guid=32bd1002-4f51-4702-a6e3-7a8b6c2bde0d</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <blockquote>
          <p>
When you implement an algorithm that computes an optimal solution (i.e. minimum/maximum
with respect to a cost function), how do you test that the solution is actually optimal? 
</p>
        </blockquote>
        <p>
This is the kind of question I face when implementing algorithms in <a target="_blank" href="http://www.codeplex.com/quickgraph">QuickGraph</a>.
For example, I was recently looking at the <a target="_blank" href="http://en.wikipedia.org/wiki/Minimum_spanning_tree">minimum
spanning tree of a graph</a> (MST)? While checking that the result tree is a spanning
tree is easy, checking that it is <em>minimum</em> is not obvious: nobody has written <em>Assert.IsMinimum</em> yet
:). Here are a couple techniques that I found useful along the way: 
</p>
        <p>
          <strong>Input-Output table</strong>
        </p>
        <p>
The most obvious approach is to pre-compute the result for a number of problems and
assert the solution of the algorithm matches. In this MST case, use a small set of
graphs for which the MST is well known, and check that the computed MST has the correct
weight. This approach will take you only so far and requires a lot of manual work
since you need to solve the problem (or find a known solution) for a number of representative
cases.
</p>
        <p>
          <strong>Solution Perturbation</strong>
        </p>
        <p>
If the algorithm computes a solution that is minimal with respect to a cost function,
one can try to perturbate the solution to see if there’s a smaller one. If so, it
clearly violates the fact that the solution should be minimal, thus you just found
a bug. In the case of MST, this would mean randomly picking edges that are not in
the minimum spanning tree, swapping them and evaluate if the result tree is smaller.
</p>
        <p>
This is kind of approach is actually used in optimization where the search space might
have local minima (see <a target="_blank" href="http://en.wikipedia.org/wiki/Simulated_annealing">simulated
annealing</a>).
</p>
        <p>
          <strong>Multiple Implementations</strong>
        </p>
        <p>
A nice thing about graph problems is that there are usually many (vastly) different
ways to solve them. If multiple implementations are available, we can simply compare
the result of each algorithm against each other and make sure that they match each
other. Each algorithm might have bugs but it is unlikely that they share common ones.
Since we have now a good oracle, we can apply this approach that a large number of
inputs to increase our coverage.
</p>
        <p>
In the MST case, two popular algorithms are <a target="_blank" href="http://en.wikipedia.org/wiki/Prim's_algorithm">Prim’s</a> and <a target="_blank" href="http://en.wikipedia.org/wiki/Kruskal's_algorithm">Kruskal’s</a>.
Those are 2 different approaches: Prim is built on top of <a target="_blank" href="http://en.wikipedia.org/wiki/Dijkstra's_algorithm">Dijkstra
single source shortest path</a>, while Kruskal is built on top of the <a target="_blank" href="http://blog.dotnetwiki.org/WritingADisjointSetInCWithTheHelpOfCodeContractsAndPex.aspx">disjoint-set</a> data
structure. By carefully picking the weights of the edges, we can assert different
things:
</p>
        <ul>
          <li>
if the edge weights are all equals, any spanning tree is minimal. So we can compare
the result to a depth-first-search algorithm (which can easily compute a spanning
tree). 
</li>
          <li>
if some edge weights are different, there may be many minimum spanning tree. In this
case, we can still assert that weight of the tree is minimum. 
</li>
          <li>
if all the edge weights are different, then the MST is unique. This fact can be used
to precisely pinpoint the differences in solution during testing.</li>
        </ul>
        <p>
There is a corner case that needs to be checked: if all algorithm are no-op, i.e.
they don’t do anything. There solution will always match!
</p>
        <p>
Happy New Year!
</p>
        <img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=32bd1002-4f51-4702-a6e3-7a8b6c2bde0d" />
      </body>
      <title>Testing Graph Algorithms With Optimal Solutions</title>
      <guid isPermaLink="false">http://blog.dotnetwiki.org/PermaLink,guid,32bd1002-4f51-4702-a6e3-7a8b6c2bde0d.aspx</guid>
      <link>http://blog.dotnetwiki.org/2009/01/01/TestingGraphAlgorithmsWithOptimalSolutions.aspx</link>
      <pubDate>Thu, 01 Jan 2009 17:03:14 GMT</pubDate>
      <description>&lt;blockquote&gt; 
&lt;p&gt;
When you implement an algorithm that computes an optimal solution (i.e. minimum/maximum
with respect to a cost function), how do you test that the solution is actually optimal? 
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
This is the kind of question I face when implementing algorithms in &lt;a target="_blank" href="http://www.codeplex.com/quickgraph"&gt;QuickGraph&lt;/a&gt;.
For example, I was recently looking at the &lt;a target="_blank" href="http://en.wikipedia.org/wiki/Minimum_spanning_tree"&gt;minimum
spanning tree of a graph&lt;/a&gt; (MST)? While checking that the result tree is a spanning
tree is easy, checking that it is &lt;em&gt;minimum&lt;/em&gt; is not obvious: nobody has written &lt;em&gt;Assert.IsMinimum&lt;/em&gt; yet
:). Here are a couple techniques that I found useful along the way: 
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Input-Output table&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
The most obvious approach is to pre-compute the result for a number of problems and
assert the solution of the algorithm matches. In this MST case, use a small set of
graphs for which the MST is well known, and check that the computed MST has the correct
weight. This approach will take you only so far and requires a lot of manual work
since you need to solve the problem (or find a known solution) for a number of representative
cases.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Solution Perturbation&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
If the algorithm computes a solution that is minimal with respect to a cost function,
one can try to perturbate the solution to see if there’s a smaller one. If so, it
clearly violates the fact that the solution should be minimal, thus you just found
a bug. In the case of MST, this would mean randomly picking edges that are not in
the minimum spanning tree, swapping them and evaluate if the result tree is smaller.
&lt;/p&gt;
&lt;p&gt;
This is kind of approach is actually used in optimization where the search space might
have local minima (see &lt;a target="_blank" href="http://en.wikipedia.org/wiki/Simulated_annealing"&gt;simulated
annealing&lt;/a&gt;).
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Multiple Implementations&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
A nice thing about graph problems is that there are usually many (vastly) different
ways to solve them. If multiple implementations are available, we can simply compare
the result of each algorithm against each other and make sure that they match each
other. Each algorithm might have bugs but it is unlikely that they share common ones.
Since we have now a good oracle, we can apply this approach that a large number of
inputs to increase our coverage.
&lt;/p&gt;
&lt;p&gt;
In the MST case, two popular algorithms are &lt;a target="_blank" href="http://en.wikipedia.org/wiki/Prim's_algorithm"&gt;Prim’s&lt;/a&gt; and &lt;a target="_blank" href="http://en.wikipedia.org/wiki/Kruskal's_algorithm"&gt;Kruskal’s&lt;/a&gt;.
Those are 2 different approaches: Prim is built on top of &lt;a target="_blank" href="http://en.wikipedia.org/wiki/Dijkstra's_algorithm"&gt;Dijkstra
single source shortest path&lt;/a&gt;, while Kruskal is built on top of the &lt;a target="_blank" href="http://blog.dotnetwiki.org/WritingADisjointSetInCWithTheHelpOfCodeContractsAndPex.aspx"&gt;disjoint-set&lt;/a&gt; data
structure. By carefully picking the weights of the edges, we can assert different
things:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
if the edge weights are all equals, any spanning tree is minimal. So we can compare
the result to a depth-first-search algorithm (which can easily compute a spanning
tree). 
&lt;li&gt;
if some edge weights are different, there may be many minimum spanning tree. In this
case, we can still assert that weight of the tree is minimum. 
&lt;li&gt;
if all the edge weights are different, then the MST is unique. This fact can be used
to precisely pinpoint the differences in solution during testing.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
There is a corner case that needs to be checked: if all algorithm are no-op, i.e.
they don’t do anything. There solution will always match!
&lt;/p&gt;
&lt;p&gt;
Happy New Year!
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=32bd1002-4f51-4702-a6e3-7a8b6c2bde0d" /&gt;</description>
      <comments>http://blog.dotnetwiki.org/CommentView,guid,32bd1002-4f51-4702-a6e3-7a8b6c2bde0d.aspx</comments>
      <category>QuickGraph</category>
      <category>Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.dotnetwiki.org/Trackback.aspx?guid=06a6b1c8-6cc3-4728-9791-c129619219eb</trackback:ping>
      <pingback:server>http://blog.dotnetwiki.org/pingback.aspx</pingback:server>
      <pingback:target>http://blog.dotnetwiki.org/PermaLink,guid,06a6b1c8-6cc3-4728-9791-c129619219eb.aspx</pingback:target>
      <dc:creator>Jonathan de Halleux</dc:creator>
      <wfw:comment>http://blog.dotnetwiki.org/CommentView,guid,06a6b1c8-6cc3-4728-9791-c129619219eb.aspx</wfw:comment>
      <wfw:commentRss>http://blog.dotnetwiki.org/SyndicationService.asmx/GetEntryCommentsRss?guid=06a6b1c8-6cc3-4728-9791-c129619219eb</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I recently implemented a <a target="_blank" href="http://en.wikipedia.org/wiki/Disjoint-set_data_structure">Disjoint-Set</a> data
structure in <a target="_blank" href="http://www.codeplex.com/quickgraph">QuickGraph</a>,
which is the main building block of the <a target="_blank" href="http://en.wikipedia.org/wiki/Kruskal%27s_algorithm">Kruskal’s
minimum spanning tree algorithm</a>. This is a fun data-structure to look at, as it
purpose is quite different from the ‘main’ BCL collections. The disjoint-set is useful
to partition elements into sets, and defines 2 main operation to that purpose: <em>Find</em>,
finds the set an element belongs to (can be used to check if 2 elements are in the
same set), <em>Union</em> merges two sets.
</p>
        <p>
The <a target="_blank" href="http://www.codeplex.com/quickgraph/SourceControl/changeset/view/29499#343829">source
of the disjoint-set</a> is in the QuickGraph source, if you are curious about the
details.
</p>
        <p>
          <strong>Testing the disjoint-set</strong>
        </p>
        <p>
There is not much left to TDD when it comes to write data structure. Such data structure
are usually described in details in an article, and you ‘just’ have to follow the
authors instruction to implement. Nevertheless, unit testing is really critical to
ensure that the implementation is correct – but there is a risk of having to re-implement
the algorithm to test it. 
</p>
        <p>
For the disjoint-set, I used 2 tools developed at <a target="_blank" href="http://research.microsoft.com/rise">RiSE</a>: <a target="_blank" href="http://research.microsoft.com/contracts">Code
Contracts</a> and… <a target="_blank" href="http://research.microsoft.com/pex">Pex</a>.
When implementing data structure from the literature, I usually start ‘dumping’ the
code and the contracts. As much as possible, any invariant or property that the author
describes should be translated to contracts. It will give more opportunities for Pex
to find bugs in my code.
</p>
        <p>
          <strong>Contracts First</strong>
        </p>
        <p>
For example, the contracts for the <strong>Union</strong> method look as follows:
</p>
        <blockquote>
          <pre class="code">
            <span style="color: blue">private bool </span>Union(<span style="color: #2b91af">Element </span>left, <span style="color: #2b91af">Element </span>right)
{ <span style="color: #2b91af">Contract</span>.Requires(left != <span style="color: blue">null</span>); <span style="color: #2b91af">Contract</span>.Requires(right
!= <span style="color: blue">null</span>); <span style="color: #2b91af">Contract</span>.Ensures(<span style="color: #2b91af">Contract</span>.Result&lt;<span style="color: blue">bool</span>&gt;()
? <span style="color: #2b91af">Contract</span>.OldValue(<span style="color: blue">this</span>.SetCount)
- 1 == <span style="color: blue">this</span>.SetCount : <span style="color: #2b91af">Contract</span>.OldValue(<span style="color: blue">this</span>.SetCount)
== <span style="color: blue">this</span>.SetCount ); <span style="color: #2b91af">Contract</span>.Ensures(<span style="color: blue">this</span>.FindNoCompression(left)
== <span style="color: blue">this</span>.FindNoCompression(right)); </pre>
        </blockquote>
        <a href="http://11011.net/software/vspaste">
        </a>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
The two first requires clauses do the usual null checks. The first ensures clause
checks that if <em>Union</em> returns true, a merge has been done and the number of
sets has decreased by 1. The last ensures checks that left and right belong to the
same set at the end of the union. 
</p>
        <p>
Note that so far, I did not have to provide any kind of implementation details. The
other methods in the implementation receive the same treatment.
</p>
        <p>
          <strong>A Parameterized Unit Test</strong>
        </p>
        <p>
I wrote a single parameterized unit test while writing/debugging the implementation
of the forest. It could probably have been re-factored into many smaller tests, but
for the sake of laziness, I used a single one.
</p>
        <p>
The parameterized unit test implement a common scenario: add elements to the disjoint-set,
then apply a bunch of union operations. Along the way, we can rely on test assertion
and code contracts to check the correctness of our implementation.
</p>
        <p>
Let’s start with the test method signature:
</p>
        <blockquote>
          <pre class="code">[<span style="color: #2b91af">PexMethod</span>] <span style="color: blue">public
void </span>Unions(<span style="color: blue">int </span>elementCount, [<span style="color: #2b91af">PexAssumeNotNull</span>]<span style="color: #2b91af">KeyValuePair</span>&lt;<span style="color: blue">int</span>,<span style="color: blue">int</span>&gt;[]
unions) {</pre>
        </blockquote>
        <p>
The test takes a number of element to add and a sequence of unions to apply. The input
data as is needs to be refined to be useful. In that sense, we add <strong>assumptions</strong>,
under the form of calls to <em>PexAssume</em>, to tell Pex ‘how it should shape’ the
input data. In this case, we want to ensure that <em>elementCount</em> is positive
and relatively small; and that the values in unions are within the <em>[0…elementCount)</em> range.
</p>
        <blockquote>
          <pre class="code">
            <span style="color: #2b91af">
              <font color="#000000">
              </font>PexAssume</span>.IsTrue(0
&lt; elementCount); <span style="color: #2b91af">PexSymbolicValue</span>.Minimize(elementCount); <span style="color: #2b91af">PexAssume</span>.TrueForAll(
unions, u =&gt; 0 &lt;= u.Key &amp;&amp; u.Key &lt; elementCount &amp;&amp; 0 &lt;=
u.Value &amp;&amp; u.Value &lt; elementCount );</pre>
        </blockquote>
        <p>
Now that we have some data, we can start writing the first part of the scenario: filling
the disjoint-set. To do so, we simply add the integers from <em>[0..elementCount)</em>.
Along the way, we check that the <em>Contains,</em><em>ElementCount</em>, <em>SetCount</em> all
behave as expected:
</p>
        <blockquote>
          <pre class="code">
            <span style="color: blue">
              <font color="#000000">
              </font>var </span>target
= <span style="color: blue">new </span><span style="color: #2b91af">ForestDisjointSet</span>&lt;<span style="color: blue">int</span>&gt;(); <span style="color: green">//
fill up with 0..elementCount - 1 </span><span style="color: blue">for </span>(<span style="color: blue">int </span>i
= 0; i &lt; elementCount; i++) { target.MakeSet(i); <span style="color: #2b91af">Assert</span>.IsTrue(target.Contains(i)); <span style="color: #2b91af">Assert</span>.AreEqual(i
+ 1, target.ElementCount); <span style="color: #2b91af">Assert</span>.AreEqual(i +
1, target.SetCount); }</pre>
        </blockquote>
        <p>
The second part gets more interesting. Each element of the unions array is a ‘union’
action between 2 elements:
</p>
        <blockquote>
          <pre class="code">
            <span style="color: green">
              <font color="#000000">
              </font>//
apply Union for pairs unions[i], unions[i+1] </span>
            <span style="color: blue">for </span>(<span style="color: blue">int </span>i
= 0; i &lt; unions.Length; i++) { <span style="color: blue">var </span>left = unions[i].Key; <span style="color: blue">var </span>right=
unions[i].Value; <span style="color: blue">var </span>setCount = target.SetCount; <span style="color: blue">bool </span>unioned
= target.Union(left, right);</pre>
        </blockquote>
        <p>
There is 2 things we want to assert here: first, then left and right now belong to
the same set. Secondly, that the <em>SetCount</em> has been updated correctly: if <em>unioned</em> is
true, it should have decreased by one.
</p>
        <blockquote>
          <p>
        <span style="color: green">// should be
in the same set now<br />
       </span><span style="color: #2b91af">Assert</span>.IsTrue(target.AreInSameSet(left,
right));<br />
        <span style="color: green">// if unioned,
the count decreased by 1<br />
       </span><span style="color: #2b91af">PexAssert</span>.ImpliesIsTrue(unioned,
() =&gt; setCount - 1 == target.SetCount);<br />
    }<br />
}
</p>
        </blockquote>
        <p>
From this parameterized unit test, I could work on the implementation, and refine
again and again until it had all passing tests (I did not write <strong>any</strong> other
test code)<strong>.</strong> 
</p>
        <p>
          <strong>Happy Ending</strong>
        </p>
        <p>
The resulting test suite generated by Pex is summarized in the screenshot below: the
number of elements does not really matter, what is interesting is the sequence of
unions performed. This test suite achieves 100% coverage of the methods under test
:).
</p>
        <p>
          <a href="http://blog.dotnetwiki.org/images/DisjointSetclassinC_8E4B/image.png">
            <img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/images/DisjointSetclassinC_8E4B/image_thumb.png" width="458" height="287" />
          </a>
        </p>
        <p>
In fact, the <em>Union</em> method involved some tricky branches to cover, due to
some optimization occurring in the disjoint-set. Pex managed to generate unit tests
for each of the branches. 
</p>
        <p>
          <a href="http://blog.dotnetwiki.org/images/DisjointSetclassinC_8E4B/image_3.png">
            <img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/images/DisjointSetclassinC_8E4B/image_thumb_3.png" width="369" height="273" />
          </a>
        </p>
        <p>
        </p>
        <p>
          <strong>Thanks to the contracts, the test assertions, and the high code coverage of
the generated test suite, I have now a good confidence that my code is properly tested. </strong>The
End!
</p>
        <p>
(Next time, we will talk about testing minimum spanning tree implementations…)
</p>
        <img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=06a6b1c8-6cc3-4728-9791-c129619219eb" />
      </body>
      <title>Writing a DisjointSet in C# with the help of Code Contracts and Pex.</title>
      <guid isPermaLink="false">http://blog.dotnetwiki.org/PermaLink,guid,06a6b1c8-6cc3-4728-9791-c129619219eb.aspx</guid>
      <link>http://blog.dotnetwiki.org/2008/12/30/WritingADisjointSetInCWithTheHelpOfCodeContractsAndPex.aspx</link>
      <pubDate>Tue, 30 Dec 2008 19:41:45 GMT</pubDate>
      <description>&lt;p&gt;
I recently implemented a &lt;a target="_blank" href="http://en.wikipedia.org/wiki/Disjoint-set_data_structure"&gt;Disjoint-Set&lt;/a&gt; data
structure in &lt;a target="_blank" href="http://www.codeplex.com/quickgraph"&gt;QuickGraph&lt;/a&gt;,
which is the main building block of the &lt;a target="_blank" href="http://en.wikipedia.org/wiki/Kruskal%27s_algorithm"&gt;Kruskal’s
minimum spanning tree algorithm&lt;/a&gt;. This is a fun data-structure to look at, as it
purpose is quite different from the ‘main’ BCL collections. The disjoint-set is useful
to partition elements into sets, and defines 2 main operation to that purpose: &lt;em&gt;Find&lt;/em&gt;,
finds the set an element belongs to (can be used to check if 2 elements are in the
same set), &lt;em&gt;Union&lt;/em&gt; merges two sets.
&lt;/p&gt;
&lt;p&gt;
The &lt;a target="_blank" href="http://www.codeplex.com/quickgraph/SourceControl/changeset/view/29499#343829"&gt;source
of the disjoint-set&lt;/a&gt; is in the QuickGraph source, if you are curious about the
details.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Testing the disjoint-set&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
There is not much left to TDD when it comes to write data structure. Such data structure
are usually described in details in an article, and you ‘just’ have to follow the
authors instruction to implement. Nevertheless, unit testing is really critical to
ensure that the implementation is correct – but there is a risk of having to re-implement
the algorithm to test it. 
&lt;/p&gt;
&lt;p&gt;
For the disjoint-set, I used 2 tools developed at &lt;a target="_blank" href="http://research.microsoft.com/rise"&gt;RiSE&lt;/a&gt;: &lt;a target="_blank" href="http://research.microsoft.com/contracts"&gt;Code
Contracts&lt;/a&gt; and… &lt;a target="_blank" href="http://research.microsoft.com/pex"&gt;Pex&lt;/a&gt;.
When implementing data structure from the literature, I usually start ‘dumping’ the
code and the contracts. As much as possible, any invariant or property that the author
describes should be translated to contracts. It will give more opportunities for Pex
to find bugs in my code.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Contracts First&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
For example, the contracts for the &lt;strong&gt;Union&lt;/strong&gt; method look as follows:
&lt;/p&gt;
&lt;blockquote&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;private bool &lt;/span&gt;Union(&lt;span style="color: #2b91af"&gt;Element &lt;/span&gt;left, &lt;span style="color: #2b91af"&gt;Element &lt;/span&gt;right)
{ &lt;span style="color: #2b91af"&gt;Contract&lt;/span&gt;.Requires(left != &lt;span style="color: blue"&gt;null&lt;/span&gt;); &lt;span style="color: #2b91af"&gt;Contract&lt;/span&gt;.Requires(right
!= &lt;span style="color: blue"&gt;null&lt;/span&gt;); &lt;span style="color: #2b91af"&gt;Contract&lt;/span&gt;.Ensures(&lt;span style="color: #2b91af"&gt;Contract&lt;/span&gt;.Result&amp;lt;&lt;span style="color: blue"&gt;bool&lt;/span&gt;&amp;gt;()
? &lt;span style="color: #2b91af"&gt;Contract&lt;/span&gt;.OldValue(&lt;span style="color: blue"&gt;this&lt;/span&gt;.SetCount)
- 1 == &lt;span style="color: blue"&gt;this&lt;/span&gt;.SetCount : &lt;span style="color: #2b91af"&gt;Contract&lt;/span&gt;.OldValue(&lt;span style="color: blue"&gt;this&lt;/span&gt;.SetCount)
== &lt;span style="color: blue"&gt;this&lt;/span&gt;.SetCount ); &lt;span style="color: #2b91af"&gt;Contract&lt;/span&gt;.Ensures(&lt;span style="color: blue"&gt;this&lt;/span&gt;.FindNoCompression(left)
== &lt;span style="color: blue"&gt;this&lt;/span&gt;.FindNoCompression(right)); &lt;/pre&gt;&lt;/blockquote&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
The two first requires clauses do the usual null checks. The first ensures clause
checks that if &lt;em&gt;Union&lt;/em&gt; returns true, a merge has been done and the number of
sets has decreased by 1. The last ensures checks that left and right belong to the
same set at the end of the union. 
&lt;/p&gt;
&lt;p&gt;
Note that so far, I did not have to provide any kind of implementation details. The
other methods in the implementation receive the same treatment.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;A Parameterized Unit Test&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
I wrote a single parameterized unit test while writing/debugging the implementation
of the forest. It could probably have been re-factored into many smaller tests, but
for the sake of laziness, I used a single one.
&lt;/p&gt;
&lt;p&gt;
The parameterized unit test implement a common scenario: add elements to the disjoint-set,
then apply a bunch of union operations. Along the way, we can rely on test assertion
and code contracts to check the correctness of our implementation.
&lt;/p&gt;
&lt;p&gt;
Let’s start with the test method signature:
&lt;/p&gt;
&lt;blockquote&gt;&lt;pre class="code"&gt;[&lt;span style="color: #2b91af"&gt;PexMethod&lt;/span&gt;] &lt;span style="color: blue"&gt;public
void &lt;/span&gt;Unions(&lt;span style="color: blue"&gt;int &lt;/span&gt;elementCount, [&lt;span style="color: #2b91af"&gt;PexAssumeNotNull&lt;/span&gt;]&lt;span style="color: #2b91af"&gt;KeyValuePair&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;,&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt;[]
unions) {&lt;/pre&gt;&lt;/blockquote&gt; 
&lt;p&gt;
The test takes a number of element to add and a sequence of unions to apply. The input
data as is needs to be refined to be useful. In that sense, we add &lt;strong&gt;assumptions&lt;/strong&gt;,
under the form of calls to &lt;em&gt;PexAssume&lt;/em&gt;, to tell Pex ‘how it should shape’ the
input data. In this case, we want to ensure that &lt;em&gt;elementCount&lt;/em&gt; is positive
and relatively small; and that the values in unions are within the &lt;em&gt;[0…elementCount)&lt;/em&gt; range.
&lt;/p&gt;
&lt;blockquote&gt;&lt;pre class="code"&gt;&lt;span style="color: #2b91af"&gt;&lt;font color="#000000"&gt; &lt;/font&gt;PexAssume&lt;/span&gt;.IsTrue(0
&amp;lt; elementCount); &lt;span style="color: #2b91af"&gt;PexSymbolicValue&lt;/span&gt;.Minimize(elementCount); &lt;span style="color: #2b91af"&gt;PexAssume&lt;/span&gt;.TrueForAll(
unions, u =&amp;gt; 0 &amp;lt;= u.Key &amp;amp;&amp;amp; u.Key &amp;lt; elementCount &amp;amp;&amp;amp; 0 &amp;lt;=
u.Value &amp;amp;&amp;amp; u.Value &amp;lt; elementCount );&lt;/pre&gt;&lt;/blockquote&gt; 
&lt;p&gt;
Now that we have some data, we can start writing the first part of the scenario: filling
the disjoint-set. To do so, we simply add the integers from &lt;em&gt;[0..elementCount)&lt;/em&gt;.
Along the way, we check that the &lt;em&gt;Contains,&lt;/em&gt; &lt;em&gt;ElementCount&lt;/em&gt;, &lt;em&gt;SetCount&lt;/em&gt; all
behave as expected:
&lt;/p&gt;
&lt;blockquote&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&lt;font color="#000000"&gt; &lt;/font&gt;var &lt;/span&gt;target
= &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ForestDisjointSet&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt;(); &lt;span style="color: green"&gt;//
fill up with 0..elementCount - 1 &lt;/span&gt;&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; elementCount; i++) { target.MakeSet(i); &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.IsTrue(target.Contains(i)); &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.AreEqual(i
+ 1, target.ElementCount); &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.AreEqual(i +
1, target.SetCount); }&lt;/pre&gt;&lt;/blockquote&gt; 
&lt;p&gt;
The second part gets more interesting. Each element of the unions array is a ‘union’
action between 2 elements:
&lt;/p&gt;
&lt;blockquote&gt;&lt;pre class="code"&gt;&lt;span style="color: green"&gt;&lt;font color="#000000"&gt; &lt;/font&gt;//
apply Union for pairs unions[i], unions[i+1] &lt;/span&gt;&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; unions.Length; i++) { &lt;span style="color: blue"&gt;var &lt;/span&gt;left = unions[i].Key; &lt;span style="color: blue"&gt;var &lt;/span&gt;right=
unions[i].Value; &lt;span style="color: blue"&gt;var &lt;/span&gt;setCount = target.SetCount; &lt;span style="color: blue"&gt;bool &lt;/span&gt;unioned
= target.Union(left, right);&lt;/pre&gt;&lt;/blockquote&gt; 
&lt;p&gt;
There is 2 things we want to assert here: first, then left and right now belong to
the same set. Secondly, that the &lt;em&gt;SetCount&lt;/em&gt; has been updated correctly: if &lt;em&gt;unioned&lt;/em&gt; is
true, it should have decreased by one.
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;// should be
in the same set now&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.IsTrue(target.AreInSameSet(left,
right));&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;// if unioned,
the count decreased by 1&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: #2b91af"&gt;PexAssert&lt;/span&gt;.ImpliesIsTrue(unioned,
() =&amp;gt; setCount - 1 == target.SetCount);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;
}
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
From this parameterized unit test, I could work on the implementation, and refine
again and again until it had all passing tests (I did not write &lt;strong&gt;any&lt;/strong&gt; other
test code)&lt;strong&gt;.&lt;/strong&gt;&amp;nbsp;
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Happy Ending&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
The resulting test suite generated by Pex is summarized in the screenshot below: the
number of elements does not really matter, what is interesting is the sequence of
unions performed. This test suite achieves 100% coverage of the methods under test
:).
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.dotnetwiki.org/images/DisjointSetclassinC_8E4B/image.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/images/DisjointSetclassinC_8E4B/image_thumb.png" width="458" height="287"&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
In fact, the &lt;em&gt;Union&lt;/em&gt; method involved some tricky branches to cover, due to
some optimization occurring in the disjoint-set. Pex managed to generate unit tests
for each of the branches. 
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.dotnetwiki.org/images/DisjointSetclassinC_8E4B/image_3.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/images/DisjointSetclassinC_8E4B/image_thumb_3.png" width="369" height="273"&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Thanks to the contracts, the test assertions, and the high code coverage of
the generated test suite, I have now a good confidence that my code is properly tested. &lt;/strong&gt;The
End!
&lt;/p&gt;
&lt;p&gt;
(Next time, we will talk about testing minimum spanning tree implementations…)
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=06a6b1c8-6cc3-4728-9791-c129619219eb" /&gt;</description>
      <comments>http://blog.dotnetwiki.org/CommentView,guid,06a6b1c8-6cc3-4728-9791-c129619219eb.aspx</comments>
      <category>Pex</category>
      <category>QuickGraph</category>
      <category>Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.dotnetwiki.org/Trackback.aspx?guid=1f98b2d2-011e-4ec8-bdf1-d6d23b0958e0</trackback:ping>
      <pingback:server>http://blog.dotnetwiki.org/pingback.aspx</pingback:server>
      <pingback:target>http://blog.dotnetwiki.org/PermaLink,guid,1f98b2d2-011e-4ec8-bdf1-d6d23b0958e0.aspx</pingback:target>
      <dc:creator>Jonathan de Halleux</dc:creator>
      <wfw:comment>http://blog.dotnetwiki.org/CommentView,guid,1f98b2d2-011e-4ec8-bdf1-d6d23b0958e0.aspx</wfw:comment>
      <wfw:commentRss>http://blog.dotnetwiki.org/SyndicationService.asmx/GetEntryCommentsRss?guid=1f98b2d2-011e-4ec8-bdf1-d6d23b0958e0</wfw:commentRss>
      <slash:comments>4</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Check out the session on <a target="_blank" href="http://research.microsoft.com/contracts">Code
Contracts</a> and <a target="_blank" href="http://research.microsoft.com/pex">Pex</a> on
Channel 9. You will learn about the new cool API to express pre-conditions, post-conditions
and invariants in your favorite language – i.e. design by contracts (DbC) for .NET
and the new <a target="_blank" href="http://blogs.msdn.com/nikolait/archive/2008/10/21/sneak-preview-code-digger-the-new-pex-experience.aspx">Code
Digger</a> experience in Pex, and most importantly <strong>how DbC and Pex play well
together.</strong></p>
        <blockquote>
          <p>
            <a title="http://channel9.msdn.com/pdc2008/tl51/" href="http://channel9.msdn.com/pdc2008/tl51/">http://channel9.msdn.com/pdc2008/tl51/</a>
          </p>
        </blockquote>
        <p>
          <a href="http://blog.dotnetwiki.org/images/CodeDiggerSessiononChannel9_14324/image.png">
            <img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/images/CodeDiggerSessiononChannel9_14324/image_thumb.png" width="404" height="223" />
          </a>
        </p>
        <img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=1f98b2d2-011e-4ec8-bdf1-d6d23b0958e0" />
      </body>
      <title>Code Digger Session on Channel 9</title>
      <guid isPermaLink="false">http://blog.dotnetwiki.org/PermaLink,guid,1f98b2d2-011e-4ec8-bdf1-d6d23b0958e0.aspx</guid>
      <link>http://blog.dotnetwiki.org/2008/11/06/CodeDiggerSessionOnChannel9.aspx</link>
      <pubDate>Thu, 06 Nov 2008 07:10:22 GMT</pubDate>
      <description>&lt;p&gt;
Check out the session on &lt;a target="_blank" href="http://research.microsoft.com/contracts"&gt;Code
Contracts&lt;/a&gt; and &lt;a target="_blank" href="http://research.microsoft.com/pex"&gt;Pex&lt;/a&gt; on
Channel 9. You will learn about the new cool API to express pre-conditions, post-conditions
and invariants in your favorite language – i.e. design by contracts (DbC) for .NET
and the new &lt;a target="_blank" href="http://blogs.msdn.com/nikolait/archive/2008/10/21/sneak-preview-code-digger-the-new-pex-experience.aspx"&gt;Code
Digger&lt;/a&gt; experience in Pex, and most importantly &lt;strong&gt;how DbC and Pex play well
together.&lt;/strong&gt;
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
&lt;a title="http://channel9.msdn.com/pdc2008/tl51/" href="http://channel9.msdn.com/pdc2008/tl51/"&gt;http://channel9.msdn.com/pdc2008/tl51/&lt;/a&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
&lt;a href="http://blog.dotnetwiki.org/images/CodeDiggerSessiononChannel9_14324/image.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/images/CodeDiggerSessiononChannel9_14324/image_thumb.png" width="404" height="223"&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=1f98b2d2-011e-4ec8-bdf1-d6d23b0958e0" /&gt;</description>
      <comments>http://blog.dotnetwiki.org/CommentView,guid,1f98b2d2-011e-4ec8-bdf1-d6d23b0958e0.aspx</comments>
      <category>Pex</category>
      <category>Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.dotnetwiki.org/Trackback.aspx?guid=68b05633-1ce2-4e69-bb0d-e975781b060d</trackback:ping>
      <pingback:server>http://blog.dotnetwiki.org/pingback.aspx</pingback:server>
      <pingback:target>http://blog.dotnetwiki.org/PermaLink,guid,68b05633-1ce2-4e69-bb0d-e975781b060d.aspx</pingback:target>
      <dc:creator>Jonathan de Halleux</dc:creator>
      <wfw:comment>http://blog.dotnetwiki.org/CommentView,guid,68b05633-1ce2-4e69-bb0d-e975781b060d.aspx</wfw:comment>
      <wfw:commentRss>http://blog.dotnetwiki.org/SyndicationService.asmx/GetEntryCommentsRss?guid=68b05633-1ce2-4e69-bb0d-e975781b060d</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Wonder what we’ve been up to for the last months… We’ve been building a very cool
development experience on top of Pex that we call <strong><em>Code Digging</em></strong>. <a target="_blank" href="http://blogs.msdn.com/nikolait/archive/2008/10/21/sneak-preview-code-digger-the-new-pex-experience.aspx">Check
out Nikolai’s blog post on what it means to <strong>you!       </strong>.</a></p>
        <p>
          <a href="http://blog.dotnetwiki.org/images/EntertheDigger_12D11/image.png">
            <img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/images/EntertheDigger_12D11/image_thumb.png" width="408" height="308" />
          </a>
        </p>
        <img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=68b05633-1ce2-4e69-bb0d-e975781b060d" />
      </body>
      <title>Enter the Digger</title>
      <guid isPermaLink="false">http://blog.dotnetwiki.org/PermaLink,guid,68b05633-1ce2-4e69-bb0d-e975781b060d.aspx</guid>
      <link>http://blog.dotnetwiki.org/2008/10/22/EnterTheDigger.aspx</link>
      <pubDate>Wed, 22 Oct 2008 04:49:02 GMT</pubDate>
      <description>&lt;p&gt;
Wonder what we’ve been up to for the last months… We’ve been building a very cool
development experience on top of Pex that we call &lt;strong&gt;&lt;em&gt;Code Digging&lt;/em&gt;&lt;/strong&gt;. &lt;a target="_blank" href="http://blogs.msdn.com/nikolait/archive/2008/10/21/sneak-preview-code-digger-the-new-pex-experience.aspx"&gt;Check
out Nikolai’s blog post on what it means to &lt;strong&gt;you!&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/strong&gt;.&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.dotnetwiki.org/images/EntertheDigger_12D11/image.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/images/EntertheDigger_12D11/image_thumb.png" width="408" height="308"&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=68b05633-1ce2-4e69-bb0d-e975781b060d" /&gt;</description>
      <comments>http://blog.dotnetwiki.org/CommentView,guid,68b05633-1ce2-4e69-bb0d-e975781b060d.aspx</comments>
      <category>Pex</category>
      <category>Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.dotnetwiki.org/Trackback.aspx?guid=c83452d0-33d7-4628-8855-957ed53a0a6b</trackback:ping>
      <pingback:server>http://blog.dotnetwiki.org/pingback.aspx</pingback:server>
      <pingback:target>http://blog.dotnetwiki.org/PermaLink,guid,c83452d0-33d7-4628-8855-957ed53a0a6b.aspx</pingback:target>
      <dc:creator>Jonathan de Halleux</dc:creator>
      <wfw:comment>http://blog.dotnetwiki.org/CommentView,guid,c83452d0-33d7-4628-8855-957ed53a0a6b.aspx</wfw:comment>
      <wfw:commentRss>http://blog.dotnetwiki.org/SyndicationService.asmx/GetEntryCommentsRss?guid=c83452d0-33d7-4628-8855-957ed53a0a6b</wfw:commentRss>
      <slash:comments>6</slash:comments>
      <title>Pex It: The File System, Abstraction, Mocking , Modeling</title>
      <guid isPermaLink="false">http://blog.dotnetwiki.org/PermaLink,guid,c83452d0-33d7-4628-8855-957ed53a0a6b.aspx</guid>
      <link>http://blog.dotnetwiki.org/2008/10/21/PexItTheFileSystemAbstractionMockingModeling.aspx</link>
      <pubDate>Tue, 21 Oct 2008 19:15:09 GMT</pubDate>
      <description>&lt;p&gt;
Have you ever written code that directly used the .NET File APIs? We probably all
did although we knew it would make the code less testable and dependent on the file
system state. As bad as it sounds, it really requires a lot of discipline and work
to avoid this: one would need to create an abstraction layer over the file system,
which is not a short task (think long/tedious).
&lt;/p&gt;
&lt;blockquote&gt;&lt;pre class="code"&gt;&lt;span style="color: green"&gt;// in how many ways can this
break? &lt;/span&gt;&lt;span style="color: blue"&gt;public static void &lt;/span&gt;CleanDirectory(&lt;span style="color: blue"&gt;string &lt;/span&gt;path)
{ &lt;span style="color: blue"&gt;if &lt;/span&gt;(&lt;span style="color: #2b91af"&gt;Directory&lt;/span&gt;.Exists(path)) &lt;span style="color: #2b91af"&gt;Directory&lt;/span&gt;.Delete(path, &lt;span style="color: blue"&gt;true&lt;/span&gt;); &lt;span style="color: #2b91af"&gt;Directory&lt;/span&gt;.CreateDirectory(path);
} &lt;/pre&gt;&lt;/blockquote&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
&lt;strong&gt;Abstraction&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
Fortunately, there always someone else who got motivated at some point. &lt;a target="_blank" href="http://www.ademiller.com/blogs/tech/2007/12/mocking-the-file-system/"&gt;Ade
Miller digged&lt;/a&gt; an abstraction of the File System, the &lt;a href="http://www.codeplex.com/CodePlexClient/SourceControl/FileView.aspx?itemId=59623&amp;changeSetId=17983"&gt;IFileSystem&lt;/a&gt; interface,
that &lt;a target="_blank" href="http://bradwilson.typepad.com/"&gt;Brad Wilson&lt;/a&gt; had
written for the CodePlex client project. Very nice since it provides a solid foundation
for cleanly abstracting from the File System, and thus increase the testability of
our code.
&lt;/p&gt;
&lt;blockquote&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;// a little better, testable
code at least&lt;br&gt;
public static void &lt;/span&gt;CleanDirectory(&lt;span style="color: #2b91af"&gt;IFileSystem &lt;/span&gt;fs, &lt;span style="color: blue"&gt;string &lt;/span&gt;path)
{ &lt;span style="color: blue"&gt;if &lt;/span&gt;(fs.DirectoryExists(path)) fs.DeleteDirectory(path, &lt;span style="color: blue"&gt;false&lt;/span&gt;);
fs.CreateDirectory(path); } &lt;/pre&gt;&lt;/blockquote&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
&lt;strong&gt;Mocking&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
So with this interface we can write code that we’ll be able to test in isolation from
the physical file system. That’s great but there is still a lot of work on the should
of the developer: the developer will have write intricate scenarios involving mocks
to simulate the different possible configurations of the file system. &lt;strong&gt;No matter
which mock framework &lt;/strong&gt;(&lt;a target="_blank" href="http://code.google.com/p/moq/"&gt;Moq&lt;/a&gt;, &lt;a target="_blank" href="http://ayende.com/projects/rhino-mocks.aspx"&gt;Rhino&lt;/a&gt;, &lt;a target="_blank" href="http://www.typemock.com/"&gt;Isolator&lt;/a&gt;,
…), he’ll be using, (1) it’s going to be painful, (2) he’ll miss cases. It’s probably
easy to write a single “happy path”, but especially with the file system there are
quite some realistic “unhappy paths”.
&lt;/p&gt;
&lt;p&gt;
This test case uses &lt;a target="_blank" href="http://code.google.com/p/moq/"&gt;Moq&lt;/a&gt; to
create the scenario where there is a directory already. Although Moq has a very slick
API to set expectations, it is still a lot of work to write this basic scenario. (And
what exactly is the meaning of “Expect”, the delegate or expression inside, “Returns”
and “Callback”?)
&lt;/p&gt;
&lt;blockquote&gt;&lt;pre class="code"&gt;[&lt;span style="color: #2b91af"&gt;TestMethod&lt;/span&gt;] &lt;span style="color: blue"&gt;public
void &lt;/span&gt;DeletesAndCreateNewDirectory() { &lt;span style="color: blue"&gt;var &lt;/span&gt;fs
= &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Mock&lt;/span&gt;&lt;&lt;span style="color: #2b91af"&gt;IFileSystem&gt;&gt;(); &lt;span style="color: blue"&gt;string &lt;/span&gt;path
= &lt;span style="color: #a31515"&gt;"foo"&lt;/span&gt;; fs.Expect(f =&gt; f.DirectoryExists(path)).Returns(&lt;span style="color: blue"&gt;true&lt;/span&gt;);
fs.Expect(f =&gt; f.DeleteDirectory(path, &lt;span style="color: blue"&gt;false&lt;/span&gt;)).Callback(
() =&gt; &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515"&gt;"deleted"&lt;/span&gt;));
fs.Expect(f =&gt; f.CreateDirectory(path)).Callback(() =&gt; &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515"&gt;"created"&lt;/span&gt;)); &lt;span style="color: #2b91af"&gt;DirectoryExtensions&lt;/span&gt;.CleanDirectory(fs.Object,
path); }&lt;/pre&gt;&lt;/blockquote&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
&lt;strong&gt;Modeling&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
We had our intern, &lt;a target="_blank" href="http://research.microsoft.com/Pex/people.aspx"&gt;Soonho
Kong&lt;/a&gt;, work on a&lt;strong&gt; Parameterized Model of the File System&lt;/strong&gt;, built
on top of the IFileSystem interface (yes that same interface Brad Wilson published
on CodePlex). We say that the model is parameterized because it uses the &lt;a target="_blank" href="http://research.microsoft.com/pex"&gt;Pex&lt;/a&gt; &lt;a target="_blank" href="http://research.microsoft.com/projects/Pex/Wiki/pexchoose.html"&gt;choices&lt;/a&gt; API
to create arbitrary initial File System states; Pex “chooses” each such state (actually,
Pex carefully computes the state &lt;a target="_blank" href="http://research.microsoft.com/projects/z3"&gt;using
a constraint solver&lt;/a&gt;) to trigger different code paths in the code. You can think
of each choice as a new parameter to the test. Or to put this with an example: if
your code checks that the file “foo.txt” exists, then the parameterized model would &lt;em&gt;choose &lt;/em&gt;a
file system state that would contain a “foo.txt” file (or not, in another state, to
cover both branches of the program).
&lt;/p&gt;
&lt;p&gt;
So what does it mean for you? Well, the way you write tests that involve the file
system changes radically. You simply need to pass the file system &lt;strong&gt;model&lt;/strong&gt; to
your implementation. The model is an under-approximation of the real file system (which
means that we didn’t model every single nastiness that can occur when the moon is
full), but it definitely captures more practically relevant corner cases than we (humans)
usually think about. Let’s see this in the following test:
&lt;/p&gt;
&lt;blockquote&gt;&lt;pre class="code"&gt;[&lt;span style="color: #2b91af"&gt;PexMethod&lt;/span&gt;] &lt;span style="color: blue"&gt;public
void &lt;/span&gt;CleanDirectory() { &lt;span style="color: blue"&gt;var &lt;/span&gt;fs = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;PFileSystem&lt;/span&gt;(); &lt;span style="color: blue"&gt;string &lt;/span&gt;path
= &lt;span style="color: #a31515"&gt;@"\foo"&lt;/span&gt;; &lt;span style="color: blue"&gt;try &lt;/span&gt;{ &lt;span style="color: #2b91af"&gt;DirectoryExtensions&lt;/span&gt;.CleanDirectory(fs,
path); &lt;span style="color: green"&gt;// assert: the directory exists and is empty &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.IsTrue(fs.DirectoryExists(path)); &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.AreEqual(0,
fs.GetFiles(path).Length); } &lt;span style="color: blue"&gt;finally &lt;/span&gt;{ fs.Dir();
} } &lt;/pre&gt;&lt;/blockquote&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
When we run Pex, we get 7 generated tests. In fact, Pex finds an interesting bug that
occurs when a file with the name of the directory to clean already exists. In the
Pex Exploration Results window, you can see a ‘dir’-like output of the file system
model associated with a particular test case (the fs.Dir() method call outputs that
text to the console which Pex captures).
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.dotnetwiki.org/images/PexFilesystemabstractingmockingModeling_C001/image.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/images/PexFilesystemabstractingmockingModeling_C001/image_thumb.png" width="778" height="234"&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
This bug is the kind of corner-case that makes testing the file system so fun/hard.
Thanks to the parameterized model (and Soonho), we got it for free. Note also that
the assertion in our test is pretty powerful since it must be true for any configuration
of the file system (it almost smells like a functional specification to me):
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
&lt;span style="color: green"&gt;// assert: the directory exists and is empty 
&lt;br&gt;
&lt;/span&gt;&lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.IsTrue(fs.DirectoryExists(path)); 
&lt;br&gt;
&lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.AreEqual(0, fs.GetFiles(path).Length); 
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
Happy modeling!
&lt;/p&gt;
&lt;p&gt;
&lt;em&gt;The full source of PFileSystem will be available in the next version of Pex (0.8)..&lt;/em&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=c83452d0-33d7-4628-8855-957ed53a0a6b" /&gt;</description>
      <comments>http://blog.dotnetwiki.org/CommentView,guid,c83452d0-33d7-4628-8855-957ed53a0a6b.aspx</comments>
      <category>Pex</category>
      <category>Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.dotnetwiki.org/Trackback.aspx?guid=addb0c31-49b9-4d2a-b684-817774b90d43</trackback:ping>
      <pingback:server>http://blog.dotnetwiki.org/pingback.aspx</pingback:server>
      <pingback:target>http://blog.dotnetwiki.org/PermaLink,guid,addb0c31-49b9-4d2a-b684-817774b90d43.aspx</pingback:target>
      <dc:creator>Jonathan de Halleux</dc:creator>
      <wfw:comment>http://blog.dotnetwiki.org/CommentView,guid,addb0c31-49b9-4d2a-b684-817774b90d43.aspx</wfw:comment>
      <wfw:commentRss>http://blog.dotnetwiki.org/SyndicationService.asmx/GetEntryCommentsRss?guid=addb0c31-49b9-4d2a-b684-817774b90d43</wfw:commentRss>
      <slash:comments>2</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
We are very excited to announce that <a target="_blank" href="http://research.microsoft.com/pex"><strong>Pex</strong></a> has
a session at <a target="_blank" href="http://www.microsoftpdc.com/">PDC 2008</a>.
We will be talking about code contracts and Pex, and how they play nicely together. <a target="_blank" href="http://sessions.microsoftpdc.com/">Book
it now in your conference agenda!!!</a> (look ‘Research’ or ‘Pex’ in the session list).
</p>
        <blockquote>
          <p>
            <a href="http://blog.dotnetwiki.org/images/PexatPDC2008Wewillbethere_D792/image.png">
              <img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/images/PexatPDC2008Wewillbethere_D792/image_thumb.png" width="482" height="366" />
            </a> 
</p>
        </blockquote>
        <p>
See you there and don’t forget to swing by our booth.
</p>
        <img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=addb0c31-49b9-4d2a-b684-817774b90d43" />
      </body>
      <title>Pex at PDC2008 &amp;ndash; We will be there!</title>
      <guid isPermaLink="false">http://blog.dotnetwiki.org/PermaLink,guid,addb0c31-49b9-4d2a-b684-817774b90d43.aspx</guid>
      <link>http://blog.dotnetwiki.org/2008/10/03/PexAtPDC2008NdashWeWillBeThere.aspx</link>
      <pubDate>Fri, 03 Oct 2008 05:42:09 GMT</pubDate>
      <description>&lt;p&gt;
We are very excited to announce that &lt;a target="_blank" href="http://research.microsoft.com/pex"&gt;&lt;strong&gt;Pex&lt;/strong&gt;&lt;/a&gt; has
a session at &lt;a target="_blank" href="http://www.microsoftpdc.com/"&gt;PDC 2008&lt;/a&gt;.
We will be talking about code contracts and Pex, and how they play nicely together. &lt;a target="_blank" href="http://sessions.microsoftpdc.com/"&gt;Book
it now in your conference agenda!!!&lt;/a&gt; (look ‘Research’ or ‘Pex’ in the session list).
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
&lt;a href="http://blog.dotnetwiki.org/images/PexatPDC2008Wewillbethere_D792/image.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/images/PexatPDC2008Wewillbethere_D792/image_thumb.png" width="482" height="366"&gt;&lt;/a&gt;&amp;nbsp;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
See you there and don’t forget to swing by our booth.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=addb0c31-49b9-4d2a-b684-817774b90d43" /&gt;</description>
      <comments>http://blog.dotnetwiki.org/CommentView,guid,addb0c31-49b9-4d2a-b684-817774b90d43.aspx</comments>
      <category>MbUnit</category>
      <category>Pex</category>
      <category>Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.dotnetwiki.org/Trackback.aspx?guid=8e066c6d-0833-4600-b3d9-9ac9a84f447a</trackback:ping>
      <pingback:server>http://blog.dotnetwiki.org/pingback.aspx</pingback:server>
      <pingback:target>http://blog.dotnetwiki.org/PermaLink,guid,8e066c6d-0833-4600-b3d9-9ac9a84f447a.aspx</pingback:target>
      <dc:creator>Jonathan de Halleux</dc:creator>
      <wfw:comment>http://blog.dotnetwiki.org/CommentView,guid,8e066c6d-0833-4600-b3d9-9ac9a84f447a.aspx</wfw:comment>
      <wfw:commentRss>http://blog.dotnetwiki.org/SyndicationService.asmx/GetEntryCommentsRss?guid=8e066c6d-0833-4600-b3d9-9ac9a84f447a</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <blockquote>
          <p>
            <em>How do you write good </em>
            <a target="_blank" href="http://research.microsoft.com/pex">
              <em>parameterized
unit tests</em>
            </a>
            <em>? 
<br />
Where do they work the best? 
<br />
Are there some <strong>Test Patterns</strong> ? <strong>Anti Patterns</strong>?</em>
          </p>
        </blockquote>
        <p>
This is the kind of questions that we have received many times from <a target="_blank" href="http://research.microsoft.com/pex">Pex</a> users.
We just released <a target="_blank" href="http://blogs.msdn.com/nikolait/archive/2008/09/22/pex-0-7-released.aspx">Pex
0.7</a> which contains a list patterns and anti-patterns for parameterized unit testing
(this is still a draft but we feel that we already have a number of good patterns
that would be helpful for anyone giving a shot at Pex):
</p>
        <ul>
          <li>
            <strong>direct link:</strong>
            <a href="http://research.microsoft.com/pex/articles/pexpatterns.pdf">
              <strong>http://research.microsoft.com/pex/articles/pexpatterns.pdf</strong>
            </a>   
</li>
          <li>
            <strong>from the installer (0.7 and higher):</strong> after installing Pex, go to
Start –&gt; All Programs –&gt; Microsoft Pex –&gt; Documentation –&gt; Patterns.pdf</li>
        </ul>
        <p>
Note that most of the patterns in this document are not Pex specific and apply to
parameterized unit tests in general; including <a target="_blank" href="http://www.mbunit.com">MbUnit</a> RowTest/CombinatorialTest/DataTest, <a target="_blank" href="http://www.nunit.org">NUnit</a> RowTest,
MSTest Data Test, etc…
</p>
        <p>
          <strong>The amazing ‘quadruple A’ pattern</strong>
        </p>
        <p>
The ‘triple A’ pattern is a common way of writing a unit test: Arrange, Act, Assert.
Even more ‘A’crobatic, we propose the ‘quadruple A’ where we added one more ‘A’ for <em>assumption</em>:
</p>
        <p>
          <a href="http://blog.dotnetwiki.org/images/ParameterizedUnitTestPatternsdraft_7F53/image.png">
            <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/images/ParameterizedUnitTestPatternsdraft_7F53/image_thumb.png" width="571" height="618" />
          </a>
        </p>
        <p>
Pex is an automated white box testing tool from Microsoft Research. 
<br />
More information at <a href="http://research.microsoft.com/pex">http://research.microsoft.com/pex</a>.
</p>
        <img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=8e066c6d-0833-4600-b3d9-9ac9a84f447a" />
      </body>
      <title>Parameterized Unit Test Patterns (draft)</title>
      <guid isPermaLink="false">http://blog.dotnetwiki.org/PermaLink,guid,8e066c6d-0833-4600-b3d9-9ac9a84f447a.aspx</guid>
      <link>http://blog.dotnetwiki.org/2008/09/22/ParameterizedUnitTestPatternsDraft.aspx</link>
      <pubDate>Mon, 22 Sep 2008 22:41:15 GMT</pubDate>
      <description>&lt;blockquote&gt; 
&lt;p&gt;
&lt;em&gt;How do you write good &lt;/em&gt;&lt;a target="_blank" href="http://research.microsoft.com/pex"&gt;&lt;em&gt;parameterized
unit tests&lt;/em&gt;&lt;/a&gt;&lt;em&gt;? 
&lt;br&gt;
Where do they work the best? 
&lt;br&gt;
Are there some &lt;strong&gt;Test Patterns&lt;/strong&gt; ? &lt;strong&gt;Anti Patterns&lt;/strong&gt;?&lt;/em&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
This is the kind of questions that we have received many times from &lt;a target="_blank" href="http://research.microsoft.com/pex"&gt;Pex&lt;/a&gt; users.
We just released &lt;a target="_blank" href="http://blogs.msdn.com/nikolait/archive/2008/09/22/pex-0-7-released.aspx"&gt;Pex
0.7&lt;/a&gt; which contains a list patterns and anti-patterns for parameterized unit testing
(this is still a draft but we feel that we already have a number of good patterns
that would be helpful for anyone giving a shot at Pex):
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;direct link:&lt;/strong&gt; &lt;a href="http://research.microsoft.com/pex/articles/pexpatterns.pdf"&gt;&lt;strong&gt;http://research.microsoft.com/pex/articles/pexpatterns.pdf&lt;/strong&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp; 
&lt;li&gt;
&lt;strong&gt;from the installer (0.7 and higher):&lt;/strong&gt; after installing Pex, go to
Start –&amp;gt; All Programs –&amp;gt; Microsoft Pex –&amp;gt; Documentation –&amp;gt; Patterns.pdf&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
Note that most of the patterns in this document are not Pex specific and apply to
parameterized unit tests in general; including &lt;a target="_blank" href="http://www.mbunit.com"&gt;MbUnit&lt;/a&gt; RowTest/CombinatorialTest/DataTest, &lt;a target="_blank" href="http://www.nunit.org"&gt;NUnit&lt;/a&gt; RowTest,
MSTest Data Test, etc…
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;The amazing ‘quadruple A’ pattern&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
The ‘triple A’ pattern is a common way of writing a unit test: Arrange, Act, Assert.
Even more ‘A’crobatic, we propose the ‘quadruple A’ where we added one more ‘A’ for &lt;em&gt;assumption&lt;/em&gt;:
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.dotnetwiki.org/images/ParameterizedUnitTestPatternsdraft_7F53/image.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.dotnetwiki.org/images/ParameterizedUnitTestPatternsdraft_7F53/image_thumb.png" width="571" height="618"&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
Pex is an automated white box testing tool from Microsoft Research. 
&lt;br&gt;
More information at &lt;a href="http://research.microsoft.com/pex"&gt;http://research.microsoft.com/pex&lt;/a&gt;.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=8e066c6d-0833-4600-b3d9-9ac9a84f447a" /&gt;</description>
      <comments>http://blog.dotnetwiki.org/CommentView,guid,8e066c6d-0833-4600-b3d9-9ac9a84f447a.aspx</comments>
      <category>MbUnit</category>
      <category>Pex</category>
      <category>Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.dotnetwiki.org/Trackback.aspx?guid=1b8caf90-5909-4516-bfc4-8b3c0369ad88</trackback:ping>
      <pingback:server>http://blog.dotnetwiki.org/pingback.aspx</pingback:server>
      <pingback:target>http://blog.dotnetwiki.org/PermaLink,guid,1b8caf90-5909-4516-bfc4-8b3c0369ad88.aspx</pingback:target>
      <dc:creator>Jonathan de Halleux</dc:creator>
      <wfw:comment>http://blog.dotnetwiki.org/CommentView,guid,1b8caf90-5909-4516-bfc4-8b3c0369ad88.aspx</wfw:comment>
      <wfw:commentRss>http://blog.dotnetwiki.org/SyndicationService.asmx/GetEntryCommentsRss?guid=1b8caf90-5909-4516-bfc4-8b3c0369ad88</wfw:commentRss>
      <slash:comments>6</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
In the <a href="http://blog.dotnetwiki.org/TDDingABinaryHeapWithPexPart1.aspx">previous
post</a>, we implemented the insertion method of a binary heap using <a target="_blank" href="http://en.wikipedia.org/wiki/Test_driven_development">Test
Driven Development</a> (TDD) and <a target="_blank" href="http://research.microsoft.com/pex">parameterized
unit tests</a> (I'll leave the full implementation of the insertion method as an exercise).
</p>
        <p>
In this post, we will take a closer look at the development flow that we used and
show how it relates to traditional TDD. For many people, combining TDD and automated
test generation makes no sense. I believe this is not true anymore, and this is what
this post is about.
</p>
        <p>
          <strong>Test Driven Development Flow</strong>
        </p>
        <p>
TDD has a well-defined flow where developers
</p>
        <ol>
          <li>
write a unit test, 
</li>
          <li>
run the test and watch it fail, 
</li>
          <li>
fix the code, 
</li>
          <li>
run the test and watch the test pass. Start again (I'll skip refactoring in this discussion)</li>
        </ol>
        <p>
During this flow, practitioners also refer to the <strong>green state</strong> when
all tests are passing and <strong>red state</strong> when some tests are failing.
The little picture below depicts this little state machine.
</p>
        <p>
          <a href="http://blog.dotnetwiki.org/images/TDDingaBinaryHeapwithPexpart2_55CE/tddstates.png">
            <img style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" border="0" alt="tddstates" src="http://blog.dotnetwiki.org/images/TDDingaBinaryHeapwithPexpart2_55CE/tddstates_thumb.png" width="404" height="207" />
          </a>
        </p>
        <p>
          <strong>A key aspect of this approach is that the design of the API is inferred by
writing the 'scenarios', i.e. tests.</strong> Therefore, <strong>unit tests</strong> are
a critical building block of the TDD flow.
</p>
        <p>
Note also that <a target="_blank" href="http://en.wikipedia.org/wiki/XUnit">xUnit</a>-like
test frameworks (pick your favorite framework here) provide the automation tools so
that the execution of the test and the investigation of a failure is painless for
the programmer.
</p>
        <p>
          <strong>Test Driven Development Flow With Parameterized Unit Tests</strong>
        </p>
        <p>
Parameterized unit tests and Pex change the TDD flow while retaining its <em>essence</em>:
building the design from tests. Here are the steps, we'll discuss them in detail later
on:
</p>
        <ol>
          <li>
Write a <strong>parameterized</strong> unit test, 
</li>
          <li>
Run Pex and watch some <strong>generated</strong> unit tests fail, 
</li>
          <li>
Fix the code, 
</li>
          <li>
Run Pex again, 
<ol><li>
Some previously generated unit tests now pass, at least one new failing unit test
get generated (go to 3) 
</li><li>
All generated tests are passing, start again (go to 1)</li></ol></li>
        </ol>
        <p>
          <strong>The key difference is the shortcut from step 4 (generating unit tests) to
3 (fix the code), without passing through step 1 (write a new test).</strong> This
is illustrated by the yellow feedback loop in the diagram below:
</p>
        <p>
 <a href="http://blog.dotnetwiki.org/images/TDDingaBinaryHeapwithPexpart2_55CE/etddcycle.png"><img style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" border="0" alt="etddcycle" src="http://blog.dotnetwiki.org/images/TDDingaBinaryHeapwithPexpart2_55CE/etddcycle_thumb.png" width="504" height="303" /></a></p>
        <p>
To the risk of repeating myself, let me emphasize some important points here:
</p>
        <ul>
          <li>
            <strong>you still need to write unit tests, it's just that they can have parameters: </strong>Pex
generates unit tests <em>from</em> parameterized unit tests, by instantiating them.
The person who writes the parameterized unit test is you, not the tool! 
</li>
          <li>
            <strong>it is still about design</strong>: using parameterized unit tests is as much
about design as closed (i.e. parameterless) unit tests. In fact, one can argue that
parameterized unit tests are way closer to a <strong>specification</strong> that closed
unit tests. 
</li>
          <li>
            <strong>it is test first</strong>: in case it was not obvious by now :)</li>
        </ul>
        <p>
          <strong>The shortcut from 4 to 3</strong>
        </p>
        <p>
As mentioned above, the main difference in the flow is that jump from running the
tool (step 4) to fixing the code (step 3), without writing new tests (step 1). This
happens because a parameterized unit tests captures <strong>equivalence classes </strong>rather
than a single scenario like closed unit tests. As a result,
</p>
        <ul>
          <li>
            <strong>you spend more time fixing/implementing the code</strong>: the nice thing
about the shortcut is that you can spend more time writing the code, rather than writing
tests. 
</li>
          <li>
            <strong>you can leverage automated white-box testing tools</strong>: Pex tries to
get the maximum coverage out of your parameterized unit test (note remember that getting
coverage also means covering the throwing branches in Assert), using an automated
white-box code analysis. Now that you have also those manycore CPUs on your motherboard,
you can finally make a good use of them :)</li>
        </ul>
        <p>
          <strong>To Pex <em>and</em> not to Pex</strong>
        </p>
        <p>
An important aspect of parameterized unit tests (and tools like Pex) is that you do <strong>not </strong>have
to drop (completely) your existing habits: in many cases, it is easier to write closed
unit tests! In fact, you can always start from a closed unit test and refactor it
later. We do not expect users to parameterized unit tests write exclusively (<a target="_blank" href="http://dspace.mit.edu/bitstream/handle/1721.1/40090/MIT-CSAIL-TR-2008-002.pdf">another
nice read here</a>), but when you do write them, we expect you'll get much more 'out
of your buck'.
</p>
        <p>
In future posts, we'll discuss different ways to write parameterized unit tests: from
refactoring existing unit tests to using test patterns.
</p>
        <p>
          <strong>To be continued...</strong>
        </p>
        <p>
Next time, we'll go back to the heap and look at implementing the 'remove minimum'
method...
</p>
        <p>
(Pex is a automated structural testing tool from Microsoft Research. More information
at <a href="http://research.microsoft.com/pex">http://research.microsoft.com/pex</a>.)
</p>
        <img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=1b8caf90-5909-4516-bfc4-8b3c0369ad88" />
      </body>
      <title>Test Driven Development With Parameterized Unit Tests</title>
      <guid isPermaLink="false">http://blog.dotnetwiki.org/PermaLink,guid,1b8caf90-5909-4516-bfc4-8b3c0369ad88.aspx</guid>
      <link>http://blog.dotnetwiki.org/2008/09/18/TestDrivenDevelopmentWithParameterizedUnitTests.aspx</link>
      <pubDate>Thu, 18 Sep 2008 00:15:26 GMT</pubDate>
      <description>&lt;p&gt;
In the &lt;a href="http://blog.dotnetwiki.org/TDDingABinaryHeapWithPexPart1.aspx"&gt;previous
post&lt;/a&gt;, we implemented the insertion method of a binary heap using &lt;a target="_blank" href="http://en.wikipedia.org/wiki/Test_driven_development"&gt;Test
Driven Development&lt;/a&gt; (TDD) and &lt;a target="_blank" href="http://research.microsoft.com/pex"&gt;parameterized
unit tests&lt;/a&gt; (I'll leave the full implementation of the insertion method as an exercise).
&lt;/p&gt;
&lt;p&gt;
In this post, we will take a closer look at the development flow that we used and
show how it relates to traditional TDD. For many people, combining TDD and automated
test generation makes no sense. I believe this is not true anymore, and this is what
this post is about.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Test Driven Development Flow&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
TDD has a well-defined flow where developers
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
write a unit test, 
&lt;li&gt;
run the test and watch it fail, 
&lt;li&gt;
fix the code, 
&lt;li&gt;
run the test and watch the test pass. Start again (I'll skip refactoring in this discussion)&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
During this flow, practitioners also refer to the &lt;strong&gt;green state&lt;/strong&gt; when
all tests are passing and &lt;strong&gt;red state&lt;/strong&gt; when some tests are failing.
The little picture below depicts this little state machine.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.dotnetwiki.org/images/TDDingaBinaryHeapwithPexpart2_55CE/tddstates.png"&gt;&lt;img style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" border="0" alt="tddstates" src="http://blog.dotnetwiki.org/images/TDDingaBinaryHeapwithPexpart2_55CE/tddstates_thumb.png" width="404" height="207"&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;A key aspect of this approach is that the design of the API is inferred by
writing the 'scenarios', i.e. tests.&lt;/strong&gt; Therefore, &lt;strong&gt;unit tests&lt;/strong&gt; are
a critical building block of the TDD flow.
&lt;/p&gt;
&lt;p&gt;
Note also that &lt;a target="_blank" href="http://en.wikipedia.org/wiki/XUnit"&gt;xUnit&lt;/a&gt;-like
test frameworks (pick your favorite framework here) provide the automation tools so
that the execution of the test and the investigation of a failure is painless for
the programmer.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Test Driven Development Flow With Parameterized Unit Tests&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
Parameterized unit tests and Pex change the TDD flow while retaining its &lt;em&gt;essence&lt;/em&gt;:
building the design from tests. Here are the steps, we'll discuss them in detail later
on:
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
Write a &lt;strong&gt;parameterized&lt;/strong&gt; unit test, 
&lt;li&gt;
Run Pex and watch some &lt;strong&gt;generated&lt;/strong&gt; unit tests fail, 
&lt;li&gt;
Fix the code, 
&lt;li&gt;
Run Pex again, 
&lt;ol&gt;
&lt;li&gt;
Some previously generated unit tests now pass, at least one new failing unit test
get generated (go to 3) 
&lt;li&gt;
All generated tests are passing, start again (go to 1)&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
&lt;strong&gt;The key difference is the shortcut from step 4 (generating unit tests) to
3 (fix the code), without passing through step 1 (write a new test).&lt;/strong&gt; This
is illustrated by the yellow feedback loop in the diagram below:
&lt;/p&gt;
&lt;p&gt;
&amp;nbsp;&lt;a href="http://blog.dotnetwiki.org/images/TDDingaBinaryHeapwithPexpart2_55CE/etddcycle.png"&gt;&lt;img style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" border="0" alt="etddcycle" src="http://blog.dotnetwiki.org/images/TDDingaBinaryHeapwithPexpart2_55CE/etddcycle_thumb.png" width="504" height="303"&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
To the risk of repeating myself, let me emphasize some important points here:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;you still need to write unit tests, it's just that they can have parameters: &lt;/strong&gt;Pex
generates unit tests &lt;em&gt;from&lt;/em&gt; parameterized unit tests, by instantiating them.
The person who writes the parameterized unit test is you, not the tool! 
&lt;li&gt;
&lt;strong&gt;it is still about design&lt;/strong&gt;: using parameterized unit tests is as much
about design as closed (i.e. parameterless) unit tests. In fact, one can argue that
parameterized unit tests are way closer to a &lt;strong&gt;specification&lt;/strong&gt; that closed
unit tests. 
&lt;li&gt;
&lt;strong&gt;it is test first&lt;/strong&gt;: in case it was not obvious by now :)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
&lt;strong&gt;The shortcut from 4 to 3&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
As mentioned above, the main difference in the flow is that jump from running the
tool (step 4) to fixing the code (step 3), without writing new tests (step 1). This
happens because a parameterized unit tests captures &lt;strong&gt;equivalence classes &lt;/strong&gt;rather
than a single scenario like closed unit tests. As a result,
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;you spend more time fixing/implementing the code&lt;/strong&gt;: the nice thing
about the shortcut is that you can spend more time writing the code, rather than writing
tests. 
&lt;li&gt;
&lt;strong&gt;you can leverage automated white-box testing tools&lt;/strong&gt;: Pex tries to
get the maximum coverage out of your parameterized unit test (note remember that getting
coverage also means covering the throwing branches in Assert), using an automated
white-box code analysis. Now that you have also those manycore CPUs on your motherboard,
you can finally make a good use of them :)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
&lt;strong&gt;To Pex &lt;em&gt;and&lt;/em&gt; not to Pex&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
An important aspect of parameterized unit tests (and tools like Pex) is that you do &lt;strong&gt;not &lt;/strong&gt;have
to drop (completely) your existing habits: in many cases, it is easier to write closed
unit tests! In fact, you can always start from a closed unit test and refactor it
later. We do not expect users to parameterized unit tests write exclusively (&lt;a target="_blank" href="http://dspace.mit.edu/bitstream/handle/1721.1/40090/MIT-CSAIL-TR-2008-002.pdf"&gt;another
nice read here&lt;/a&gt;), but when you do write them, we expect you'll get much more 'out
of your buck'.
&lt;/p&gt;
&lt;p&gt;
In future posts, we'll discuss different ways to write parameterized unit tests: from
refactoring existing unit tests to using test patterns.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;To be continued...&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
Next time, we'll go back to the heap and look at implementing the 'remove minimum'
method...
&lt;/p&gt;
&lt;p&gt;
(Pex is a automated structural testing tool from Microsoft Research. More information
at &lt;a href="http://research.microsoft.com/pex"&gt;http://research.microsoft.com/pex&lt;/a&gt;.)
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=1b8caf90-5909-4516-bfc4-8b3c0369ad88" /&gt;</description>
      <comments>http://blog.dotnetwiki.org/CommentView,guid,1b8caf90-5909-4516-bfc4-8b3c0369ad88.aspx</comments>
      <category>Pex</category>
      <category>Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.dotnetwiki.org/Trackback.aspx?guid=41f3d19d-98f9-41a6-8df2-810f27ea0034</trackback:ping>
      <pingback:server>http://blog.dotnetwiki.org/pingback.aspx</pingback:server>
      <pingback:target>http://blog.dotnetwiki.org/PermaLink,guid,41f3d19d-98f9-41a6-8df2-810f27ea0034.aspx</pingback:target>
      <dc:creator>Jonathan de Halleux</dc:creator>
      <wfw:comment>http://blog.dotnetwiki.org/CommentView,guid,41f3d19d-98f9-41a6-8df2-810f27ea0034.aspx</wfw:comment>
      <wfw:commentRss>http://blog.dotnetwiki.org/SyndicationService.asmx/GetEntryCommentsRss?guid=41f3d19d-98f9-41a6-8df2-810f27ea0034</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
The other day I stumbled on a first draft on a new book on algorithms (<a href="http://dotnetslackers.com/projects/Data-Structures-And-Algorithms/">Data
Structure and Algorithms</a>). After taking a peak at the draft, I found some (hidden)
motivation to finally write a decent <a href="http://en.wikipedia.org/wiki/Binary_heap">binary
heap</a> for <a href="http://www.codeplex.com/quickgraph">QuickGraph</a>. A heap is
a critical data structure for <a href="http://en.wikipedia.org/wiki/Dijkstra's_algorithm">Dijkstra
shortest path</a> or Prim's minimum spanning tree algorithm, since it is used to build
efficient priority queues.
</p>
        <p>
In this post, we'll start building a binary heap using <a href="http://en.wikipedia.org/wiki/Test_driven_development">Test-Driven
Development</a> (write the tests first, etc...) and <strong><a href="http://research.microsoft.com/pex">parameterized
unit tests</a></strong>. 
</p>
        <p>
          <strong>BinaryHeap?</strong>
        </p>
        <p>
The heap is a tree where each parent node has a value smaller or equal to the child
nodes. The binary heap is a heap implemented through a binary tree, and to make things
more interesting (and fast), the tree is usually mapped to an array using indexing
magic:
</p>
        <ul>
          <li>
parent node index: (index - 1) /2 
</li>
          <li>
left child node: 2*index + 1 
</li>
          <li>
right child node: 2*index + 2</li>
        </ul>
        <p>
The indexing magic is typically the kind of things that introduce bugs. 
</p>
        <p>
          <strong>Let's write that first test</strong>
        </p>
        <p>
We start by writing a test that simply fills <em>any</em> binary heap with a <em>number</em> of
entries. A possible test for this is written as follows:
</p>
        <blockquote>
          <pre class="code">[<span style="color: #2b91af">PexMethod</span>] <span style="color: blue">public
void </span>Insert&lt;TPriority, TValue&gt;( [<span style="color: #2b91af">PexAssumeUnderTest</span>]<span style="color: #2b91af">BinaryHeap</span>&lt;TPriority,
TValue&gt; target, [<span style="color: #2b91af">PexAssumeNotNull</span>] <span style="color: #2b91af">KeyValuePair</span>&lt;TPriority,
TValue&gt;[] kvs) { <span style="color: blue">var </span>count = target.Count; <span style="color: blue">foreach </span>(<span style="color: blue">var </span>kv <span style="color: blue">in </span>kvs)
{ target.Add(kv.Key, kv.Value); AssertInvariant&lt;TPriority, TValue&gt;(target);
} <span style="color: #2b91af">Assert</span>.IsTrue(count + kvs.Length == target.Count);
}</pre>
        </blockquote>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
There are a number of unusual annotations in this test. Let's review all of them:
</p>
        <ul>
          <li>
The test is generic: Pex supports generic unit tests. This is really convenient when
testing a generic type. 
</li>
          <li>
            <span style="color: #2b91af">PexAssumeUnderTest</span>, PexAssumeNotNull: it basically
tells Pex that we don't care about the case where <em>kvs</em> or <em>target</em> is
null. 
</li>
          <li>
We've added a Add(TPriority priority, TValue value) method and Count property to the
BinaryHeap</li>
        </ul>
        <p>
There are also two assertions in the test. Inlined in the loop, we check the <strong>invariant</strong> of
the heap (AssertInvariant). We'll fill up this method as we go. At the end of the
test, we check that the Count property.
</p>
        <p>
          <strong>BinaryHeap version #0</strong>
        </p>
        <p>
We start with a simple (broken) implementation that does nothing:
</p>
        <pre class="code">
          <span style="color: blue">public class </span>
          <span style="color: #2b91af">BinaryHeap</span>&lt;TPriority,
TValue&gt; { <span style="color: blue">public void </span>Add(TPriority priority,
TValue value) { } <span style="color: blue">public int </span>Count { <span style="color: blue">returnt
0;</span> }<br />
}</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
Now that the code compiles, we run Pex which quickly finds that a non-empty array
breaks the count assertion.
</p>
        <p>
          <a href="http://blog.dotnetwiki.org/images/TDDingaBinaryHeapwithPex_F3A0/image.png">
            <img style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" border="0" alt="image" src="http://blog.dotnetwiki.org/images/TDDingaBinaryHeapwithPex_F3A0/image_thumb.png" width="674" height="123" />
          </a>
        </p>
        <p>
          <strong>BinaryHeap version #1</strong>
        </p>
        <p>
We have a failing test so let's fix the code by storing the items in a list:
</p>
        <pre class="code">
          <span style="color: blue">public class </span>
          <span style="color: #2b91af">BinaryHeap</span>&lt;TPriority,
TValue&gt; { <span style="color: #2b91af">List</span>&lt;<span style="color: #2b91af">KeyValuePair</span>&lt;TPriority,
TValue&gt;&gt; items = <span style="color: blue">new </span><span style="color: #2b91af">List</span>&lt;<span style="color: #2b91af">KeyValuePair</span>&lt;TPriority,
TValue&gt;&gt;(); <span style="color: blue">public void </span>Add(TPriority priority,
TValue value) { <span style="color: blue">this</span>.items.Add(<span style="color: blue">new </span><span style="color: #2b91af">KeyValuePair</span>&lt;TPriority,
TValue&gt;(priority, value)); } <span style="color: blue">public int </span>Count
{ <span style="color: blue">get </span>{ <span style="color: blue">return this</span>.items.Count;
} } </pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
We run Pex again and all the previous failing tests are now passing :)
</p>
        <p>
          <a href="http://blog.dotnetwiki.org/images/TDDingaBinaryHeapwithPex_F3A0/image_3.png">
            <img style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" border="0" alt="image" src="http://blog.dotnetwiki.org/images/TDDingaBinaryHeapwithPex_F3A0/image_thumb_3.png" width="470" height="159" />
          </a>
        </p>
        <p>
There's a new <strong>object creation </strong>event that happened (bold events should
be looked at). Remember that the test takes a binary heap as argument, well we probably
need to tell Pex how to instantiate that class. In fact, this is exactly what happens
when I click on the object creation button:
</p>
        <p>
          <a href="http://blog.dotnetwiki.org/images/TDDingaBinaryHeapwithPex_F3A0/image_4.png">
            <img style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" border="0" alt="image" src="http://blog.dotnetwiki.org/images/TDDingaBinaryHeapwithPex_F3A0/image_thumb_4.png" width="644" height="161" />
          </a>
        </p>
        <p>
Pex tells me that it guessed how to create the binary heap instance and gives me the
opportunity to save and edit the factory. The factory looks like this and get included
automatically in the test project:
</p>
        <pre class="code">[<span style="color: #2b91af">PexFactoryClass</span>] <span style="color: blue">public
partial class </span><span style="color: #2b91af">BinaryHeapFactory </span>{ [<span style="color: #2b91af">PexFactoryMethod</span>(<span style="color: blue">typeof</span>(<span style="color: #2b91af">BinaryHeap</span>&lt;<span style="color: blue">int</span>, <span style="color: blue">int</span>&gt;))] <span style="color: blue">public
static </span><span style="color: #2b91af">BinaryHeap</span>&lt;<span style="color: blue">int</span>, <span style="color: blue">int</span>&gt;
Create() { <span style="color: #2b91af">BinaryHeap</span>&lt;<span style="color: blue">int</span>, <span style="color: blue">int</span>&gt;
binaryHeap = <span style="color: blue">new </span><span style="color: #2b91af">BinaryHeap</span>&lt;<span style="color: blue">int</span>, <span style="color: blue">int</span>&gt;(); <span style="color: blue">return </span>binaryHeap; <span style="color: green"></span>}
}</pre>
        <p>
All our tests are passing, we can write the next test... but wait!
</p>
        <p>
          <strong>We have an invariant!</strong>
        </p>
        <p>
The nice thing about data structures is that they have fairly well defined <a href="http://en.wikipedia.org/wiki/Object_invariant">invariants</a>.
These are very useful for testing! 
</p>
        <p>
In the case of the heap, we know that the parent node priority should always be less
or equal to the priorities of both of his left and right children. Therefore, we can
add a method to BinaryHeap that walks the array and checks this property on each node:
</p>
        <blockquote>
          <pre class="code">[<span style="color: #2b91af">Conditional</span>(<span style="color: #a31515">"DEBUG"</span>)] <span style="color: blue">public
void </span>ObjectInvariant() { <span style="color: blue">for </span>(<span style="color: blue">int </span>index
= 0; index &lt; <span style="color: blue">this</span>.count; ++index) { <span style="color: blue">var </span>left
= 2 * index + 1; <span style="color: #2b91af">Debug</span>.Assert(left &gt;= count
|| <span style="color: blue">this</span>.Less(index, left)); <span style="color: blue">var </span>right
= 2 * index + 2; <span style="color: #2b91af">Debug</span>.Assert(right &gt;= count
|| <span style="color: blue">this</span>.Less(index, right)); } }<br /><span style="color: blue">private bool </span>Less(<span style="color: blue">int </span>i, <span style="color: blue">int </span>j)
{ <span style="color: blue">return false</span>; }</pre>
          <a href="http://11011.net/software/vspaste">
          </a>
        </blockquote>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
Remember that AssertInvariant method, let's call ObjectInvariant in that method and
run Pex again.
</p>
        <pre class="code">
          <span style="color: blue">void </span>AssertInvariant&lt;TPriority,TValue&gt;(<span style="color: #2b91af">BinaryHeap</span>&lt;TPriority,TValue&gt;
target) { target.ObjectInvariant(); }</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
Pex immediately finds an issue:
</p>
        <p>
          <a href="http://blog.dotnetwiki.org/images/TDDingaBinaryHeapwithPex_F3A0/image_5.png">
            <img style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" border="0" alt="image" src="http://blog.dotnetwiki.org/images/TDDingaBinaryHeapwithPex_F3A0/image_thumb_5.png" width="382" height="124" />
          </a>
        </p>
        <p>
This assertion is due to our overly simplified implementation of Less, which always
return false. 
</p>
        <p>
          <strong>Fixing tests, and finding new failing tests</strong>
        </p>
        <p>
We have failing tests so it's time to fix the code again. Let's start fixing on the
Less method by using a comparer:
</p>
        <pre class="code">
          <span style="color: blue">readonly </span>
          <span style="color: #2b91af">Comparison</span>&lt;TPriority&gt;
comparison = <span style="color: #2b91af">Comparer</span>&lt;TPriority&gt;.Default.Compare; <span style="color: blue">private
bool </span>Less(<span style="color: blue">int </span>i, <span style="color: blue">int </span>j)
{ <span style="color: blue">return this</span>.comparison(<span style="color: blue">this</span>.items[i].Key, <span style="color: blue">this</span>.items[j].Key)
&gt;= 0; }</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
We run Pex and it comes back with the following tests:
</p>
        <p>
          <a href="http://blog.dotnetwiki.org/images/TDDingaBinaryHeapwithPex_F3A0/image_6.png">
            <img style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" border="0" alt="image" src="http://blog.dotnetwiki.org/images/TDDingaBinaryHeapwithPex_F3A0/image_thumb_6.png" width="473" height="177" />
          </a>
        </p>
        <p>
Two interesting things happened here:
</p>
        <ul>
          <li>
the previous failing test (with [0,0], [0,0]) was fixed by fixing Less 
</li>
          <li>
Pex found a new issue where the input array involves a small key (3584), then a larger
key (4098). A correct heap implementation would have kept the smaller key at the first
position.</li>
        </ul>
        <p>
The coolest part is we did not have to write any additional line of code to get to
this point: Pex updated the previous tests and generated the new failure for us. 
</p>
        <p>
This is a new kind of flow that occurs when using Pex in TDD: a code update has fixed
some issues but created new ones. We are still moving towards our goal but we did
not have to pay the price of writing a new unit test.
</p>
        <p>
In fact, to fulfill the invariant and make this test pass we will have to write a
correct implementation of the Add method.... without writing a single additional line
of test code :)
</p>
        <p>
          <em>to be continued...</em>
        </p>
        <img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=41f3d19d-98f9-41a6-8df2-810f27ea0034" />
      </body>
      <title>TDD'ing a BinaryHeap with Pex (part 1)</title>
      <guid isPermaLink="false">http://blog.dotnetwiki.org/PermaLink,guid,41f3d19d-98f9-41a6-8df2-810f27ea0034.aspx</guid>
      <link>http://blog.dotnetwiki.org/2008/09/09/TDDingABinaryHeapWithPexPart1.aspx</link>
      <pubDate>Tue, 09 Sep 2008 05:39:35 GMT</pubDate>
      <description>&lt;p&gt;
The other day I stumbled on a first draft on a new book on algorithms (&lt;a href="http://dotnetslackers.com/projects/Data-Structures-And-Algorithms/"&gt;Data
Structure and Algorithms&lt;/a&gt;). After taking a peak at the draft, I found some (hidden)
motivation to finally write a decent &lt;a href="http://en.wikipedia.org/wiki/Binary_heap"&gt;binary
heap&lt;/a&gt; for &lt;a href="http://www.codeplex.com/quickgraph"&gt;QuickGraph&lt;/a&gt;. A heap is
a critical data structure for &lt;a href="http://en.wikipedia.org/wiki/Dijkstra's_algorithm"&gt;Dijkstra
shortest path&lt;/a&gt; or Prim's minimum spanning tree algorithm, since it is used to build
efficient priority queues.
&lt;/p&gt;
&lt;p&gt;
In this post, we'll start building a binary heap using &lt;a href="http://en.wikipedia.org/wiki/Test_driven_development"&gt;Test-Driven
Development&lt;/a&gt; (write the tests first, etc...) and &lt;strong&gt;&lt;a href="http://research.microsoft.com/pex"&gt;parameterized
unit tests&lt;/a&gt;&lt;/strong&gt;. 
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;BinaryHeap?&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
The heap is a tree where each parent node has a value smaller or equal to the child
nodes. The binary heap is a heap implemented through a binary tree, and to make things
more interesting (and fast), the tree is usually mapped to an array using indexing
magic:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
parent node index: (index - 1) /2 
&lt;li&gt;
left child node: 2*index + 1 
&lt;li&gt;
right child node: 2*index + 2&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
The indexing magic is typically the kind of things that introduce bugs. 
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Let's write that first test&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
We start by writing a test that simply fills &lt;em&gt;any&lt;/em&gt; binary heap with a &lt;em&gt;number&lt;/em&gt; of
entries. A possible test for this is written as follows:
&lt;/p&gt;
&lt;blockquote&gt;&lt;pre class="code"&gt;[&lt;span style="color: #2b91af"&gt;PexMethod&lt;/span&gt;] &lt;span style="color: blue"&gt;public
void &lt;/span&gt;Insert&amp;lt;TPriority, TValue&amp;gt;( [&lt;span style="color: #2b91af"&gt;PexAssumeUnderTest&lt;/span&gt;]&lt;span style="color: #2b91af"&gt;BinaryHeap&lt;/span&gt;&amp;lt;TPriority,
TValue&amp;gt; target, [&lt;span style="color: #2b91af"&gt;PexAssumeNotNull&lt;/span&gt;] &lt;span style="color: #2b91af"&gt;KeyValuePair&lt;/span&gt;&amp;lt;TPriority,
TValue&amp;gt;[] kvs) { &lt;span style="color: blue"&gt;var &lt;/span&gt;count = target.Count; &lt;span style="color: blue"&gt;foreach &lt;/span&gt;(&lt;span style="color: blue"&gt;var &lt;/span&gt;kv &lt;span style="color: blue"&gt;in &lt;/span&gt;kvs)
{ target.Add(kv.Key, kv.Value); AssertInvariant&amp;lt;TPriority, TValue&amp;gt;(target);
} &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.IsTrue(count + kvs.Length == target.Count);
}&lt;/pre&gt;&lt;/blockquote&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
There are a number of unusual annotations in this test. Let's review all of them:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
The test is generic: Pex supports generic unit tests. This is really convenient when
testing a generic type. 
&lt;li&gt;
&lt;span style="color: #2b91af"&gt;PexAssumeUnderTest&lt;/span&gt;, PexAssumeNotNull: it basically
tells Pex that we don't care about the case where &lt;em&gt;kvs&lt;/em&gt; or &lt;em&gt;target&lt;/em&gt; is
null. 
&lt;li&gt;
We've added a Add(TPriority priority, TValue value) method and Count property to the
BinaryHeap&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
There are also two assertions in the test. Inlined in the loop, we check the &lt;strong&gt;invariant&lt;/strong&gt; of
the heap (AssertInvariant). We'll fill up this method as we go. At the end of the
test, we check that the Count property.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;BinaryHeap version #0&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
We start with a simple (broken) implementation that does nothing:
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;BinaryHeap&lt;/span&gt;&amp;lt;TPriority,
TValue&amp;gt; { &lt;span style="color: blue"&gt;public void &lt;/span&gt;Add(TPriority priority,
TValue value) { } &lt;span style="color: blue"&gt;public int &lt;/span&gt;Count { &lt;span style="color: blue"&gt;returnt
0;&lt;/span&gt; }&lt;br&gt;
}&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
Now that the code compiles, we run Pex which quickly finds that a non-empty array
breaks the count assertion.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.dotnetwiki.org/images/TDDingaBinaryHeapwithPex_F3A0/image.png"&gt;&lt;img style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" border="0" alt="image" src="http://blog.dotnetwiki.org/images/TDDingaBinaryHeapwithPex_F3A0/image_thumb.png" width="674" height="123"&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;BinaryHeap version #1&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
We have a failing test so let's fix the code by storing the items in a list:
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;BinaryHeap&lt;/span&gt;&amp;lt;TPriority,
TValue&amp;gt; { &lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;KeyValuePair&lt;/span&gt;&amp;lt;TPriority,
TValue&amp;gt;&amp;gt; items = &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;KeyValuePair&lt;/span&gt;&amp;lt;TPriority,
TValue&amp;gt;&amp;gt;(); &lt;span style="color: blue"&gt;public void &lt;/span&gt;Add(TPriority priority,
TValue value) { &lt;span style="color: blue"&gt;this&lt;/span&gt;.items.Add(&lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;KeyValuePair&lt;/span&gt;&amp;lt;TPriority,
TValue&amp;gt;(priority, value)); } &lt;span style="color: blue"&gt;public int &lt;/span&gt;Count
{ &lt;span style="color: blue"&gt;get &lt;/span&gt;{ &lt;span style="color: blue"&gt;return this&lt;/span&gt;.items.Count;
} } &lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
We run Pex again and all the previous failing tests are now passing :)
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.dotnetwiki.org/images/TDDingaBinaryHeapwithPex_F3A0/image_3.png"&gt;&lt;img style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" border="0" alt="image" src="http://blog.dotnetwiki.org/images/TDDingaBinaryHeapwithPex_F3A0/image_thumb_3.png" width="470" height="159"&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
There's a new &lt;strong&gt;object creation &lt;/strong&gt;event that happened (bold events should
be looked at). Remember that the test takes a binary heap as argument, well we probably
need to tell Pex how to instantiate that class. In fact, this is exactly what happens
when I click on the object creation button:
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.dotnetwiki.org/images/TDDingaBinaryHeapwithPex_F3A0/image_4.png"&gt;&lt;img style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" border="0" alt="image" src="http://blog.dotnetwiki.org/images/TDDingaBinaryHeapwithPex_F3A0/image_thumb_4.png" width="644" height="161"&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
Pex tells me that it guessed how to create the binary heap instance and gives me the
opportunity to save and edit the factory. The factory looks like this and get included
automatically in the test project:
&lt;/p&gt;
&lt;pre class="code"&gt;[&lt;span style="color: #2b91af"&gt;PexFactoryClass&lt;/span&gt;] &lt;span style="color: blue"&gt;public
partial class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;BinaryHeapFactory &lt;/span&gt;{ [&lt;span style="color: #2b91af"&gt;PexFactoryMethod&lt;/span&gt;(&lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;BinaryHeap&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;, &lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt;))] &lt;span style="color: blue"&gt;public
static &lt;/span&gt;&lt;span style="color: #2b91af"&gt;BinaryHeap&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;, &lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt;
Create() { &lt;span style="color: #2b91af"&gt;BinaryHeap&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;, &lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt;
binaryHeap = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;BinaryHeap&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;, &lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt;(); &lt;span style="color: blue"&gt;return &lt;/span&gt;binaryHeap; &lt;span style="color: green"&gt; &lt;/span&gt;}
}&lt;/pre&gt;
&lt;p&gt;
All our tests are passing, we can write the next test... but wait!
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;We have an invariant!&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
The nice thing about data structures is that they have fairly well defined &lt;a href="http://en.wikipedia.org/wiki/Object_invariant"&gt;invariants&lt;/a&gt;.
These are very useful for testing! 
&lt;/p&gt;
&lt;p&gt;
In the case of the heap, we know that the parent node priority should always be less
or equal to the priorities of both of his left and right children. Therefore, we can
add a method to BinaryHeap that walks the array and checks this property on each node:
&lt;/p&gt;
&lt;blockquote&gt;&lt;pre class="code"&gt;[&lt;span style="color: #2b91af"&gt;Conditional&lt;/span&gt;(&lt;span style="color: #a31515"&gt;"DEBUG"&lt;/span&gt;)] &lt;span style="color: blue"&gt;public
void &lt;/span&gt;ObjectInvariant() { &lt;span style="color: blue"&gt;for &lt;/span&gt;(&lt;span style="color: blue"&gt;int &lt;/span&gt;index
= 0; index &amp;lt; &lt;span style="color: blue"&gt;this&lt;/span&gt;.count; ++index) { &lt;span style="color: blue"&gt;var &lt;/span&gt;left
= 2 * index + 1; &lt;span style="color: #2b91af"&gt;Debug&lt;/span&gt;.Assert(left &amp;gt;= count
|| &lt;span style="color: blue"&gt;this&lt;/span&gt;.Less(index, left)); &lt;span style="color: blue"&gt;var &lt;/span&gt;right
= 2 * index + 2; &lt;span style="color: #2b91af"&gt;Debug&lt;/span&gt;.Assert(right &amp;gt;= count
|| &lt;span style="color: blue"&gt;this&lt;/span&gt;.Less(index, right)); } }&lt;br&gt;
&lt;span style="color: blue"&gt;private bool &lt;/span&gt;Less(&lt;span style="color: blue"&gt;int &lt;/span&gt;i, &lt;span style="color: blue"&gt;int &lt;/span&gt;j)
{ &lt;span style="color: blue"&gt;return false&lt;/span&gt;; }&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/blockquote&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
Remember that AssertInvariant method, let's call ObjectInvariant in that method and
run Pex again.
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;void &lt;/span&gt;AssertInvariant&amp;lt;TPriority,TValue&amp;gt;(&lt;span style="color: #2b91af"&gt;BinaryHeap&lt;/span&gt;&amp;lt;TPriority,TValue&amp;gt;
target) { target.ObjectInvariant(); }&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
Pex immediately finds an issue:
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.dotnetwiki.org/images/TDDingaBinaryHeapwithPex_F3A0/image_5.png"&gt;&lt;img style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" border="0" alt="image" src="http://blog.dotnetwiki.org/images/TDDingaBinaryHeapwithPex_F3A0/image_thumb_5.png" width="382" height="124"&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
This assertion is due to our overly simplified implementation of Less, which always
return false. 
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Fixing tests, and finding new failing tests&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
We have failing tests so it's time to fix the code again. Let's start fixing on the
Less method by using a comparer:
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;readonly &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Comparison&lt;/span&gt;&amp;lt;TPriority&amp;gt;
comparison = &lt;span style="color: #2b91af"&gt;Comparer&lt;/span&gt;&amp;lt;TPriority&amp;gt;.Default.Compare; &lt;span style="color: blue"&gt;private
bool &lt;/span&gt;Less(&lt;span style="color: blue"&gt;int &lt;/span&gt;i, &lt;span style="color: blue"&gt;int &lt;/span&gt;j)
{ &lt;span style="color: blue"&gt;return this&lt;/span&gt;.comparison(&lt;span style="color: blue"&gt;this&lt;/span&gt;.items[i].Key, &lt;span style="color: blue"&gt;this&lt;/span&gt;.items[j].Key)
&amp;gt;= 0; }&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
We run Pex and it comes back with the following tests:
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.dotnetwiki.org/images/TDDingaBinaryHeapwithPex_F3A0/image_6.png"&gt;&lt;img style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" border="0" alt="image" src="http://blog.dotnetwiki.org/images/TDDingaBinaryHeapwithPex_F3A0/image_thumb_6.png" width="473" height="177"&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
Two interesting things happened here:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
the previous failing test (with [0,0], [0,0]) was fixed by fixing Less 
&lt;li&gt;
Pex found a new issue where the input array involves a small key (3584), then a larger
key (4098). A correct heap implementation would have kept the smaller key at the first
position.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
The coolest part is we did not have to write any additional line of code to get to
this point: Pex updated the previous tests and generated the new failure for us. 
&lt;/p&gt;
&lt;p&gt;
This is a new kind of flow that occurs when using Pex in TDD: a code update has fixed
some issues but created new ones. We are still moving towards our goal but we did
not have to pay the price of writing a new unit test.
&lt;/p&gt;
&lt;p&gt;
In fact, to fulfill the invariant and make this test pass we will have to write a
correct implementation of the Add method.... without writing a single additional line
of test code :)
&lt;/p&gt;
&lt;p&gt;
&lt;em&gt;to be continued...&lt;/em&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=41f3d19d-98f9-41a6-8df2-810f27ea0034" /&gt;</description>
      <comments>http://blog.dotnetwiki.org/CommentView,guid,41f3d19d-98f9-41a6-8df2-810f27ea0034.aspx</comments>
      <category>Fun with graphs</category>
      <category>Pex</category>
      <category>Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.dotnetwiki.org/Trackback.aspx?guid=051b3680-5e01-4e76-bcf5-2985faadf9f4</trackback:ping>
      <pingback:server>http://blog.dotnetwiki.org/pingback.aspx</pingback:server>
      <pingback:target>http://blog.dotnetwiki.org/PermaLink,guid,051b3680-5e01-4e76-bcf5-2985faadf9f4.aspx</pingback:target>
      <dc:creator>Jonathan de Halleux</dc:creator>
      <wfw:comment>http://blog.dotnetwiki.org/CommentView,guid,051b3680-5e01-4e76-bcf5-2985faadf9f4.aspx</wfw:comment>
      <wfw:commentRss>http://blog.dotnetwiki.org/SyndicationService.asmx/GetEntryCommentsRss?guid=051b3680-5e01-4e76-bcf5-2985faadf9f4</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
          <em>Update: renamed project to YUnit to avoid clashing with other frameworks.</em>
        </p>
        <p>
I've been playing with <a href="http://msdn.microsoft.com/en-us/library/bb165914(VS.80).aspx">custom
test types for team test</a> lately and the result of this experiment is <strong>YUnit</strong>.
A microscopic test framework that lets you write tests anywhere since it only uses
the <em>Conditional</em> attribute. That's right, any public static parameterless
method tagged with [Conditional("TEST")] becomes a test :)
</p>
        <p>
        </p>
        <p>
          <a href="http://code.msdn.microsoft.com/punit">
          </a>
        </p>
        <img src="http://blog.dotnetwiki.org/content/binary/punitscreenshot.png" border="0" />
        <p>
        </p>
        <p>
If you've always dreamed of implementing your own custom test type, this sample could
be helpful to you. <em>Remember that this is only a sample and comes with no guarantees
of support</em>.
</p>
        <p>
Sources and installer available at: <a href="http://code.msdn.microsoft.com/yunit">http://code.msdn.microsoft.com/yunit</a>. 
</p>
        <p>
 
</p>
        <img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=051b3680-5e01-4e76-bcf5-2985faadf9f4" />
      </body>
      <title>Introducing YUnit, a microscopic unit test framework (for Team Test)</title>
      <guid isPermaLink="false">http://blog.dotnetwiki.org/PermaLink,guid,051b3680-5e01-4e76-bcf5-2985faadf9f4.aspx</guid>
      <link>http://blog.dotnetwiki.org/2008/08/27/IntroducingYUnitAMicroscopicUnitTestFrameworkForTeamTest.aspx</link>
      <pubDate>Wed, 27 Aug 2008 03:26:36 GMT</pubDate>
      <description>&lt;p&gt;
&lt;em&gt;Update: renamed project to YUnit to avoid clashing with other frameworks.&lt;/em&gt;
&lt;/p&gt;
&lt;p&gt;
I've been playing with &lt;a href="http://msdn.microsoft.com/en-us/library/bb165914(VS.80).aspx"&gt;custom
test types for team test&lt;/a&gt; lately and the result of this experiment is &lt;strong&gt;YUnit&lt;/strong&gt;.
A microscopic test framework that lets you write tests anywhere since it only uses
the &lt;em&gt;Conditional&lt;/em&gt; attribute. That's right, any public static parameterless
method tagged with [Conditional("TEST")] becomes a test :)
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;
&lt;a href="http://code.msdn.microsoft.com/punit"&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;img src="http://blog.dotnetwiki.org/content/binary/punitscreenshot.png" border=0&gt; 
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
If you've always dreamed of implementing your own custom test type, this sample could
be helpful to you. &lt;em&gt;Remember that this is only a sample and comes with no guarantees
of support&lt;/em&gt;.
&lt;/p&gt;
&lt;p&gt;
Sources and installer available at: &lt;a href="http://code.msdn.microsoft.com/yunit"&gt;http://code.msdn.microsoft.com/yunit&lt;/a&gt;. 
&lt;/p&gt;
&lt;p&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=051b3680-5e01-4e76-bcf5-2985faadf9f4" /&gt;</description>
      <comments>http://blog.dotnetwiki.org/CommentView,guid,051b3680-5e01-4e76-bcf5-2985faadf9f4.aspx</comments>
      <category>MbUnit</category>
      <category>Pex</category>
      <category>Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.dotnetwiki.org/Trackback.aspx?guid=feb859b8-fce0-4d5a-80b9-e2209c92dbae</trackback:ping>
      <pingback:server>http://blog.dotnetwiki.org/pingback.aspx</pingback:server>
      <pingback:target>http://blog.dotnetwiki.org/PermaLink,guid,feb859b8-fce0-4d5a-80b9-e2209c92dbae.aspx</pingback:target>
      <dc:creator>Jonathan de Halleux</dc:creator>
      <wfw:comment>http://blog.dotnetwiki.org/CommentView,guid,feb859b8-fce0-4d5a-80b9-e2209c92dbae.aspx</wfw:comment>
      <wfw:commentRss>http://blog.dotnetwiki.org/SyndicationService.asmx/GetEntryCommentsRss?guid=feb859b8-fce0-4d5a-80b9-e2209c92dbae</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Read on <a href="http://blogs.msdn.com/nikolait/archive/2008/08/06/pex-0-6-released.aspx" target="_blank">Nikolai's
announcement</a> on the latest drop of Pex.
</p>
        <img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=feb859b8-fce0-4d5a-80b9-e2209c92dbae" />
      </body>
      <title>Pex 0.6 Released</title>
      <guid isPermaLink="false">http://blog.dotnetwiki.org/PermaLink,guid,feb859b8-fce0-4d5a-80b9-e2209c92dbae.aspx</guid>
      <link>http://blog.dotnetwiki.org/2008/08/06/Pex06Released.aspx</link>
      <pubDate>Wed, 06 Aug 2008 18:06:59 GMT</pubDate>
      <description>&lt;p&gt;
Read on &lt;a href="http://blogs.msdn.com/nikolait/archive/2008/08/06/pex-0-6-released.aspx" target="_blank"&gt;Nikolai's
announcement&lt;/a&gt; on the latest drop of Pex.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=feb859b8-fce0-4d5a-80b9-e2209c92dbae" /&gt;</description>
      <comments>http://blog.dotnetwiki.org/CommentView,guid,feb859b8-fce0-4d5a-80b9-e2209c92dbae.aspx</comments>
      <category>Pex</category>
      <category>Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.dotnetwiki.org/Trackback.aspx?guid=65d567e5-a2ce-44dd-9934-b67c72864bd8</trackback:ping>
      <pingback:server>http://blog.dotnetwiki.org/pingback.aspx</pingback:server>
      <pingback:target>http://blog.dotnetwiki.org/PermaLink,guid,65d567e5-a2ce-44dd-9934-b67c72864bd8.aspx</pingback:target>
      <dc:creator>Jonathan de Halleux</dc:creator>
      <wfw:comment>http://blog.dotnetwiki.org/CommentView,guid,65d567e5-a2ce-44dd-9934-b67c72864bd8.aspx</wfw:comment>
      <wfw:commentRss>http://blog.dotnetwiki.org/SyndicationService.asmx/GetEntryCommentsRss?guid=65d567e5-a2ce-44dd-9934-b67c72864bd8</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
          <a href="http://appdevchronicles.blogspot.com/">Alexander Nowak</a> has started a
blog post chronicle on <a href="http://research.microsoft.com/pex">Pex</a> and already
has 6 episodes to it!
</p>
        <li>
          <a href="http://appdevchronicles.blogspot.com/2008/07/pex-test-case-5.html">Pex -
test Case 5</a> (regular expressions) 
</li>
        <li>
          <a href="http://appdevchronicles.blogspot.com/2008/07/pex-test-case-4.html">Pex -
test case 4</a> (strings and parameter validation) 
</li>
        <li>
          <a href="http://appdevchronicles.blogspot.com/2008/07/pex-test-case-3.html">Pex -
Test case 3</a> (enums and business rules validation) 
</li>
        <li>
          <a href="http://appdevchronicles.blogspot.com/2008/07/pex-test-case-2.html">Pex -
test case 2</a>  
</li>
        <li>
          <a href="http://appdevchronicles.blogspot.com/2008/07/pex-test-case-1.html">Pex -
test case 1</a>
        </li>
        <li>
          <a href="http://appdevchronicles.blogspot.com/2008/07/starting-with-pex-program-exploration.html">Starting
with Pex (Program Exploration)</a>
          <p>
The posts give a nice point of view of Pex from a user perspective, and against classic
testing techniques such as equivalence classes. 
</p>
        </li>
        <img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=65d567e5-a2ce-44dd-9934-b67c72864bd8" />
      </body>
      <title>Blog watch: Pex series</title>
      <guid isPermaLink="false">http://blog.dotnetwiki.org/PermaLink,guid,65d567e5-a2ce-44dd-9934-b67c72864bd8.aspx</guid>
      <link>http://blog.dotnetwiki.org/2008/07/31/BlogWatchPexSeries.aspx</link>
      <pubDate>Thu, 31 Jul 2008 14:25:49 GMT</pubDate>
      <description>&lt;p&gt;
&lt;a href="http://appdevchronicles.blogspot.com/"&gt;Alexander Nowak&lt;/a&gt; has started a
blog post chronicle on &lt;a href="http://research.microsoft.com/pex"&gt;Pex&lt;/a&gt; and already
has 6 episodes to it!
&lt;/p&gt;
&lt;li&gt;
&lt;a href="http://appdevchronicles.blogspot.com/2008/07/pex-test-case-5.html"&gt;Pex -
test Case 5&lt;/a&gt; (regular expressions) 
&lt;li&gt;
&lt;a href="http://appdevchronicles.blogspot.com/2008/07/pex-test-case-4.html"&gt;Pex -
test case 4&lt;/a&gt; (strings and parameter validation) 
&lt;li&gt;
&lt;a href="http://appdevchronicles.blogspot.com/2008/07/pex-test-case-3.html"&gt;Pex -
Test case 3&lt;/a&gt; (enums and business rules validation) 
&lt;li&gt;
&lt;a href="http://appdevchronicles.blogspot.com/2008/07/pex-test-case-2.html"&gt;Pex -
test case 2&lt;/a&gt;&amp;nbsp; 
&lt;li&gt;
&lt;a href="http://appdevchronicles.blogspot.com/2008/07/pex-test-case-1.html"&gt;Pex -
test case 1&lt;/a&gt; 
&lt;li&gt;
&lt;a href="http://appdevchronicles.blogspot.com/2008/07/starting-with-pex-program-exploration.html"&gt;Starting
with Pex (Program Exploration)&lt;/a&gt; 
&lt;p&gt;
The posts give a nice point of view of Pex from a user perspective, and against classic
testing techniques such as equivalence classes. 
&lt;/p&gt;
&lt;/li&gt;&lt;img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=65d567e5-a2ce-44dd-9934-b67c72864bd8" /&gt;</description>
      <comments>http://blog.dotnetwiki.org/CommentView,guid,65d567e5-a2ce-44dd-9934-b67c72864bd8.aspx</comments>
      <category>Pex</category>
      <category>Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.dotnetwiki.org/Trackback.aspx?guid=b450c6bb-681d-4b7e-9012-318146cc39a1</trackback:ping>
      <pingback:server>http://blog.dotnetwiki.org/pingback.aspx</pingback:server>
      <pingback:target>http://blog.dotnetwiki.org/PermaLink,guid,b450c6bb-681d-4b7e-9012-318146cc39a1.aspx</pingback:target>
      <dc:creator>Jonathan de Halleux</dc:creator>
      <wfw:comment>http://blog.dotnetwiki.org/CommentView,guid,b450c6bb-681d-4b7e-9012-318146cc39a1.aspx</wfw:comment>
      <wfw:commentRss>http://blog.dotnetwiki.org/SyndicationService.asmx/GetEntryCommentsRss?guid=b450c6bb-681d-4b7e-9012-318146cc39a1</wfw:commentRss>
      <slash:comments>2</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
          <a href="http://en.wikipedia.org/wiki/Linear_programming" target="_blank">Linear programming</a> problems
are usually solved using the <a href="http://en.wikipedia.org/wiki/Simplex_algorithm" target="_blank">simplex
algorithm</a>. While it is easy to encode a constraint system of linear equalities
and inequalities as a Parameterized Unit Test for <a href="http://research.microsoft.com/pex">Pex</a>,
there is currently no way to tell Pex that we want test inputs that are “minimal”
according to a custom objective function. However, Pex can still generate <strong>*surprising*</strong> feasible
solutions.
</p>
        <p>
Let's start with a simple set of linear inequalities that define our problem.
</p>
        <blockquote>
          <pre class="code">[<span style="color: #2b91af">PexMethod</span>] <span style="color: blue">public
int </span>Test(<span style="color: blue">int </span>x, <span style="color: blue">int </span>y)
{<br />
// PexAssume is used to add 'constraints' on the input<br />
// in this case, we simply encode the inequalities in a boolean formula <span style="color: #2b91af">PexAssume</span>.IsTrue(
x + y &lt; 10 &amp; // using bitwise &amp; to avoid introducing branches 5 * x + 2
* y &gt; 20 &amp; -x + 2 * y &gt; 0 &amp; x &gt; 0 &amp; y &gt; 0);<br />
// the profit is returned so that it is automatically logged by Pex <span style="color: blue">return </span>x
+ 4 * y; }</pre>
        </blockquote>
        <a href="http://11011.net/software/vspaste">
        </a>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
After running <strong>Pex</strong>, we get one feasible solution. It is not optimal
as expected since we don't apply the simplex algorithm.
</p>
        <blockquote>
          <p>
            <a href="http://blog.dotnetwiki.org/images/OverflowingLinearProgramming_EC9D/image.png">
              <img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" border="0" alt="image" src="http://blog.dotnetwiki.org/images/OverflowingLinearProgramming_EC9D/image_thumb.png" width="273" height="101" />
            </a>
          </p>
        </blockquote>
        <p>
          <strong>Enter overflows</strong>
        </p>
        <p>
Remember that .Net arithmetic operations will silently overflow unless you execute
them in a checked context? Let's push our luck and try to force an overflow by changing <strong>x
&gt; 0</strong> constraint to <strong>x &gt; 1000</strong>:
</p>
        <blockquote>
          <pre class="code">[<span style="color: #2b91af">PexMethod</span>] <span style="color: blue">public
int </span>Test(<span style="color: blue">int </span>x, <span style="color: blue">int </span>y)
{ <span style="color: #2b91af">PexAssume</span>.IsTrue( x + y &lt; 10 &amp; 5 * x
+ 2 * y &gt; 20 &amp; -x + 2 * y &gt; 0 &amp; <strong>x &gt; 1000 </strong>&amp; y
&gt; 0 ); <span style="color: blue">return </span>x + 4 * y; }</pre>
        </blockquote>
        <p>
          <a href="http://research.microsoft.com/projects/Z3" target="_blank">Z3</a>, the <a href="http://en.wikipedia.org/wiki/SMT_solver" target="_blank">constraint
solver</a> that Pex uses to compute new test inputs, uses <strong>bitvector arithmetic</strong> to
find a surprising solution that fulfills all the inequalities (our profit has just
gone of the roof :)).
</p>
        <blockquote>
          <p>
            <a href="http://blog.dotnetwiki.org/images/OverflowingLinearProgramming_EC9D/image_3.png">
              <img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" border="0" alt="image" src="http://blog.dotnetwiki.org/images/OverflowingLinearProgramming_EC9D/image_thumb_3.png" width="290" height="105" />
            </a> 
</p>
        </blockquote>
        <p>
Z3 is truly <a href="http://www.smtexec.org/exec/" target="_blank">an astonishing
tool</a>!
</p>
        <p>
          <strong>Checked context</strong>
        </p>
        <p>
In order to avoid overflow, one should use a checked context. Let's update the parameterized
unit test:
</p>
        <blockquote>
          <pre class="code">[<span style="color: #2b91af">PexMethod</span>] <span style="color: blue">public
int </span>Test(<span style="color: blue">int </span>x, <span style="color: blue">int </span>y)
{ <strong></strong><strong><span style="color: blue">checked </span>{ </strong><span style="color: #2b91af">PexAssume</span>.IsTrue(
x + y &lt; 10 &amp; 5 * x + 2 * y &gt; 20 &amp; -x + 2 * y &gt; 0 &amp; x &gt; 0 &amp;
y &gt; 0 ); <strong> } </strong><span style="color: blue">return </span>x + 4 * y;
}</pre>
        </blockquote>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
In fact, in that case, Pex generates 2 test cases. One test that passes and the other
test that triggers an OverflowException (implicit branch).
</p>
        <blockquote>
          <p>
            <a href="http://blog.dotnetwiki.org/images/OverflowingLinearProgramming_EC9D/image_4.png">
              <img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" border="0" alt="image" src="http://blog.dotnetwiki.org/images/OverflowingLinearProgramming_EC9D/image_thumb_4.png" width="416" height="158" />
            </a>
          </p>
        </blockquote>
        <p>
Stay tuned for more surprising discoveries using Pex.
</p>
        <img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=b450c6bb-681d-4b7e-9012-318146cc39a1" />
      </body>
      <title>Overflowing Linear Programming</title>
      <guid isPermaLink="false">http://blog.dotnetwiki.org/PermaLink,guid,b450c6bb-681d-4b7e-9012-318146cc39a1.aspx</guid>
      <link>http://blog.dotnetwiki.org/2008/07/30/OverflowingLinearProgramming.aspx</link>
      <pubDate>Wed, 30 Jul 2008 01:35:31 GMT</pubDate>
      <description>&lt;p&gt;
&lt;a href="http://en.wikipedia.org/wiki/Linear_programming" target="_blank"&gt;Linear programming&lt;/a&gt; problems
are usually solved using the &lt;a href="http://en.wikipedia.org/wiki/Simplex_algorithm" target="_blank"&gt;simplex
algorithm&lt;/a&gt;. While it is easy to encode a constraint system of linear equalities
and inequalities as a Parameterized Unit Test for &lt;a href="http://research.microsoft.com/pex"&gt;Pex&lt;/a&gt;,
there is currently no way to tell Pex that we want test inputs that are “minimal”
according to a custom objective function. However, Pex can still generate &lt;strong&gt;*surprising*&lt;/strong&gt; feasible
solutions.
&lt;/p&gt;
&lt;p&gt;
Let's start with a simple set of linear inequalities that define our problem.
&lt;/p&gt;
&lt;blockquote&gt;&lt;pre class="code"&gt;[&lt;span style="color: #2b91af"&gt;PexMethod&lt;/span&gt;] &lt;span style="color: blue"&gt;public
int &lt;/span&gt;Test(&lt;span style="color: blue"&gt;int &lt;/span&gt;x, &lt;span style="color: blue"&gt;int &lt;/span&gt;y)
{&lt;br&gt;
// PexAssume is used to add 'constraints' on the input&lt;br&gt;
// in this case, we simply encode the inequalities in a boolean formula &lt;span style="color: #2b91af"&gt;PexAssume&lt;/span&gt;.IsTrue(
x + y &amp;lt; 10 &amp;amp; // using bitwise &amp;amp; to avoid introducing branches 5 * x + 2
* y &amp;gt; 20 &amp;amp; -x + 2 * y &amp;gt; 0 &amp;amp; x &amp;gt; 0 &amp;amp; y &amp;gt; 0);&lt;br&gt;
// the profit is returned so that it is automatically logged by Pex &lt;span style="color: blue"&gt;return &lt;/span&gt;x
+ 4 * y; }&lt;/pre&gt;&lt;/blockquote&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
After running &lt;strong&gt;Pex&lt;/strong&gt;, we get one feasible solution. It is not optimal
as expected since we don't apply the simplex algorithm.
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
&lt;a href="http://blog.dotnetwiki.org/images/OverflowingLinearProgramming_EC9D/image.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" border="0" alt="image" src="http://blog.dotnetwiki.org/images/OverflowingLinearProgramming_EC9D/image_thumb.png" width="273" height="101"&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
&lt;strong&gt;Enter overflows&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
Remember that .Net arithmetic operations will silently overflow unless you execute
them in a checked context? Let's push our luck and try to force an overflow by changing &lt;strong&gt;x
&amp;gt; 0&lt;/strong&gt; constraint to &lt;strong&gt;x &amp;gt; 1000&lt;/strong&gt;:
&lt;/p&gt;
&lt;blockquote&gt;&lt;pre class="code"&gt;[&lt;span style="color: #2b91af"&gt;PexMethod&lt;/span&gt;] &lt;span style="color: blue"&gt;public
int &lt;/span&gt;Test(&lt;span style="color: blue"&gt;int &lt;/span&gt;x, &lt;span style="color: blue"&gt;int &lt;/span&gt;y)
{ &lt;span style="color: #2b91af"&gt;PexAssume&lt;/span&gt;.IsTrue( x + y &amp;lt; 10 &amp;amp; 5 * x
+ 2 * y &amp;gt; 20 &amp;amp; -x + 2 * y &amp;gt; 0 &amp;amp; &lt;strong&gt;x &amp;gt; 1000 &lt;/strong&gt;&amp;amp; y
&amp;gt; 0 ); &lt;span style="color: blue"&gt;return &lt;/span&gt;x + 4 * y; }&lt;/pre&gt;&lt;/blockquote&gt; 
&lt;p&gt;
&lt;a href="http://research.microsoft.com/projects/Z3" target="_blank"&gt;Z3&lt;/a&gt;, the &lt;a href="http://en.wikipedia.org/wiki/SMT_solver" target="_blank"&gt;constraint
solver&lt;/a&gt; that Pex uses to compute new test inputs, uses &lt;strong&gt;bitvector arithmetic&lt;/strong&gt; to
find a surprising solution that fulfills all the inequalities (our profit has just
gone of the roof :)).
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
&lt;a href="http://blog.dotnetwiki.org/images/OverflowingLinearProgramming_EC9D/image_3.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" border="0" alt="image" src="http://blog.dotnetwiki.org/images/OverflowingLinearProgramming_EC9D/image_thumb_3.png" width="290" height="105"&gt;&lt;/a&gt;&amp;nbsp;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
Z3 is truly &lt;a href="http://www.smtexec.org/exec/" target="_blank"&gt;an astonishing
tool&lt;/a&gt;!
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Checked context&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
In order to avoid overflow, one should use a checked context. Let's update the parameterized
unit test:
&lt;/p&gt;
&lt;blockquote&gt;&lt;pre class="code"&gt;[&lt;span style="color: #2b91af"&gt;PexMethod&lt;/span&gt;] &lt;span style="color: blue"&gt;public
int &lt;/span&gt;Test(&lt;span style="color: blue"&gt;int &lt;/span&gt;x, &lt;span style="color: blue"&gt;int &lt;/span&gt;y)
{ &lt;strong&gt; &lt;/strong&gt;&lt;strong&gt;&lt;span style="color: blue"&gt;checked &lt;/span&gt;{ &lt;/strong&gt; &lt;span style="color: #2b91af"&gt;PexAssume&lt;/span&gt;.IsTrue(
x + y &amp;lt; 10 &amp;amp; 5 * x + 2 * y &amp;gt; 20 &amp;amp; -x + 2 * y &amp;gt; 0 &amp;amp; x &amp;gt; 0 &amp;amp;
y &amp;gt; 0 ); &lt;strong&gt; } &lt;/strong&gt; &lt;span style="color: blue"&gt;return &lt;/span&gt;x + 4 * y;
}&lt;/pre&gt;&lt;/blockquote&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
In fact, in that case, Pex generates 2 test cases. One test that passes and the other
test that triggers an OverflowException (implicit branch).
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
&lt;a href="http://blog.dotnetwiki.org/images/OverflowingLinearProgramming_EC9D/image_4.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" border="0" alt="image" src="http://blog.dotnetwiki.org/images/OverflowingLinearProgramming_EC9D/image_thumb_4.png" width="416" height="158"&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
Stay tuned for more surprising discoveries using Pex.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=b450c6bb-681d-4b7e-9012-318146cc39a1" /&gt;</description>
      <comments>http://blog.dotnetwiki.org/CommentView,guid,b450c6bb-681d-4b7e-9012-318146cc39a1.aspx</comments>
      <category>Pex</category>
      <category>Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.dotnetwiki.org/Trackback.aspx?guid=c02e3478-43f6-4d4b-82e1-6efbb613feec</trackback:ping>
      <pingback:server>http://blog.dotnetwiki.org/pingback.aspx</pingback:server>
      <pingback:target>http://blog.dotnetwiki.org/PermaLink,guid,c02e3478-43f6-4d4b-82e1-6efbb613feec.aspx</pingback:target>
      <dc:creator>Jonathan de Halleux</dc:creator>
      <wfw:comment>http://blog.dotnetwiki.org/CommentView,guid,c02e3478-43f6-4d4b-82e1-6efbb613feec.aspx</wfw:comment>
      <wfw:commentRss>http://blog.dotnetwiki.org/SyndicationService.asmx/GetEntryCommentsRss?guid=c02e3478-43f6-4d4b-82e1-6efbb613feec</wfw:commentRss>
      <slash:comments>3</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
This is a general recommendation if you're planning to use a tool like Pex in the
future: make sure that preconditions (i.e. parameter validation) fails in a different
fashion that other assertions. 
</p>
        <p>
Here's a snippet that shows the problem:
</p>
        <blockquote>
          <p>
// don't do this<br />
void Clone(ICloneable o) {<br /><strong>     Debug.Assert(o != null); // pre-condition<br /></strong>     ...<br />
     object clone = o.Clone();<br /><strong>     Debug.Assert(clone); // assertion<br /></strong>}
</p>
        </blockquote>
        <p>
          <strong>Why is this bad?</strong>
        </p>
        <p>
A tool like <a href="http://research.microsoft.com/pex" target="_blank">Pex</a> will
explore your code and try to trigger every Debug.Assert it finds on its way. When
the assertion is a precondition, it is likely expected and one would like to emit
a negative test case (i.e. 'expected exception').
</p>
        <p>
The problem in the snippet above is that both failure will yield to the same assertion
exception and it will very difficult to <strong>*automatically*</strong> triage the
failure as expected or not.
</p>
        <p>
          <strong>How do I fix this?</strong>
        </p>
        <p>
Make sure different classes of assertions can be differentiated automatically, through
different exception types, tags in the message, etc...
</p>
        <img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=c02e3478-43f6-4d4b-82e1-6efbb613feec" />
      </body>
      <title>Pex It: Don't mix preconditions and assertions</title>
      <guid isPermaLink="false">http://blog.dotnetwiki.org/PermaLink,guid,c02e3478-43f6-4d4b-82e1-6efbb613feec.aspx</guid>
      <link>http://blog.dotnetwiki.org/2008/02/13/PexItDontMixPreconditionsAndAssertions.aspx</link>
      <pubDate>Wed, 13 Feb 2008 17:51:00 GMT</pubDate>
      <description>&lt;p&gt;
This is a general recommendation if you're planning to use a tool like Pex in the
future: make sure that preconditions (i.e. parameter validation) fails in a different
fashion that other assertions. 
&lt;/p&gt;
&lt;p&gt;
Here's a snippet that shows the problem:
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
// don't do this&lt;br&gt;
void Clone(ICloneable o) {&lt;br&gt;
&lt;strong&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Debug.Assert(o != null); // pre-condition&lt;br&gt;
&lt;/strong&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ...&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; object clone = o.Clone();&lt;br&gt;
&lt;strong&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Debug.Assert(clone); // assertion&lt;br&gt;
&lt;/strong&gt;}
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
&lt;strong&gt;Why is this bad?&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
A tool like &lt;a href="http://research.microsoft.com/pex" target="_blank"&gt;Pex&lt;/a&gt; will
explore your code and try to trigger every Debug.Assert it finds on its way. When
the assertion is a precondition, it is likely expected and one would like to emit
a negative test case (i.e. 'expected exception').
&lt;/p&gt;
&lt;p&gt;
The problem in the snippet above is that both failure will yield to the same assertion
exception and it will very difficult to &lt;strong&gt;*automatically*&lt;/strong&gt; triage the
failure as expected or not.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;How do I fix this?&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
Make sure different classes of assertions can be differentiated automatically, through
different exception types, tags in the message, etc...
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=c02e3478-43f6-4d4b-82e1-6efbb613feec" /&gt;</description>
      <comments>http://blog.dotnetwiki.org/CommentView,guid,c02e3478-43f6-4d4b-82e1-6efbb613feec.aspx</comments>
      <category>Pex</category>
      <category>Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.dotnetwiki.org/Trackback.aspx?guid=7fee5daf-8370-4479-b141-a892e669ca3b</trackback:ping>
      <pingback:server>http://blog.dotnetwiki.org/pingback.aspx</pingback:server>
      <pingback:target>http://blog.dotnetwiki.org/PermaLink,guid,7fee5daf-8370-4479-b141-a892e669ca3b.aspx</pingback:target>
      <dc:creator>Jonathan de Halleux</dc:creator>
      <wfw:comment>http://blog.dotnetwiki.org/CommentView,guid,7fee5daf-8370-4479-b141-a892e669ca3b.aspx</wfw:comment>
      <wfw:commentRss>http://blog.dotnetwiki.org/SyndicationService.asmx/GetEntryCommentsRss?guid=7fee5daf-8370-4479-b141-a892e669ca3b</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
          <strong>Update: this talk has been cancelled.</strong>
        </p>
        <p>
I'll be giving a talk about Pex in Diegem on January 3. 
</p>
        <p>
          <a title="http://msdnrss.thecoderblogs.com/2007/12/06/msdn-techtalk-dynamic-analysis-and-test-generation-for-net-with-pex/" href="http://msdnrss.thecoderblogs.com/2007/12/06/msdn-techtalk-dynamic-analysis-and-test-generation-for-net-with-pex/">http://msdnrss.thecoderblogs.com/2007/12/06/msdn-techtalk-dynamic-analysis-and-test-generation-for-net-with-pex/</a>
        </p>
        <img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=7fee5daf-8370-4479-b141-a892e669ca3b" />
      </body>
      <title>Pex TechTalk in Belgium</title>
      <guid isPermaLink="false">http://blog.dotnetwiki.org/PermaLink,guid,7fee5daf-8370-4479-b141-a892e669ca3b.aspx</guid>
      <link>http://blog.dotnetwiki.org/2007/12/06/PexTechTalkInBelgium.aspx</link>
      <pubDate>Thu, 06 Dec 2007 16:33:33 GMT</pubDate>
      <description>&lt;p&gt;
&lt;strong&gt;Update: this talk has been cancelled.&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
I'll be giving a talk about Pex in Diegem on January 3. 
&lt;/p&gt;
&lt;p&gt;
&lt;a title=http://msdnrss.thecoderblogs.com/2007/12/06/msdn-techtalk-dynamic-analysis-and-test-generation-for-net-with-pex/ href="http://msdnrss.thecoderblogs.com/2007/12/06/msdn-techtalk-dynamic-analysis-and-test-generation-for-net-with-pex/"&gt;http://msdnrss.thecoderblogs.com/2007/12/06/msdn-techtalk-dynamic-analysis-and-test-generation-for-net-with-pex/&lt;/a&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=7fee5daf-8370-4479-b141-a892e669ca3b" /&gt;</description>
      <comments>http://blog.dotnetwiki.org/CommentView,guid,7fee5daf-8370-4479-b141-a892e669ca3b.aspx</comments>
      <category>Pex</category>
      <category>Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.dotnetwiki.org/Trackback.aspx?guid=476de33d-0dd7-497a-bb82-fd26e1a82efe</trackback:ping>
      <pingback:server>http://blog.dotnetwiki.org/pingback.aspx</pingback:server>
      <pingback:target>http://blog.dotnetwiki.org/PermaLink,guid,476de33d-0dd7-497a-bb82-fd26e1a82efe.aspx</pingback:target>
      <dc:creator>Jonathan de Halleux</dc:creator>
      <wfw:comment>http://blog.dotnetwiki.org/CommentView,guid,476de33d-0dd7-497a-bb82-fd26e1a82efe.aspx</wfw:comment>
      <wfw:commentRss>http://blog.dotnetwiki.org/SyndicationService.asmx/GetEntryCommentsRss?guid=476de33d-0dd7-497a-bb82-fd26e1a82efe</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
          <a href="http://blog.dotnetwiki.org/PexItHowDoWeFindTheValuesPart2Of.aspx" target="_blank">In
the previous post</a>, we went through the exploration testing process to exercise
a simple method, CheckPositive. In this post, we'll try the same exploration testing,
but will let Pex do it. 
</p>
        <blockquote>
          <p>
// mehod under test<br />
1 void CheckPositive(int i, bool @throw) {<br />
2     if (i &lt; 0) {       
<br />
3          Console.WriteLine("not ok");<br />
4          if (@throw)<br />
5             throw new
ArgumentException();<br />
6     }<br />
7     else 
<br />
8         Console.WriteLine("ok");<br />
9 } 
<br />
// hand-crafted unit tests<br />
[TestMethod] void Zero() {<br />
     CheckPositive(0, false);<br />
}<br />
[TestMethod] void MinusOne() {<br />
     CheckPositive(-1, false);<br />
}<br />
[TestMethod] void MinusOneAndThrow() {<br />
     CheckPositive(-1, true);<br />
}
</p>
        </blockquote>
        <p>
          <strong>Exploration testing with Pex</strong>
        </p>
        <p>
To let Pex explore the CheckPositive, we write a little test wrapper around that method:
</p>
        <blockquote>
          <p>
[TestClass, PexClass]<br />
public partial class ExplorationTesting {<br />
    [PexMethod]<br />
    public void Test(int i, bool @throw) {<br />
        CheckPositive(i, @throw);<br />
    } 
</p>
        </blockquote>
        <p>
We also instrumented the original method with additional methods to track down the
path conditions that Pex computes along the execution traces. Pex generates 3 pairs
of values which are equivalent to what the test we manually created:
</p>
        <ul>
          <li>
0, false</li>
          <li>
int.MinValue, false</li>
          <li>
int.MinValue, true (throws)</li>
        </ul>
        <img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=476de33d-0dd7-497a-bb82-fd26e1a82efe" />
      </body>
      <title>Pex It: How do we find the values? (part 3 of ...)</title>
      <guid isPermaLink="false">http://blog.dotnetwiki.org/PermaLink,guid,476de33d-0dd7-497a-bb82-fd26e1a82efe.aspx</guid>
      <link>http://blog.dotnetwiki.org/2007/12/05/PexItHowDoWeFindTheValuesPart3Of.aspx</link>
      <pubDate>Wed, 05 Dec 2007 16:54:15 GMT</pubDate>
      <description>&lt;p&gt;
&lt;a href="http://blog.dotnetwiki.org/PexItHowDoWeFindTheValuesPart2Of.aspx" target="_blank"&gt;In
the previous post&lt;/a&gt;, we went through the exploration testing process to exercise
a simple method, CheckPositive. In this post, we'll try the same exploration testing,
but will let Pex do it. 
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
// mehod under test&lt;br&gt;
1 void CheckPositive(int i, bool @throw) {&lt;br&gt;
2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (i &amp;lt; 0) {&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&lt;br&gt;
3&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Console.WriteLine("not ok");&lt;br&gt;
4&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (@throw)&lt;br&gt;
5&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; throw new
ArgumentException();&lt;br&gt;
6&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;
7&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else 
&lt;br&gt;
8&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Console.WriteLine("ok");&lt;br&gt;
9 } 
&lt;br&gt;
// hand-crafted unit tests&lt;br&gt;
[TestMethod] void Zero() {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; CheckPositive(0, false);&lt;br&gt;
}&lt;br&gt;
[TestMethod] void MinusOne() {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; CheckPositive(-1, false);&lt;br&gt;
}&lt;br&gt;
[TestMethod] void MinusOneAndThrow() {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; CheckPositive(-1, true);&lt;br&gt;
}
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
&lt;strong&gt;Exploration testing with Pex&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
To let Pex explore the CheckPositive, we write a little test wrapper around that method:
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
[TestClass, PexClass]&lt;br&gt;
public partial class ExplorationTesting {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; [PexMethod]&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public void Test(int i, bool @throw) {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; CheckPositive(i, @throw);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; } 
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
We also instrumented the original method with additional methods to track down the
path conditions that Pex computes along the execution traces. Pex generates 3 pairs
of values which are equivalent to what the test we manually created:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
0, false&lt;/li&gt;
&lt;li&gt;
int.MinValue, false&lt;/li&gt;
&lt;li&gt;
int.MinValue, true (throws)&lt;/li&gt;
&lt;/ul&gt;
&lt;img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=476de33d-0dd7-497a-bb82-fd26e1a82efe" /&gt;</description>
      <comments>http://blog.dotnetwiki.org/CommentView,guid,476de33d-0dd7-497a-bb82-fd26e1a82efe.aspx</comments>
      <category>Pex</category>
      <category>Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.dotnetwiki.org/Trackback.aspx?guid=01a00ee5-a4a1-47b3-a832-df4bef223e36</trackback:ping>
      <pingback:server>http://blog.dotnetwiki.org/pingback.aspx</pingback:server>
      <pingback:target>http://blog.dotnetwiki.org/PermaLink,guid,01a00ee5-a4a1-47b3-a832-df4bef223e36.aspx</pingback:target>
      <dc:creator>Jonathan de Halleux</dc:creator>
      <wfw:comment>http://blog.dotnetwiki.org/CommentView,guid,01a00ee5-a4a1-47b3-a832-df4bef223e36.aspx</wfw:comment>
      <wfw:commentRss>http://blog.dotnetwiki.org/SyndicationService.asmx/GetEntryCommentsRss?guid=01a00ee5-a4a1-47b3-a832-df4bef223e36</wfw:commentRss>
      <slash:comments>3</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I realized that I had not talked much about how <a href="http://research.microsoft.com/pex">Pex</a> computes
the values for the test parameters... the most important part of the tool!
</p>
        <h4>It's not ...
</h4>
        <p>
Let's start by getting to wrong ideas out of the way. Parameterized tests are nothing
new, they exist in <a href="http://www.mbunit.com/">MbUnit</a>, VSTS, <a href="http://www.codeplex.com/xunit">XUnit.Net</a>,
FIT, etc... so what's different with Pex?
</p>
        <ul>
          <li>
            <strong>it is not random</strong>: when it comes to generate data, the easiest solution
is to plugin a random generator. If the state space is big enough (e.g. integers 2^32),
it highly unlikely that random tests will find the interesting corner cases,</li>
        </ul>
        <p>
if (i == 123456)<br />
     throw new Exception(); &lt;---------- random won't find this 
</p>
        <ul>
          <li>
            <strong>it does not require ranges or hints for the data: </strong>Pex does not require
annotation to specify the range of particular inputs. All the relevant input values
are inferred from the code itself (we'll see later how).</li>
          <li>
            <strong>it is not pairwize testing</strong>: following the comment above, Pex does
use a pairwize approach.</li>
          <li>
            <strong>it is not data testing</strong>: data testing such as FIT or MbUnit RowTest
usually consists of rows containing a set of inputs and the expected output. In Pex,
you cannot provide the expected output as a 'concrete' value, you need to express
it as code (through assertions for example). This is a subtle difference that radically
changes the way you write your tests.</li>
        </ul>
        <p>
[RowTest, Row(0, 1, 1), Row(1, 0, 1)] // data test for the addition<br />
void AddTest(int a, int b, int result)<br />
{   Assert.AreEqual(result, a + b);  }
</p>
[PexMethod] // 0 is the neutral of the addition operation<br />
void ZeroNeutralTest(int b)<br />
{   Assert.AreEqual(b, 0 + b);  }<br /><br /><ul><li><strong>it is not a static analysis tool:</strong> Pex does a <strong>dynamic</strong> analysis
of the code; it analyses the code that *is* running. Pex does this by rewriting the
IL before it's jitted and instrumenting it with (many) callbacks to track precisely
which IL instruction is being run by the CLR. So yes, Pex analyses the IL but on the
fly rather than 'statically'.</li></ul><p>
Ok, now we've got a better idea of what Pex is not. So how does it work? .... 
</p><img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=01a00ee5-a4a1-47b3-a832-df4bef223e36" /></body>
      <title>Pex It: How do we find the values? (part 1 of ...)</title>
      <guid isPermaLink="false">http://blog.dotnetwiki.org/PermaLink,guid,01a00ee5-a4a1-47b3-a832-df4bef223e36.aspx</guid>
      <link>http://blog.dotnetwiki.org/2007/11/30/PexItHowDoWeFindTheValuesPart1Of.aspx</link>
      <pubDate>Fri, 30 Nov 2007 09:19:05 GMT</pubDate>
      <description>&lt;p&gt;
I realized that I had not talked much about how &lt;a href="http://research.microsoft.com/pex"&gt;Pex&lt;/a&gt; computes
the values for the test parameters... the most important part of the tool!
&lt;/p&gt;
&lt;h4&gt;It's not ...
&lt;/h4&gt;
&lt;p&gt;
Let's start by getting to wrong ideas out of the way. Parameterized tests are nothing
new, they exist in &lt;a href="http://www.mbunit.com/"&gt;MbUnit&lt;/a&gt;, VSTS, &lt;a href="http://www.codeplex.com/xunit"&gt;XUnit.Net&lt;/a&gt;,
FIT, etc... so what's different with Pex?
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;it is not random&lt;/strong&gt;: when it comes to generate data, the easiest solution
is to plugin a random generator. If the state space is big enough (e.g. integers 2^32),
it highly unlikely that random tests will find the interesting corner cases,&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
if (i == 123456)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; throw new Exception(); &amp;lt;---------- random won't find this 
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;it does not require ranges or hints for the data: &lt;/strong&gt;Pex does not require
annotation to specify the range of particular inputs. All the relevant input values
are inferred from the code itself (we'll see later how).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;it is not pairwize testing&lt;/strong&gt;: following the comment above, Pex does
use a pairwize approach.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;it is not data testing&lt;/strong&gt;: data testing such as FIT or MbUnit RowTest
usually consists of rows containing a set of inputs and the expected output. In Pex,
you cannot provide the expected output as a 'concrete' value, you need to express
it as code (through assertions for example). This is a subtle difference that radically
changes the way you write your tests.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
[RowTest, Row(0, 1, 1), Row(1, 0, 1)] // data test for the addition&lt;br&gt;
void AddTest(int a, int b, int result)&lt;br&gt;
{&amp;nbsp;&amp;nbsp; Assert.AreEqual(result, a + b);&amp;nbsp; }
&lt;/p&gt;
[PexMethod] // 0 is the neutral of the addition operation&lt;br&gt;
void ZeroNeutralTest(int b)&lt;br&gt;
{&amp;nbsp;&amp;nbsp; Assert.AreEqual(b, 0 + b);&amp;nbsp; }&lt;br&gt;
&lt;br&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;it is not a static analysis tool:&lt;/strong&gt; Pex does a &lt;strong&gt;dynamic&lt;/strong&gt; analysis
of the code; it analyses the code that *is* running. Pex does this by rewriting the
IL before it's jitted and instrumenting it with (many) callbacks to track precisely
which IL instruction is being run by the CLR. So yes, Pex analyses the IL but on the
fly rather than 'statically'.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
Ok, now we've got a better idea of what Pex is not. So how does it work? .... 
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=01a00ee5-a4a1-47b3-a832-df4bef223e36" /&gt;</description>
      <comments>http://blog.dotnetwiki.org/CommentView,guid,01a00ee5-a4a1-47b3-a832-df4bef223e36.aspx</comments>
      <category>Pex</category>
      <category>Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.dotnetwiki.org/Trackback.aspx?guid=602a908d-03e0-4a9d-be21-f8587f6f2bb9</trackback:ping>
      <pingback:server>http://blog.dotnetwiki.org/pingback.aspx</pingback:server>
      <pingback:target>http://blog.dotnetwiki.org/PermaLink,guid,602a908d-03e0-4a9d-be21-f8587f6f2bb9.aspx</pingback:target>
      <dc:creator>Jonathan de Halleux</dc:creator>
      <wfw:comment>http://blog.dotnetwiki.org/CommentView,guid,602a908d-03e0-4a9d-be21-f8587f6f2bb9.aspx</wfw:comment>
      <wfw:commentRss>http://blog.dotnetwiki.org/SyndicationService.asmx/GetEntryCommentsRss?guid=602a908d-03e0-4a9d-be21-f8587f6f2bb9</wfw:commentRss>
      <slash:comments>3</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
In it's <a href="http://www.hanselman.com/blog/TheWeeklySourceCode8.aspx"><strong><font color="#355ea0">Weekly
Source Code</font></strong></a>, Scott Hanselman presents a new CodePlex project, <a href="http://www.codeplex.com/FileDirectoryPath"><font color="#005bba">NDepend.Helpers.FileDirectoryPath</font></a> from
Patrick Smacchia. Nice, better path handling should have been part of the BCL a while
ago. 
</p>
        <p>
          <strong>Path stuff is hard</strong>
        </p>
        <p>
Path normalization and parsing is not an easy task so when Patrick Smacchia mentions
that his code<em> "100% unit-tested", </em>I decided to see if <a href="http://research.microsoft.com/pex"><strong><font color="#355ea0">Pex</font></strong></a> could
not find a little bug over there.
</p>
        <p>
          <strong>A dumb parameterized unit test</strong>
        </p>
        <p>
So I added wrote the following parameterized unit test, which 'simply' calls the constructor
of FilePathRelative. Under the hood, there is some string manipulation done by the
library to normalize the path, it should be interresting to see what comes out of
this. I also added calls to PexComment.XXX to log the input/output values (Pex will
build a table out of this):
</p>
        <blockquote>
          <pre>    [PexMethod]<br />
    public void FilePathRelativeCtor(string path) {<br />
        PexComment.Parameters(path);<br />
        FilePath result = new FilePathRelative(path);<br />
        PexComment.Value("result", result.FileName);<br />
    }</pre>
        </blockquote>
        <p>
          <strong>Ooops</strong>
        </p>
        <p>
So Pex starts running and soon enough an assert pops up. Pex had just found a neat
little path that broke an assertion in the library:
</p>
        <blockquote>
          <pre>[Test]<br />
public void FilePathRelativeCtor_String_71019_003302_0_05() {<br />
    this.FilePathRelativeCtor<strong>("/");</strong><br />
}</pre>
        </blockquote>
        <p>
Popping up the reports, I went for the parameter table (remember the PexComment calls)
that shows one row for each generated test. In fact, the 5-th test that Pex generated
was triggering the assert:
</p>
        <p>
          <img src="http://blog.dotnetwiki.org/content/binary/summarytable.png" border="0" />
        </p>
        <p>
          <a href="file:///C:/Users/jhalleux/AppData/Roaming/Windows%20Live%20Writer/PostSupportingFiles/40c8d1f7-7037-47aa-a6ee-736367755a3b/image[7].png" atomicselection="true">
          </a>
        </p>
        <p>
Note that "//" also triggers the bug which seems to indicate that any path finishing
by "/" will have this behavior. 
</p>
        <p>
          <strong>The path condition</strong>
        </p>
        <p>
Lastly, I took a quick look at the path condition that Pex solved to discover the
bug (see red below). Luckily this one is fairly easy and one can clearly see 'path[0]
== '/' in there.
</p>
        <p>
          <img src="http://blog.dotnetwiki.org/content/binary/summarytable1.png" border="0" />
        </p>
        <p>
          <a href="file:///C:/Users/jhalleux/AppData/Roaming/Windows%20Live%20Writer/PostSupportingFiles/40c8d1f7-7037-47aa-a6ee-736367755a3b/image[10].png" atomicselection="true">
          </a>
        </p>
        <p>
          <strong> What did we learn today?</strong>
        </p>
        <p>
Handling paths is hard :)
</p>
        <p>
Also, we saw that a <strong>dumb</strong> parameterized unit test (just calling a
ctor), could find a bugs. If you use assertions, it will help Pex look for bugs in
your code.
</p>
        <img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=602a908d-03e0-4a9d-be21-f8587f6f2bb9" />
      </body>
      <title>Pex It: looking at NDepend.Helpers.FileDirectoryPath</title>
      <guid isPermaLink="false">http://blog.dotnetwiki.org/PermaLink,guid,602a908d-03e0-4a9d-be21-f8587f6f2bb9.aspx</guid>
      <link>http://blog.dotnetwiki.org/2007/10/19/PexItLookingAtNDependHelpersFileDirectoryPath.aspx</link>
      <pubDate>Fri, 19 Oct 2007 08:46:37 GMT</pubDate>
      <description>&lt;p&gt;
In it's &lt;a href="http://www.hanselman.com/blog/TheWeeklySourceCode8.aspx"&gt;&lt;strong&gt;&lt;font color=#355ea0&gt;Weekly
Source Code&lt;/font&gt;&lt;/strong&gt;&lt;/a&gt;, Scott Hanselman presents a new CodePlex project, &lt;a href="http://www.codeplex.com/FileDirectoryPath"&gt;&lt;font color=#005bba&gt;NDepend.Helpers.FileDirectoryPath&lt;/font&gt;&lt;/a&gt; from
Patrick Smacchia. Nice, better path handling should have been part of the BCL a while
ago.&amp;nbsp;
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Path stuff is hard&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
Path normalization and parsing is not an easy task so when Patrick Smacchia mentions
that his code&lt;em&gt; "100% unit-tested", &lt;/em&gt;I decided to see if &lt;a href="http://research.microsoft.com/pex"&gt;&lt;strong&gt;&lt;font color=#355ea0&gt;Pex&lt;/font&gt;&lt;/strong&gt;&lt;/a&gt; could
not find a little bug over there.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;A dumb parameterized unit test&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
So I added wrote the following parameterized unit test, which 'simply' calls the constructor
of FilePathRelative. Under the hood, there is some string manipulation done by the
library to normalize the path, it should be interresting to see what comes out of
this. I also added calls to PexComment.XXX to log the input/output values (Pex will
build a table out of this):
&lt;/p&gt;
&lt;blockquote&gt;&lt;pre&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; [PexMethod]&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public void FilePathRelativeCtor(string path) {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; PexComment.Parameters(path);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; FilePath result = new FilePathRelative(path);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; PexComment.Value("result", result.FileName);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;/blockquote&gt; 
&lt;p&gt;
&lt;strong&gt;Ooops&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
So Pex starts running and soon enough an assert pops up. Pex had just found a neat
little path that broke an assertion in the library:
&lt;/p&gt;
&lt;blockquote&gt;&lt;pre&gt;[Test]&lt;br&gt;
public void FilePathRelativeCtor_String_71019_003302_0_05() {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; this.FilePathRelativeCtor&lt;strong&gt;("/");&lt;/strong&gt;
&lt;br&gt;
}&lt;/pre&gt;&lt;/blockquote&gt; 
&lt;p&gt;
Popping up the reports, I went for the parameter table (remember the PexComment calls)
that shows one row for each generated test. In fact, the 5-th test that Pex generated
was triggering the assert:
&lt;/p&gt;
&lt;p&gt;
&lt;img src="http://blog.dotnetwiki.org/content/binary/summarytable.png" border=0&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;a href="file:///C:/Users/jhalleux/AppData/Roaming/Windows%20Live%20Writer/PostSupportingFiles/40c8d1f7-7037-47aa-a6ee-736367755a3b/image[7].png" atomicselection="true"&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
Note that "//" also triggers the bug which seems to indicate that any path finishing
by "/" will have this behavior. 
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;The path condition&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
Lastly, I took a quick look at the path condition that Pex solved to discover the
bug (see red below). Luckily this one is fairly easy and one can clearly see 'path[0]
== '/' in there.
&lt;/p&gt;
&lt;p&gt;
&lt;img src="http://blog.dotnetwiki.org/content/binary/summarytable1.png" border=0&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;a href="file:///C:/Users/jhalleux/AppData/Roaming/Windows%20Live%20Writer/PostSupportingFiles/40c8d1f7-7037-47aa-a6ee-736367755a3b/image[10].png" atomicselection="true"&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;&amp;nbsp;What did we learn today?&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
Handling paths is hard :)
&lt;/p&gt;
&lt;p&gt;
Also, we saw that a &lt;strong&gt;dumb&lt;/strong&gt; parameterized unit test (just calling a
ctor), could find a bugs. If you use assertions, it will help Pex look for bugs in
your code.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=602a908d-03e0-4a9d-be21-f8587f6f2bb9" /&gt;</description>
      <comments>http://blog.dotnetwiki.org/CommentView,guid,602a908d-03e0-4a9d-be21-f8587f6f2bb9.aspx</comments>
      <category>Pex</category>
      <category>Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.dotnetwiki.org/Trackback.aspx?guid=aa94cbbd-4c64-4de8-aac6-9e293dbd6f7e</trackback:ping>
      <pingback:server>http://blog.dotnetwiki.org/pingback.aspx</pingback:server>
      <pingback:target>http://blog.dotnetwiki.org/PermaLink,guid,aa94cbbd-4c64-4de8-aac6-9e293dbd6f7e.aspx</pingback:target>
      <dc:creator>Jonathan de Halleux</dc:creator>
      <wfw:comment>http://blog.dotnetwiki.org/CommentView,guid,aa94cbbd-4c64-4de8-aac6-9e293dbd6f7e.aspx</wfw:comment>
      <wfw:commentRss>http://blog.dotnetwiki.org/SyndicationService.asmx/GetEntryCommentsRss?guid=aa94cbbd-4c64-4de8-aac6-9e293dbd6f7e</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Update: I will not be at the Seattle Code Camp, too much rescheduling.
</p>
        <p>
          <font color="#000000">I'll be presenting <a href="http://research.microsoft.com/pex">Pex</a> at
the <a href="http://seattle.codecamp.us/sessions.aspx">Seattle Code Camp</a> in
Nov. </font>
        </p>
        <blockquote dir="ltr" style="MARGIN-RIGHT: 0px">
          <p>
            <font color="#000000">
              <strong>Pex – Automated White Box Unit Testing</strong>
            </font>
          </p>
          <p>
            <font color="#000000">Parameterized unit testing is becoming a mainstream feature
of most unit test frameworks; MbUnit RowTest (and more), VSTS data tests, xUnit.net
Theories, etc... Unfortunately, it is still the responsibility of the developer to
figure out relevant parameter values to exercise the code. With Pex, this is no longer
true. Pex is a unit test framework addin that can generate relevant parameter values
for parameterized unit tests. Pex uses an automated white box analysis (i.e. it monitors
the code execution at runtime) to systematically explore every branches in the code.
In this talk, Peli will give an overview of the technology behind Pex (with juicy
low-level .NET profiling goodness), then quickly jump to exiting live demos.</font>
          </p>
        </blockquote>
        <img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=aa94cbbd-4c64-4de8-aac6-9e293dbd6f7e" />
      </body>
      <title>Pex at the Seattle Code Camp</title>
      <guid isPermaLink="false">http://blog.dotnetwiki.org/PermaLink,guid,aa94cbbd-4c64-4de8-aac6-9e293dbd6f7e.aspx</guid>
      <link>http://blog.dotnetwiki.org/2007/10/17/PexAtTheSeattleCodeCamp.aspx</link>
      <pubDate>Wed, 17 Oct 2007 01:36:52 GMT</pubDate>
      <description>&lt;p&gt;
Update: I will not be at the Seattle Code Camp, too much rescheduling.
&lt;/p&gt;
&lt;p&gt;
&lt;font color=#000000&gt;I'll be presenting &lt;a href="http://research.microsoft.com/pex"&gt;Pex&lt;/a&gt; at
the &lt;a href="http://seattle.codecamp.us/sessions.aspx"&gt;Seattle Code Camp&lt;/a&gt;&amp;nbsp;in
Nov. &lt;/font&gt;
&lt;/p&gt;
&lt;blockquote dir=ltr style="MARGIN-RIGHT: 0px"&gt; 
&lt;p&gt;
&lt;font color=#000000&gt;&lt;strong&gt;Pex – Automated White Box Unit Testing&lt;/strong&gt;&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font color=#000000&gt;Parameterized unit testing is becoming a mainstream feature of
most unit test frameworks; MbUnit RowTest (and more), VSTS data tests, xUnit.net Theories,
etc... Unfortunately, it is still the responsibility of the developer to figure out
relevant parameter values to exercise the code. With Pex, this is no longer true.
Pex is a unit test framework addin that can generate relevant parameter values for
parameterized unit tests. Pex uses an automated white box analysis (i.e. it monitors
the code execution at runtime) to systematically explore every branches in the code.
In this talk, Peli will give an overview of the technology behind Pex (with juicy
low-level .NET profiling goodness), then quickly jump to exiting live demos.&lt;/font&gt;
&lt;/p&gt;
&lt;/blockquote&gt;&lt;img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=aa94cbbd-4c64-4de8-aac6-9e293dbd6f7e" /&gt;</description>
      <comments>http://blog.dotnetwiki.org/CommentView,guid,aa94cbbd-4c64-4de8-aac6-9e293dbd6f7e.aspx</comments>
      <category>MbUnit</category>
      <category>Pex</category>
      <category>Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.dotnetwiki.org/Trackback.aspx?guid=07143420-44c6-4480-b82f-8b056453af75</trackback:ping>
      <pingback:server>http://blog.dotnetwiki.org/pingback.aspx</pingback:server>
      <pingback:target>http://blog.dotnetwiki.org/PermaLink,guid,07143420-44c6-4480-b82f-8b056453af75.aspx</pingback:target>
      <dc:creator>Jonathan de Halleux</dc:creator>
      <wfw:comment>http://blog.dotnetwiki.org/CommentView,guid,07143420-44c6-4480-b82f-8b056453af75.aspx</wfw:comment>
      <wfw:commentRss>http://blog.dotnetwiki.org/SyndicationService.asmx/GetEntryCommentsRss?guid=07143420-44c6-4480-b82f-8b056453af75</wfw:commentRss>
      <slash:comments>2</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
When someone is writing a book that contains code snippets, the question of (automatically)
keeping those in sync quickly becomes very imporant. There's already lots of
different solutions to this problem (every author has probably it's own), here's yet
another one for C# that we've developed to author the <a href="http://research.microsoft.com/pex/">Pex</a> documentation.
</p>
        <p>
          <strong>Goals</strong>
        </p>
        <p>
A couple things that we wanted to acheive with this tool:
</p>
        <ul>
          <li>
snippets are always compilable and run as expected, 
</li>
          <li>
snippets can be full classes, methods or even partial statements 
</li>
          <li>
simple :)</li>
        </ul>
        <p>
          <strong>'#region' based solution</strong>
        </p>
        <p>
This solution uses the <strong>#region</strong> directive to define a snippet.
The region describe contains the snippet name, which will be used to dump it into
a file. For example, given this piece of C#,
</p>
        <blockquote dir="ltr" style="MARGIN-RIGHT: 0px">
          <pre>...<br />
#region snippet StackExamplePart3<br />
stack.Push(new object);<br />
#endregion<br />
...</pre>
        </blockquote>
        <p>
Our parse will extract the code in the region and write it to <em>StackExamplePart3.tex</em>,
which gets pulled in our LaTeX scripts.
</p>
        <blockquote dir="ltr" style="MARGIN-RIGHT: 0px">
          <pre>\begin{verbatim}<br />
stack.Push(new object);<br />
\end{verbatim}</pre>
        </blockquote>
        <p>
          <strong>That's it?</strong>
        </p>
        <p>
Yes, you can author snippets that stay compilable and up to date:
</p>
        <ul>
          <li>
we can author all the snippets in Visual Studio and we are sure they always compile 
</li>
          <li>
it's very easy to parse the #region's (left as exercise ;)) 
</li>
          <li>
#region are very flexible in terms what they contain so we can have snippets containing
partial methods, statement, etc... 
</li>
          <li>
the scheme aslo supports nested regions which is usefull when one explains an example
line by line, and integrate the entire sample at the end. For example, DeclaringUnitTest
is a 'sub'-snippet of UnitTest:</li>
        </ul>
        <blockquote dir="ltr" style="MARGIN-RIGHT: 0px">
          <pre>#region snippet UnitTest<br />
#region snippet DeclaringUnitTest<br />
[TestMethod]<br />
void Test(int i)<br />
#endregion<br />
{<br />
    
<br />
}<br />
#endregion</pre>
        </blockquote>
        <ul>
          <li>
we can integrate our snippets in unit tests and verify they work as expected 
</li>
          <li>
the tool can be integrated into the build process as a post-command build</li>
        </ul>
        <p>
 
</p>
        <img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=07143420-44c6-4480-b82f-8b056453af75" />
      </body>
      <title>A simple approach for keeping book snippets in sync for C#</title>
      <guid isPermaLink="false">http://blog.dotnetwiki.org/PermaLink,guid,07143420-44c6-4480-b82f-8b056453af75.aspx</guid>
      <link>http://blog.dotnetwiki.org/2007/10/11/ASimpleApproachForKeepingBookSnippetsInSyncForC.aspx</link>
      <pubDate>Thu, 11 Oct 2007 06:41:32 GMT</pubDate>
      <description>&lt;p&gt;
When someone is writing a book that contains code snippets, the question of (automatically)
keeping those in sync&amp;nbsp;quickly becomes very imporant. There's already lots of
different solutions to this problem (every author has probably it's own), here's yet
another one for C# that we've developed to author the&amp;nbsp;&lt;a href="http://research.microsoft.com/pex/"&gt;Pex&lt;/a&gt; documentation.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Goals&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
A couple things that we wanted to acheive with this tool:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
snippets are always compilable and run as expected, 
&lt;li&gt;
snippets can be full classes, methods or even partial statements 
&lt;li&gt;
simple :)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
&lt;strong&gt;'#region' based solution&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
This&amp;nbsp;solution uses the &lt;strong&gt;#region&lt;/strong&gt; directive&amp;nbsp;to define a snippet.
The region describe contains the snippet name, which will be used to dump it into
a file. For example, given this piece of C#,
&lt;/p&gt;
&lt;blockquote dir=ltr style="MARGIN-RIGHT: 0px"&gt;&lt;pre&gt;...&lt;br&gt;
#region snippet StackExamplePart3&lt;br&gt;
stack.Push(new object);&lt;br&gt;
#endregion&lt;br&gt;
...&lt;/pre&gt;&lt;/blockquote&gt; 
&lt;p&gt;
Our parse will extract the code in the region and write it to &lt;em&gt;StackExamplePart3.tex&lt;/em&gt;,
which gets pulled in our LaTeX scripts.
&lt;/p&gt;
&lt;blockquote dir=ltr style="MARGIN-RIGHT: 0px"&gt;&lt;pre&gt;\begin{verbatim}&lt;br&gt;
stack.Push(new object);&lt;br&gt;
\end{verbatim}&lt;/pre&gt;&lt;/blockquote&gt; 
&lt;p&gt;
&lt;strong&gt;That's it?&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
Yes, you can author snippets that stay compilable and up to date:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
we can author all the snippets in Visual Studio and we are sure they always compile 
&lt;li&gt;
it's very easy to parse the #region's (left as exercise ;)) 
&lt;li&gt;
#region are very flexible in terms what they contain so we can have snippets containing
partial methods, statement, etc... 
&lt;li&gt;
the scheme aslo supports nested regions which is usefull when one explains an example
line by line, and integrate the entire sample at the end. For example, DeclaringUnitTest
is a 'sub'-snippet of UnitTest:&lt;/li&gt;
&lt;/ul&gt;
&lt;blockquote dir=ltr style="MARGIN-RIGHT: 0px"&gt;&lt;pre&gt;#region snippet UnitTest&lt;br&gt;
#region snippet DeclaringUnitTest&lt;br&gt;
[TestMethod]&lt;br&gt;
void Test(int i)&lt;br&gt;
#endregion&lt;br&gt;
{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&lt;br&gt;
}&lt;br&gt;
#endregion&lt;/pre&gt;&lt;/blockquote&gt; 
&lt;ul&gt;
&lt;li&gt;
we can integrate our snippets in unit tests and verify they work as expected 
&lt;li&gt;
the tool can be integrated into the build process as a post-command build&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=07143420-44c6-4480-b82f-8b056453af75" /&gt;</description>
      <comments>http://blog.dotnetwiki.org/CommentView,guid,07143420-44c6-4480-b82f-8b056453af75.aspx</comments>
      <category>Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.dotnetwiki.org/Trackback.aspx?guid=777c9993-5d2a-4fb2-a354-c11f488cc0a9</trackback:ping>
      <pingback:server>http://blog.dotnetwiki.org/pingback.aspx</pingback:server>
      <pingback:target>http://blog.dotnetwiki.org/PermaLink,guid,777c9993-5d2a-4fb2-a354-c11f488cc0a9.aspx</pingback:target>
      <dc:creator>Jonathan de Halleux</dc:creator>
      <wfw:comment>http://blog.dotnetwiki.org/CommentView,guid,777c9993-5d2a-4fb2-a354-c11f488cc0a9.aspx</wfw:comment>
      <wfw:commentRss>http://blog.dotnetwiki.org/SyndicationService.asmx/GetEntryCommentsRss?guid=777c9993-5d2a-4fb2-a354-c11f488cc0a9</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
          <a href="http://www.codeplex.com/xunit" target="_blank">xUnit</a>, the new variation
on the 'unit test framework' theme comes with support for data driven tests: 'Theories'
(funny name by btw). <a href="http://research.microsoft.com/pex" target="_blank">Pex</a> is
a plugin for test frameworks, so we've added support for xUnit as well.
</p>
        <blockquote>
          <p>
            <strong>[PexClass]</strong> // xUnit does not have fixture attributes<br />
public class MyTests<br />
{<br />
    <strong>[Theory</strong>, DataViaXXXX] // xUnit theories<br />
    <strong>[PexTest]</strong> // let pex help you <br />
    public void Test(int a, ....)<br />
    {}<br />
}
</p>
        </blockquote>
        <img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=777c9993-5d2a-4fb2-a354-c11f488cc0a9" />
      </body>
      <title>Pex It: Support for xUnit</title>
      <guid isPermaLink="false">http://blog.dotnetwiki.org/PermaLink,guid,777c9993-5d2a-4fb2-a354-c11f488cc0a9.aspx</guid>
      <link>http://blog.dotnetwiki.org/2007/09/23/PexItSupportForXUnit.aspx</link>
      <pubDate>Sun, 23 Sep 2007 16:26:32 GMT</pubDate>
      <description>&lt;p&gt;
&lt;a href="http://www.codeplex.com/xunit" target=_blank&gt;xUnit&lt;/a&gt;, the new variation
on the 'unit test framework' theme comes with support for data driven tests: 'Theories'
(funny name by btw). &lt;a href="http://research.microsoft.com/pex" target=_blank&gt;Pex&lt;/a&gt; is
a plugin for test frameworks, so we've added support for xUnit as well.
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
&lt;strong&gt;[PexClass]&lt;/strong&gt; // xUnit does not have fixture attributes&lt;br&gt;
public class MyTests&lt;br&gt;
{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;strong&gt;[Theory&lt;/strong&gt;, DataViaXXXX] // xUnit theories&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;strong&gt;[PexTest]&lt;/strong&gt; // let pex&amp;nbsp;help you&amp;nbsp;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public void Test(int a, ....)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {}&lt;br&gt;
}
&lt;/p&gt;
&lt;/blockquote&gt;&lt;img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=777c9993-5d2a-4fb2-a354-c11f488cc0a9" /&gt;</description>
      <comments>http://blog.dotnetwiki.org/CommentView,guid,777c9993-5d2a-4fb2-a354-c11f488cc0a9.aspx</comments>
      <category>Pex</category>
      <category>Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.dotnetwiki.org/Trackback.aspx?guid=3ef9c121-6529-4e80-ac88-84ee73ab2b73</trackback:ping>
      <pingback:server>http://blog.dotnetwiki.org/pingback.aspx</pingback:server>
      <pingback:target>http://blog.dotnetwiki.org/PermaLink,guid,3ef9c121-6529-4e80-ac88-84ee73ab2b73.aspx</pingback:target>
      <dc:creator>Jonathan de Halleux</dc:creator>
      <wfw:comment>http://blog.dotnetwiki.org/CommentView,guid,3ef9c121-6529-4e80-ac88-84ee73ab2b73.aspx</wfw:comment>
      <wfw:commentRss>http://blog.dotnetwiki.org/SyndicationService.asmx/GetEntryCommentsRss?guid=3ef9c121-6529-4e80-ac88-84ee73ab2b73</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
In a previous, we were looking at partial trust the lack of support for it. In this
post, I'll show the key 'fixes' that we did to make Pex <a href="http://blog.dotnetwiki.org/IsYourTestFrameworkPartialTrustFriendly.aspx" target="_blank">'partial
trust' aware</a>.
</p>
        <p>
          <strong>Simulating Partial Trust</strong>
        </p>
        <p>
The easiest way to run under partial trust is to run your .net application from
the network. However, in the context of a test framework, this would
not work since many required permissions would not be granted (reflection, i/o, etc...).
So we need a new AppDomain whose security policy considers the test
framework assemblies as fully trusted.
</p>
        <ul>
          <li>
Get a new AppDomain:</li>
        </ul>
        <pre>string trust = "LocalIntranet";<br />
AppDomain domain = AppDomain.CreateAppDomain(trust);</pre>
        <ul>
          <li>
Load the named permission set</li>
        </ul>
        <pre>PermissionSet permission = <a href="http://blogs.msdn.com/shawnfa/archive/2004/10/22/246549.aspx" target="_blank">GetNamedPermissionSet</a>(trust);</pre>
        <ul>
          <li>
Create the code group structure that associate the partial trust permission to any
code 
</li>
        </ul>
        <pre>UnionCodeGroup code= new UnionCodeGroup(<br />
    new AllMembershipCondition(),<br />
    new PolicyStatement(permission, PolicyStatementAttribute.Nothing)); </pre>
        <ul>
          <li>
give full trust to each test framework assembly:</li>
        </ul>
        <pre>StrongName strongName = <a href="http://blogs.msdn.com/shawnfa/archive/2005/08/08/449050.aspx" target="_blank">CreateStrongName</a>(typeof(TestFixtureAttribute).Assembly);<br />
PermissionSet fullTrust = new PermissionSet(PermissionState.Unrestricted);<br />
UnionCodeGroup fullTrustCode = new UnionCodeGroup(<br />
new StrongNameMembershipCondition(strongName.PublicKey, strongName.Name, strongName.Version), 
<br />
new PolicyStatement(fullTrust, PolicyStatementAttribute.Nothing));<br />
code.AddChild(fullTrustCode);</pre>
        <ul>
          <li>
Assign the policy to the AppDomain</li>
        </ul>
        <pre>PolicyLevel policy = PolicyLevel.CreateAppDomainLevel();<br />
policy.RootCodeGroup = code;<br />
domain.SetAppDomainPolicy(policy); </pre>
        <p>
This is basically it (the rest of the details are left as an exercise :)). 
</p>
        <p>
          <strong>Let them call you</strong>
        </p>
        <p>
Make sure to add the <strong>AllowPartiallyTrustedCallers</strong> to the test framework
assembly otherwize users won't be allowed to call into it... 
</p>
        <p>
          <strong>A twist...</strong>
        </p>
        <p>
Pex is bit invasive when it comes to partial trust. Pex rewrites the IL at runtime
and turns all method bodies into... <strong>unsafe</strong> code (that is unverifiable).
At this point, any will not run because of the SkipVerification permission. 
</p>
        <p>
No problemo, just add it to the permissionset: 
</p>
        <pre>permission.AddPermission(<br />
    new SecurityPermission(SecurityPermissionFlag.SkipVerification)<br />
    ); </pre>
        <p>
 
</p>
        <img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=3ef9c121-6529-4e80-ac88-84ee73ab2b73" />
      </body>
      <title>Pex It: Partial Trust... with a twist</title>
      <guid isPermaLink="false">http://blog.dotnetwiki.org/PermaLink,guid,3ef9c121-6529-4e80-ac88-84ee73ab2b73.aspx</guid>
      <link>http://blog.dotnetwiki.org/2007/09/08/PexItPartialTrustWithATwist.aspx</link>
      <pubDate>Sat, 08 Sep 2007 06:39:22 GMT</pubDate>
      <description>&lt;p&gt;
In a previous, we were looking at partial trust the lack of support for it. In this
post, I'll show the key 'fixes'&amp;nbsp;that we did to make&amp;nbsp;Pex &lt;a href="http://blog.dotnetwiki.org/IsYourTestFrameworkPartialTrustFriendly.aspx" target=_blank&gt;'partial
trust' aware&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Simulating Partial Trust&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
The easiest way to&amp;nbsp;run under partial trust is to run your .net application from
the network.&amp;nbsp;However, in&amp;nbsp;the context of a&amp;nbsp;test framework, this would
not work since many required permissions would not be granted (reflection, i/o, etc...).
So we need a new&amp;nbsp;AppDomain whose security policy&amp;nbsp;considers the&amp;nbsp;test
framework assemblies&amp;nbsp;as fully trusted.
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
Get a new AppDomain:&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;string trust = "LocalIntranet";&lt;br&gt;
AppDomain domain = AppDomain.CreateAppDomain(trust);&lt;/pre&gt;
&lt;ul&gt;
&lt;li&gt;
Load the named permission set&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;PermissionSet permission = &lt;a href="http://blogs.msdn.com/shawnfa/archive/2004/10/22/246549.aspx" target=_blank&gt;GetNamedPermissionSet&lt;/a&gt;(trust);&lt;/pre&gt;
&lt;ul&gt;
&lt;li&gt;
Create the code group structure that associate the partial trust permission to any
code 
&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;UnionCodeGroup code= new UnionCodeGroup(&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; new AllMembershipCondition(),&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; new PolicyStatement(permission, PolicyStatementAttribute.Nothing)); &lt;/pre&gt;
&lt;ul&gt;
&lt;li&gt;
give full trust to each test framework assembly:&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;StrongName strongName = &lt;a href="http://blogs.msdn.com/shawnfa/archive/2005/08/08/449050.aspx" target=_blank&gt;CreateStrongName&lt;/a&gt;(typeof(TestFixtureAttribute).Assembly);&lt;br&gt;
PermissionSet fullTrust = new PermissionSet(PermissionState.Unrestricted);&lt;br&gt;
UnionCodeGroup fullTrustCode = new UnionCodeGroup(&lt;br&gt;
new StrongNameMembershipCondition(strongName.PublicKey, strongName.Name, strongName.Version), 
&lt;br&gt;
new PolicyStatement(fullTrust, PolicyStatementAttribute.Nothing));&lt;br&gt;
code.AddChild(fullTrustCode);&lt;/pre&gt;
&lt;ul&gt;
&lt;li&gt;
Assign the policy to the AppDomain&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;PolicyLevel policy = PolicyLevel.CreateAppDomainLevel();&lt;br&gt;
policy.RootCodeGroup = code;&lt;br&gt;
domain.SetAppDomainPolicy(policy); &lt;/pre&gt;
&lt;p&gt;
This is basically it (the rest of the details are left as an exercise :)). 
&lt;p&gt;
&lt;strong&gt;Let them call you&lt;/strong&gt; 
&lt;p&gt;
Make sure to add the &lt;strong&gt;AllowPartiallyTrustedCallers&lt;/strong&gt; to the test framework
assembly otherwize users won't be allowed to call into it... 
&lt;p&gt;
&lt;strong&gt;A twist...&lt;/strong&gt; 
&lt;p&gt;
Pex is bit invasive when it comes to partial trust. Pex rewrites the IL at runtime
and turns all method bodies into... &lt;strong&gt;unsafe&lt;/strong&gt; code (that is unverifiable).
At this point, any will not run because of the SkipVerification permission. 
&lt;p&gt;
No problemo, just add it to the permissionset: &lt;pre&gt;permission.AddPermission(&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; new SecurityPermission(SecurityPermissionFlag.SkipVerification)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; ); &lt;/pre&gt;
&lt;p&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=3ef9c121-6529-4e80-ac88-84ee73ab2b73" /&gt;</description>
      <comments>http://blog.dotnetwiki.org/CommentView,guid,3ef9c121-6529-4e80-ac88-84ee73ab2b73.aspx</comments>
      <category>MbUnit</category>
      <category>Pex</category>
      <category>Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.dotnetwiki.org/Trackback.aspx?guid=f24a0dac-29c8-440b-9034-5fc37d5d471b</trackback:ping>
      <pingback:server>http://blog.dotnetwiki.org/pingback.aspx</pingback:server>
      <pingback:target>http://blog.dotnetwiki.org/PermaLink,guid,f24a0dac-29c8-440b-9034-5fc37d5d471b.aspx</pingback:target>
      <dc:creator>Jonathan de Halleux</dc:creator>
      <wfw:comment>http://blog.dotnetwiki.org/CommentView,guid,f24a0dac-29c8-440b-9034-5fc37d5d471b.aspx</wfw:comment>
      <wfw:commentRss>http://blog.dotnetwiki.org/SyndicationService.asmx/GetEntryCommentsRss?guid=f24a0dac-29c8-440b-9034-5fc37d5d471b</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
A common requirement for unit test framework is the ability to test<strong> internal</strong> types.
</p>
        <p>
          <strong>That's easy! use </strong>
          <a href="http://msdn2.microsoft.com/en-us/library/system.runtime.compilerservices.internalsvisibletoattribute.aspx" target="_blank">
            <strong>InternalsVisibleToAttribute</strong>
          </a>
        </p>
        <p>
With .Net 2.0 and up, this is a fairly easy task thanks to the InternalsVisibleToAttribute: add it
to the product assembly to give 'visibility rights' to the test assembly. 
</p>
        <blockquote>
          <pre>// in assembly Foo<br />
internal class Foo {}<br />
// giving visibility rights to the Foo.Tests assembly<br />
[assembly:InternalsVisibleTo("Foo.Tests")]</pre>
        </blockquote>
        <p>
On the test assembly side, this works because unit test are 'closed' methods which
do not expose any internal types.
</p>
        <blockquote>
          <pre>
            <br />
[Test]<br />
public void FooTest() {<br />
    Foo foo = new Foo(); // we're using the internal type Foo 
<br />
                         //
but it's hidden in the unit test<br />
}</pre>
        </blockquote>
        <p>
          <strong>What about parameterized tests? Make them internal as well</strong>
        </p>
        <p>
If one of the parameters of the test is internal, the test method will have to be
internal as well in order to compile:
</p>
        <blockquote>
          <pre>[PexTest]<br />
internal void FooTest(Foo foo) {<br />
   ... 
<br />
}</pre>
        </blockquote>
        <p>
Not pretty but still gets the job done. Pex will generate public unit test methods
that invoke the internal parameterized test method, and we'll be happy:
</p>
        <blockquote>
          <pre>[Test]<br />
public void FooTest_12345() {<br />
    this.FooTest(null);<br />
}</pre>
        </blockquote>
        <p>
          <strong>What about <a href="http://blog.dotnetwiki.org/archive/2004/10/20/1212.aspx" target="_blank">MbUnit
RowTest</a>?</strong>
        </p>
        <p>
This issue was never faced by MbUnit RowTest because it only accepts intrinsic types
such as int, long, etc... Those types are obviously public :)
</p>
        <img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=f24a0dac-29c8-440b-9034-5fc37d5d471b" />
      </body>
      <title>Pex It: Testing Internals classes</title>
      <guid isPermaLink="false">http://blog.dotnetwiki.org/PermaLink,guid,f24a0dac-29c8-440b-9034-5fc37d5d471b.aspx</guid>
      <link>http://blog.dotnetwiki.org/2007/08/23/PexItTestingInternalsClasses.aspx</link>
      <pubDate>Thu, 23 Aug 2007 17:24:08 GMT</pubDate>
      <description>&lt;p&gt;
A common requirement for unit test framework is the ability to test&lt;strong&gt; internal&lt;/strong&gt; types.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;That's easy! use &lt;/strong&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/system.runtime.compilerservices.internalsvisibletoattribute.aspx" target=_blank&gt;&lt;strong&gt;InternalsVisibleToAttribute&lt;/strong&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
With .Net 2.0 and up, this is a fairly easy task thanks to the InternalsVisibleToAttribute:&amp;nbsp;add&amp;nbsp;it
to the product assembly to give 'visibility rights' to the test&amp;nbsp;assembly. 
&lt;/p&gt;
&lt;blockquote&gt;&lt;pre&gt;// in assembly Foo&lt;br&gt;
internal class Foo {}&lt;br&gt;
// giving visibility rights to the Foo.Tests assembly&lt;br&gt;
[assembly:InternalsVisibleTo("Foo.Tests")]&lt;/pre&gt;&lt;/blockquote&gt; 
&lt;p&gt;
On the test assembly side, this works because unit test are 'closed' methods which
do not expose any&amp;nbsp;internal types.
&lt;/p&gt;
&lt;blockquote&gt;&lt;pre&gt;
&lt;br&gt;
[Test]&lt;br&gt;
public void FooTest() {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; Foo foo = new Foo(); // we're using the internal type Foo 
&lt;br&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//
but it's hidden in the unit test&lt;br&gt;
}&lt;/pre&gt;&lt;/blockquote&gt; 
&lt;p&gt;
&lt;strong&gt;What about parameterized tests? Make them internal as well&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
If one of the parameters of the test is internal, the test method will have to be
internal as well in order to compile:
&lt;/p&gt;
&lt;blockquote&gt;&lt;pre&gt;[PexTest]&lt;br&gt;
internal void FooTest(Foo foo) {&lt;br&gt;
&amp;nbsp;&amp;nbsp; ... 
&lt;br&gt;
}&lt;/pre&gt;&lt;/blockquote&gt; 
&lt;p&gt;
Not pretty but still gets the job done. Pex will generate public unit test methods
that invoke the internal parameterized test method, and we'll be happy:
&lt;/p&gt;
&lt;blockquote&gt;&lt;pre&gt;[Test]&lt;br&gt;
public void FooTest_12345() {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; this.FooTest(null);&lt;br&gt;
}&lt;/pre&gt;&lt;/blockquote&gt; 
&lt;p&gt;
&lt;strong&gt;What about &lt;a href="http://blog.dotnetwiki.org/archive/2004/10/20/1212.aspx" target=_blank&gt;MbUnit
RowTest&lt;/a&gt;?&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
This issue was never faced by MbUnit RowTest because it only accepts intrinsic types
such as int, long, etc... Those types are obviously public :)
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=f24a0dac-29c8-440b-9034-5fc37d5d471b" /&gt;</description>
      <comments>http://blog.dotnetwiki.org/CommentView,guid,f24a0dac-29c8-440b-9034-5fc37d5d471b.aspx</comments>
      <category>MbUnit</category>
      <category>Pex</category>
      <category>Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.dotnetwiki.org/Trackback.aspx?guid=ebbeae3f-9e53-4f67-9d30-6d8a1d2c4903</trackback:ping>
      <pingback:server>http://blog.dotnetwiki.org/pingback.aspx</pingback:server>
      <pingback:target>http://blog.dotnetwiki.org/PermaLink,guid,ebbeae3f-9e53-4f67-9d30-6d8a1d2c4903.aspx</pingback:target>
      <dc:creator>Jonathan de Halleux</dc:creator>
      <wfw:comment>http://blog.dotnetwiki.org/CommentView,guid,ebbeae3f-9e53-4f67-9d30-6d8a1d2c4903.aspx</wfw:comment>
      <wfw:commentRss>http://blog.dotnetwiki.org/SyndicationService.asmx/GetEntryCommentsRss?guid=ebbeae3f-9e53-4f67-9d30-6d8a1d2c4903</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
          <strong>
            <a href="http://research.microsoft.com/pex" target="_blank">Pex</a> can analyze
regular expressions*** </strong>and generate strings that matches them<strong> automatically</strong>! 
</p>
        <p>
What does it mean? Well, maybe, somewhere deep in your code your are validating some
string with a regex (for example a url). In order to test the validation code, one
needs to craft inputs that does not match (easy) and matches (harder) the regex.
</p>
        <p>
          <strong>Let Pex do it:</strong>
        </p>
        <p>
So what if Pex could be smart enough to understand a regex and craft inputs accordingly?
In the example below, it would be very hard for a random generate to generate a
string that matches the regex.
</p>
        <blockquote>
          <p>
            <font face="Courier New">[PexTest]<br />
public void Url([PexAssumeIsNotNull]string s)<br />
{<br />
    if (Regex.IsMatch(s, "(?&lt;Protocol&gt;\w+):\/\/(?&lt;Domain&gt;[\w@][\w.:@]+)\/?[\w\.?=%&amp;=\-@/$,]*"))<br />
        throw new PexCoverThisException(); // random
won't find this<br />
} </font>
          </p>
        </blockquote>
        <p>
          <font face="Courier New">""</font> would be a failing match and<font face="Courier New"> foo://foo.com</font> a
good match. (To generate the correct match, my brain simulated the regex automaton
and estimated one possible path). Interestingly, Pex generates....
</p>
        <blockquote>
          <p>
            <font face="Courier New">
              <strong>Ä€://Ä€Ä</strong>
            </font>
          </p>
        </blockquote>
        <p>
Pretty ugly... but correct! This reminds us that while regex are used to validate
input, what they'll let through is sometimes scary.
</p>
        <p>
          <strong>Compiled Regex + Pex = Love</strong>
        </p>
        <p>
The great part about supporting the regular expressions is that it comes for
free (almost) since<strong> Regex can be compiled to IL in .Net</strong>. When
the BCL generates the regex IL code, it effectily builds the automaton... which can
be analyzed by Pex!!!
</p>
        <blockquote>
          <p>
            <em>Refresher: Pex works analyzing the MSIL being executed.</em>
          </p>
        </blockquote>
        <p>
          <strong>Hey but not all Regex are compiled!</strong>
        </p>
        <p>
That's true. Compiling the regex is optional so Pex needs to do a little bit of 'plumbing'
to make sure all regular expressions are compiled. This is simply done by <strong>substituting</strong> the
real .ctor of the <font face="Courier New">Regex</font> class with a customized version
that compiles the regex. I'll talk about substitutions deeper in the future.
</p>
        <p>
*** Of course, the bigger the regex is, the harder it is going to be for Pex to craft
a successful match.
</p>
        <img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=ebbeae3f-9e53-4f67-9d30-6d8a1d2c4903" />
      </body>
      <title>Pex It: Regex Madness</title>
      <guid isPermaLink="false">http://blog.dotnetwiki.org/PermaLink,guid,ebbeae3f-9e53-4f67-9d30-6d8a1d2c4903.aspx</guid>
      <link>http://blog.dotnetwiki.org/2007/07/14/PexItRegexMadness.aspx</link>
      <pubDate>Sat, 14 Jul 2007 21:49:53 GMT</pubDate>
      <description>&lt;p&gt;
&lt;strong&gt;&lt;a href="http://research.microsoft.com/pex" target=_blank&gt;Pex&lt;/a&gt; can analyze
regular expressions*** &lt;/strong&gt;and generate strings that matches them&lt;strong&gt; automatically&lt;/strong&gt;! 
&lt;/p&gt;
&lt;p&gt;
What does it mean? Well, maybe, somewhere deep in your code your are validating some
string with a regex (for example a url). In order to test the validation code,&amp;nbsp;one
needs to craft inputs that does not match (easy) and matches (harder) the regex.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Let Pex do it:&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
So what if Pex could be smart enough to understand a regex and craft inputs accordingly?
In the example below, it would be very hard for a random generate to generate&amp;nbsp;a
string that matches the regex.
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
&lt;font face="Courier New"&gt;[PexTest]&lt;br&gt;
public void Url([PexAssumeIsNotNull]string s)&lt;br&gt;
{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; if (Regex.IsMatch(s, "(?&amp;lt;Protocol&amp;gt;\w+):\/\/(?&amp;lt;Domain&amp;gt;[\w@][\w.:@]+)\/?[\w\.?=%&amp;amp;=\-@/$,]*"))&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; throw new PexCoverThisException(); // random
won't find this&lt;br&gt;
} &lt;/font&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
&lt;font face="Courier New"&gt;""&lt;/font&gt; would be a failing match and&lt;font face="Courier New"&gt; foo://foo.com&lt;/font&gt; a
good match. (To generate the correct match,&amp;nbsp;my brain simulated the regex automaton
and estimated one possible path). Interestingly, Pex generates....
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
&lt;font face="Courier New"&gt;&lt;strong&gt;Ä€://Ä€Ä&lt;/strong&gt;&lt;/font&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
Pretty ugly... but correct! This reminds us that while regex are used to validate
input, what they'll let through is sometimes scary.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Compiled Regex + Pex = Love&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
The great part about&amp;nbsp;supporting the regular expressions is that it comes for
free (almost) since&lt;strong&gt; Regex can be compiled to IL&amp;nbsp;in .Net&lt;/strong&gt;. When
the BCL generates the regex IL code, it effectily builds the automaton... which can
be analyzed by Pex!!!
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
&lt;em&gt;Refresher: Pex works analyzing the MSIL&amp;nbsp;being executed.&lt;/em&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
&lt;strong&gt;Hey but not all Regex are compiled!&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
That's true. Compiling the regex is optional so Pex needs to do a little bit of 'plumbing'
to make sure all regular expressions are compiled. This is simply done by &lt;strong&gt;substituting&lt;/strong&gt; the
real .ctor of the &lt;font face="Courier New"&gt;Regex&lt;/font&gt; class with a customized version
that compiles the regex. I'll talk about substitutions deeper in the future.
&lt;/p&gt;
&lt;p&gt;
*** Of course, the bigger the regex is, the harder it is going to be for Pex to craft
a successful match.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=ebbeae3f-9e53-4f67-9d30-6d8a1d2c4903" /&gt;</description>
      <comments>http://blog.dotnetwiki.org/CommentView,guid,ebbeae3f-9e53-4f67-9d30-6d8a1d2c4903.aspx</comments>
      <category>Pex</category>
      <category>Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.dotnetwiki.org/Trackback.aspx?guid=a5d241f6-aa07-4968-beb1-b76c78ffa740</trackback:ping>
      <pingback:server>http://blog.dotnetwiki.org/pingback.aspx</pingback:server>
      <pingback:target>http://blog.dotnetwiki.org/PermaLink,guid,a5d241f6-aa07-4968-beb1-b76c78ffa740.aspx</pingback:target>
      <dc:creator>Jonathan de Halleux</dc:creator>
      <wfw:comment>http://blog.dotnetwiki.org/CommentView,guid,a5d241f6-aa07-4968-beb1-b76c78ffa740.aspx</wfw:comment>
      <wfw:commentRss>http://blog.dotnetwiki.org/SyndicationService.asmx/GetEntryCommentsRss?guid=a5d241f6-aa07-4968-beb1-b76c78ffa740</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
One of the problem with Pex is that.... it's yet another dependency in your test project.
Pex has it's own attributes which makes it very difficult to 'strip it out' of
the test source. Many teams won't allow to check-in assemblies, they won't
install external components on the build machine and just forget about touching the
source code!
</p>
        <p>
So how do you strip Pex?
</p>
        <p>
          <strong>In "Condition" lies the answer</strong>
        </p>
        <p>
An elegant solution uses a bit of Reflection, CodeDom and the MSBuild <em>Condition</em> attribute:
generate Attribute stubs (shadows) and bind them to the project.
</p>
        <p>
Pex modifies the project file and uses MSBuild conditions to conditionally include
files and assembly references in the generated test project. 
</p>
        <ul>
          <li>
A boolean property <font face="Courier New">PexShadows</font> to controls the
shadowing state: <font face="Courier New">true</font> if shadowed, <font face="Courier New">false</font> or
missing otherwize. 
</li>
        </ul>
        <pre>&lt;Project DefaultTargets="Build" ...&gt;
  &lt;PropertyGroup&gt;
    ...
  <strong> &lt;PexShadows&gt;true&lt;/PexShadows&gt; </strong>&lt;/PropertyGroup&gt; </pre>
        <ul>
          <ul>
            <li>
            </li>
          </ul>
          <li>
a conditional reference to <font face="Courier New">Microsoft.Pex.Framework</font>: 
</li>
        </ul>
        <pre>    &lt;Reference 
        Include="Microsoft.Pex.Framework" 
        <strong>Condition="$(PexShadows)
!= 'true'"</strong> /&gt; </pre>
        <p>
when the property <font face="Courier New">PexShadows</font> evalutes to true, the <font face="Courier New">Microsoft.Pex.Framework</font> assembly
is not referenced anymore. 
</p>
        <ul>
          <li>
a file containing all the custom attributes 'stubs' (for all attributes in the <font face="Courier New">Microsoft.Pex.Framework</font>)
is generated (automatically of course :)). Pex also dumps the source of several other
helper classes. Each generated file is added to the test project conditionally: 
</li>
        </ul>
        <pre>    &lt;Compile Include="Properties\PexAttributes.cs" <strong>Condition="$(PexShadows)
== 'true'"</strong>&gt; &lt;AutoGen&gt;True&lt;/AutoGen&gt; &lt;DependentUpon&gt;AssemblyInfo.cs&lt;/DependentUpon&gt;
&lt;/Compile&gt;</pre>
        <p>
That's it! Flip the value of <font face="Courier New">PexShadows</font> to bind/unbing
your test project from Pex.
</p>
        <p>
          <strong>So what does Visual Studio says ?</strong>
        </p>
        <p>
Visual Studio is totally fooled by the maneuver. It shows the conditional files and
references as if nothing happened. Add a menu item in the project context menu to
switch it on and off and we are good to go :)
</p>
        <img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=a5d241f6-aa07-4968-beb1-b76c78ffa740" />
      </body>
      <title>Pex it: Moving in the shadows (and out)</title>
      <guid isPermaLink="false">http://blog.dotnetwiki.org/PermaLink,guid,a5d241f6-aa07-4968-beb1-b76c78ffa740.aspx</guid>
      <link>http://blog.dotnetwiki.org/2007/07/04/PexItMovingInTheShadowsAndOut.aspx</link>
      <pubDate>Wed, 04 Jul 2007 05:28:12 GMT</pubDate>
      <description>&lt;p&gt;
One of the problem with Pex is that.... it's yet another dependency in your test project.
Pex has it's own attributes which makes it very difficult to&amp;nbsp;'strip it out' of
the test&amp;nbsp;source.&amp;nbsp;Many teams won't allow to check-in assemblies, they won't
install external components on the build machine and just forget about touching the
source code!
&lt;/p&gt;
&lt;p&gt;
So how do you strip Pex?
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;In "Condition" lies the answer&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
An elegant solution uses a bit of Reflection, CodeDom and the MSBuild &lt;em&gt;Condition&lt;/em&gt; attribute:
generate Attribute stubs (shadows) and bind them to the project.
&lt;/p&gt;
&lt;p&gt;
Pex modifies the project file and uses MSBuild conditions to conditionally include
files and assembly references in the generated test project. 
&lt;ul&gt;
&lt;li&gt;
A boolean property &lt;font face="Courier New"&gt;PexShadows&lt;/font&gt;&amp;nbsp;to controls the
shadowing state: &lt;font face="Courier New"&gt;true&lt;/font&gt; if shadowed, &lt;font face="Courier New"&gt;false&lt;/font&gt; or
missing otherwize. 
&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;&amp;lt;Project DefaultTargets="Build" ...&amp;gt;
  &amp;lt;PropertyGroup&amp;gt;
    ...
  &lt;strong&gt; &amp;lt;PexShadows&amp;gt;true&amp;lt;/PexShadows&amp;gt; &lt;/strong&gt;&amp;lt;/PropertyGroup&amp;gt; &lt;/pre&gt;
&lt;ul&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt;
a conditional reference to &lt;font face="Courier New"&gt;Microsoft.Pex.Framework&lt;/font&gt;: 
&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;    &amp;lt;Reference 
        Include="Microsoft.Pex.Framework" 
        &lt;strong&gt;Condition="$(PexShadows)
!= 'true'"&lt;/strong&gt; /&amp;gt; &lt;/pre&gt;
&lt;p&gt;
when the property &lt;font face="Courier New"&gt;PexShadows&lt;/font&gt; evalutes to true, the &lt;font face="Courier New"&gt;Microsoft.Pex.Framework&lt;/font&gt; assembly
is not referenced anymore. 
&lt;ul&gt;
&lt;li&gt;
a file containing all the custom attributes 'stubs' (for all attributes in the &lt;font face="Courier New"&gt;Microsoft.Pex.Framework&lt;/font&gt;)
is generated (automatically of course :)). Pex also dumps the source of several other
helper classes. Each generated file is added to the test project conditionally: 
&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;    &amp;lt;Compile Include="Properties\PexAttributes.cs" &lt;strong&gt;Condition="$(PexShadows)
== 'true'"&lt;/strong&gt;&amp;gt; &amp;lt;AutoGen&amp;gt;True&amp;lt;/AutoGen&amp;gt; &amp;lt;DependentUpon&amp;gt;AssemblyInfo.cs&amp;lt;/DependentUpon&amp;gt;
&amp;lt;/Compile&amp;gt;&lt;/pre&gt;
&lt;p&gt;
That's it! Flip the value of &lt;font face="Courier New"&gt;PexShadows&lt;/font&gt; to bind/unbing
your test project from Pex.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;So what does Visual Studio says ?&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
Visual Studio is totally fooled by the maneuver. It shows the conditional files and
references as if nothing happened. Add a menu item in the project context menu to
switch it on and off and we are good to go :)
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=a5d241f6-aa07-4968-beb1-b76c78ffa740" /&gt;</description>
      <comments>http://blog.dotnetwiki.org/CommentView,guid,a5d241f6-aa07-4968-beb1-b76c78ffa740.aspx</comments>
      <category>Pex</category>
      <category>Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.dotnetwiki.org/Trackback.aspx?guid=37630fe2-84c0-4267-8682-b77531b75fa1</trackback:ping>
      <pingback:server>http://blog.dotnetwiki.org/pingback.aspx</pingback:server>
      <pingback:target>http://blog.dotnetwiki.org/PermaLink,guid,37630fe2-84c0-4267-8682-b77531b75fa1.aspx</pingback:target>
      <dc:creator>Jonathan de Halleux</dc:creator>
      <wfw:comment>http://blog.dotnetwiki.org/CommentView,guid,37630fe2-84c0-4267-8682-b77531b75fa1.aspx</wfw:comment>
      <wfw:commentRss>http://blog.dotnetwiki.org/SyndicationService.asmx/GetEntryCommentsRss?guid=37630fe2-84c0-4267-8682-b77531b75fa1</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
In <a href="http://research.microsoft.com/pex">Pex</a>, we added the possibility to specify
the <strong>type under test</strong> of a given fixture:
</p>
        <blockquote>
          <pre>public class Account {...}<br /><br /><br />
[TestFixture, PexClass<strong>(typeof(Account))</strong>]<br />
public class AccountTest {...}  </pre>
        </blockquote>
        <p>
That's nice but why would it be useful... Beyond the fact that it clearly expresses
the 'target' of the fixture, this kind of information can be leverage by tools like
Pex. 
</p>
        <p>
For example, since we know that <font face="Courier New">Account</font> is the type
under test, we can tune Pex to<strong> prioritize the exploration</strong> of the
Account type.
</p>
        <p>
Another interesting side effect is the <strong>targeted code coverage data</strong>.
Instead of getting coverage information over the entire assembly, we can directly
provide coverage over the type under test: the <font face="Courier New">AccountTest</font> covered
xx% of <font face="Courier New">Account</font>.
</p>
        <p>
Still toying around the concept, one can add a special filtering mode to the command
line to execute all tests that target <font face="Courier New">'Account'</font>:
</p>
        <blockquote>
          <pre>pex.exe /type-under-test:Account Bank.Tests.dll</pre>
        </blockquote>
        <img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=37630fe2-84c0-4267-8682-b77531b75fa1" />
      </body>
      <title>Pex It: Test-to-Code Binding</title>
      <guid isPermaLink="false">http://blog.dotnetwiki.org/PermaLink,guid,37630fe2-84c0-4267-8682-b77531b75fa1.aspx</guid>
      <link>http://blog.dotnetwiki.org/2007/06/30/PexItTesttoCodeBinding.aspx</link>
      <pubDate>Sat, 30 Jun 2007 07:23:24 GMT</pubDate>
      <description>&lt;p&gt;
In &lt;a href="http://research.microsoft.com/pex"&gt;Pex&lt;/a&gt;, we added the possibility to&amp;nbsp;specify
the &lt;strong&gt;type under test&lt;/strong&gt; of a given fixture:
&lt;/p&gt;
&lt;blockquote&gt;&lt;pre&gt;public class Account {...}&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
[TestFixture, PexClass&lt;strong&gt;(typeof(Account))&lt;/strong&gt;]&lt;br&gt;
public class AccountTest {...} &amp;nbsp;&lt;/pre&gt;&lt;/blockquote&gt; 
&lt;p&gt;
That's nice but why would it be useful... Beyond the fact that it clearly expresses
the 'target' of the fixture, this kind of information can be leverage by tools like
Pex. 
&lt;/p&gt;
&lt;p&gt;
For example, since we know that &lt;font face="Courier New"&gt;Account&lt;/font&gt; is the type
under test, we can tune Pex to&lt;strong&gt; prioritize the exploration&lt;/strong&gt; of the
Account type.
&lt;/p&gt;
&lt;p&gt;
Another interesting side effect is the &lt;strong&gt;targeted code coverage data&lt;/strong&gt;.
Instead of getting coverage information over the entire assembly, we can directly
provide coverage over the type under test: the &lt;font face="Courier New"&gt;AccountTest&lt;/font&gt; covered
xx% of &lt;font face="Courier New"&gt;Account&lt;/font&gt;.
&lt;/p&gt;
&lt;p&gt;
Still toying around the concept, one can add a special filtering mode to the command
line to execute all tests that target &lt;font face="Courier New"&gt;'Account'&lt;/font&gt;:
&lt;/p&gt;
&lt;blockquote&gt;&lt;pre&gt;pex.exe /type-under-test:Account Bank.Tests.dll&lt;/pre&gt;&lt;/blockquote&gt;&lt;img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=37630fe2-84c0-4267-8682-b77531b75fa1" /&gt;</description>
      <comments>http://blog.dotnetwiki.org/CommentView,guid,37630fe2-84c0-4267-8682-b77531b75fa1.aspx</comments>
      <category>Pex</category>
      <category>Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.dotnetwiki.org/Trackback.aspx?guid=eea10783-b616-42fb-93b3-b6e06e3cba0b</trackback:ping>
      <pingback:server>http://blog.dotnetwiki.org/pingback.aspx</pingback:server>
      <pingback:target>http://blog.dotnetwiki.org/PermaLink,guid,eea10783-b616-42fb-93b3-b6e06e3cba0b.aspx</pingback:target>
      <dc:creator>Jonathan de Halleux</dc:creator>
      <wfw:comment>http://blog.dotnetwiki.org/CommentView,guid,eea10783-b616-42fb-93b3-b6e06e3cba0b.aspx</wfw:comment>
      <wfw:commentRss>http://blog.dotnetwiki.org/SyndicationService.asmx/GetEntryCommentsRss?guid=eea10783-b616-42fb-93b3-b6e06e3cba0b</wfw:commentRss>
      <slash:comments>3</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
.Net 2.0 has been out for a while and it seems that 'generics' have not made it into
unit test frameworks (that I know of). When I write unit tests for generics, I don't
want to have to instantiate them! 
<br /></p>
        <p>
For example, if I have an generic interface,
</p>
        <blockquote dir="ltr" style="MARGIN-RIGHT: 0px">
          <pre>interface IFoo&lt;T&gt; {...}</pre>
        </blockquote>
        <p dir="ltr">
then I'd really like to write this kind of test and let the test framework figure
out an interresting instantiation (i.e. choice of T):
</p>
        <blockquote dir="ltr" style="MARGIN-RIGHT: 0px">
          <pre>[Test]<br />
public void Test&lt;T&gt;(IFoo&lt;T&gt; foo) { ... }</pre>
        </blockquote>
        <p>
In the example above, <em>System.Object</em> can be trivially used to instantiate <em>IFoo&lt;T&gt;.</em> Of
course, things get more interresting when mixing type argument, method argument and
constraints :)
</p>
        <blockquote dir="ltr" style="MARGIN-RIGHT: 0px">
          <pre>interface IFoo&lt;T&gt;<br />
{<br />
    R Bar&lt;R&gt;(T t)<br />
     where R : IEnumerable&lt;T&gt;<br />
}</pre>
        </blockquote>
        <p>
In <a href="http://research.microsoft.com/pex">Pex</a>, we've started to look at this
problem... stay tuned.
</p>
        <img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=eea10783-b616-42fb-93b3-b6e06e3cba0b" />
      </body>
      <title>Unit Tests&amp;lt;T&amp;gt; ?</title>
      <guid isPermaLink="false">http://blog.dotnetwiki.org/PermaLink,guid,eea10783-b616-42fb-93b3-b6e06e3cba0b.aspx</guid>
      <link>http://blog.dotnetwiki.org/2007/05/27/UnitTestsltTgt.aspx</link>
      <pubDate>Sun, 27 May 2007 06:09:00 GMT</pubDate>
      <description>&lt;p&gt;
.Net 2.0 has been out for a while and it seems that 'generics' have not made it into
unit test frameworks (that I know of). When I write unit tests for generics, I don't
want to have to instantiate them! 
&lt;br&gt;
&lt;/p&gt;
&lt;p&gt;
For example, if I have an generic interface,
&lt;/p&gt;
&lt;blockquote dir=ltr style="MARGIN-RIGHT: 0px"&gt;&lt;pre&gt;interface IFoo&amp;lt;T&amp;gt; {...}&lt;/pre&gt;&lt;/blockquote&gt; 
&lt;p dir=ltr&gt;
then I'd really like to write this kind of test and let the test framework figure
out an interresting instantiation (i.e. choice of T):
&lt;/p&gt;
&lt;blockquote dir=ltr style="MARGIN-RIGHT: 0px"&gt;&lt;pre&gt;[Test]&lt;br&gt;
public void Test&amp;lt;T&amp;gt;(IFoo&amp;lt;T&amp;gt; foo) { ... }&lt;/pre&gt;&lt;/blockquote&gt; 
&lt;p&gt;
In the example above, &lt;em&gt;System.Object&lt;/em&gt; can be trivially used to instantiate &lt;em&gt;IFoo&amp;lt;T&amp;gt;.&lt;/em&gt; Of
course, things get more interresting when mixing type argument, method argument and
constraints :)
&lt;/p&gt;
&lt;blockquote dir=ltr style="MARGIN-RIGHT: 0px"&gt;&lt;pre&gt;interface IFoo&amp;lt;T&amp;gt;&lt;br&gt;
{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; R Bar&amp;lt;R&amp;gt;(T t)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; where R : IEnumerable&amp;lt;T&amp;gt;&lt;br&gt;
}&lt;/pre&gt;&lt;/blockquote&gt; 
&lt;p&gt;
In &lt;a href="http://research.microsoft.com/pex"&gt;Pex&lt;/a&gt;, we've started to look at this
problem... stay tuned.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=eea10783-b616-42fb-93b3-b6e06e3cba0b" /&gt;</description>
      <comments>http://blog.dotnetwiki.org/CommentView,guid,eea10783-b616-42fb-93b3-b6e06e3cba0b.aspx</comments>
      <category>MbUnit</category>
      <category>Pex</category>
      <category>Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.dotnetwiki.org/Trackback.aspx?guid=28e392f4-685d-4b6b-b01a-b731eeb185bc</trackback:ping>
      <pingback:server>http://blog.dotnetwiki.org/pingback.aspx</pingback:server>
      <pingback:target>http://blog.dotnetwiki.org/PermaLink,guid,28e392f4-685d-4b6b-b01a-b731eeb185bc.aspx</pingback:target>
      <dc:creator>Jonathan de Halleux</dc:creator>
      <wfw:comment>http://blog.dotnetwiki.org/CommentView,guid,28e392f4-685d-4b6b-b01a-b731eeb185bc.aspx</wfw:comment>
      <wfw:commentRss>http://blog.dotnetwiki.org/SyndicationService.asmx/GetEntryCommentsRss?guid=28e392f4-685d-4b6b-b01a-b731eeb185bc</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
With <a href="http://research.microsoft.com/pex">parameterized unit tests</a>, it
is not uncommon to generate a large number of exceptions. An basic exception log usually
looks like this: a small message and the stack trace.
</p>
        <p>
          <img src="http://blog.dotnetwiki.org/content/binary/rawstacktrace.png" border="0" />
        </p>
        <p>
Whith this kind of output, a lot of work is still left to the user since he has, *for
each frame*,  to manually open the source file and move to the line
refered by the trace. 
</p>
        <p>
          <strong>Give me the source context!</strong>
        </p>
        <p>
In the <a href="http://research.microsoft.com/pex">Pex</a> reports we added a couple
lines of code to read the source for each frame and display it in the reports (no
rocket science). The cool thing is that you now can get a pretty good idea of what
happened <strong>without leaving the test report</strong>. Multiply that by dozens
of exceptions and you've won a loooot of time.
</p>
        <p>
Here are some screenshots to illustrate this: an exception was thrown in some arcane
method. The error message is not really useful (as usual).
</p>
        <p>
          <img src="http://blog.dotnetwiki.org/content/binary/nicestacktrace.png" border="0" />
        </p>
        <p>
If we expand the source and actually <strong>see</strong> the code, things become
much clearer...
</p>
        <p>
          <img src="http://blog.dotnetwiki.org/content/binary/niceexpandedstacktrace.png" border="0" />
        </p>
        <img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=28e392f4-685d-4b6b-b01a-b731eeb185bc" />
      </body>
      <title>Pimp my StackTrace</title>
      <guid isPermaLink="false">http://blog.dotnetwiki.org/PermaLink,guid,28e392f4-685d-4b6b-b01a-b731eeb185bc.aspx</guid>
      <link>http://blog.dotnetwiki.org/2007/04/20/PimpMyStackTrace.aspx</link>
      <pubDate>Fri, 20 Apr 2007 00:12:40 GMT</pubDate>
      <description>&lt;p&gt;
With &lt;a href="http://research.microsoft.com/pex"&gt;parameterized unit tests&lt;/a&gt;, it
is not uncommon to generate a large number of exceptions. An basic exception log usually
looks like this: a small message and the stack trace.
&lt;/p&gt;
&lt;p&gt;
&lt;img src="http://blog.dotnetwiki.org/content/binary/rawstacktrace.png" border=0&gt;
&lt;/p&gt;
&lt;p&gt;
Whith this kind of output, a lot of work is still left to the user since he has, *for
each&amp;nbsp;frame*,&amp;nbsp;&amp;nbsp;to manually open the source file and move to the line
refered by the trace. 
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Give me the source context!&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
In the &lt;a href="http://research.microsoft.com/pex"&gt;Pex&lt;/a&gt; reports we added a couple
lines of code to read the source for each frame and display it in the reports (no
rocket science). The cool thing is that you now can get a pretty good idea of what
happened &lt;strong&gt;without leaving the test report&lt;/strong&gt;. Multiply that by dozens
of exceptions and you've won a loooot of time.
&lt;/p&gt;
&lt;p&gt;
Here are some screenshots to illustrate this: an exception was thrown in some arcane
method. The error message is not really useful (as usual).
&lt;/p&gt;
&lt;p&gt;
&lt;img src="http://blog.dotnetwiki.org/content/binary/nicestacktrace.png" border=0&gt;
&lt;/p&gt;
&lt;p&gt;
If we expand the source and actually &lt;strong&gt;see&lt;/strong&gt; the code, things become
much clearer...
&lt;/p&gt;
&lt;p&gt;
&lt;img src="http://blog.dotnetwiki.org/content/binary/niceexpandedstacktrace.png" border=0&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=28e392f4-685d-4b6b-b01a-b731eeb185bc" /&gt;</description>
      <comments>http://blog.dotnetwiki.org/CommentView,guid,28e392f4-685d-4b6b-b01a-b731eeb185bc.aspx</comments>
      <category>Pex</category>
      <category>Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.dotnetwiki.org/Trackback.aspx?guid=d6d459a4-3da3-4fc3-b52c-b23b0bbe949e</trackback:ping>
      <pingback:server>http://blog.dotnetwiki.org/pingback.aspx</pingback:server>
      <pingback:target>http://blog.dotnetwiki.org/PermaLink,guid,d6d459a4-3da3-4fc3-b52c-b23b0bbe949e.aspx</pingback:target>
      <dc:creator>Jonathan de Halleux</dc:creator>
      <wfw:comment>http://blog.dotnetwiki.org/CommentView,guid,d6d459a4-3da3-4fc3-b52c-b23b0bbe949e.aspx</wfw:comment>
      <wfw:commentRss>http://blog.dotnetwiki.org/SyndicationService.asmx/GetEntryCommentsRss?guid=d6d459a4-3da3-4fc3-b52c-b23b0bbe949e</wfw:commentRss>
      <slash:comments>2</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
          <a href="http://www.mbunit.com">MbUnit</a> supports different flavors of parameterized
unit tests: <a href="http://www.mertner.com/confluence/display/MbUnit/RowTestAttribute">RowTest</a>, <a href="http://www.mertner.com/confluence/display/MbUnit/CombinatorialTestAttribute">CombinatorialTest</a>,
etc... If you are already using those features, it would be very easy for
you to '<a href="http://research.microsoft.com/pex">pexify</a>' them:
</p>
        <pre>namespace MyTest<br />
{<br />
     using MbUnit.Framework;<br />
     using Microsoft.Pex.Framework;<br /><br />
     [TestFixture, <strong><font color="#ff0000">PexClass</font></strong>] 
<br />
     public <strong><font color="#ff0000">partial</font></strong>class
MyTestFixture 
<br />
{<br />
[RowTest]<br />
[Row("a", "b")]<br /><strong><font color="#ff0000">[PexTest]</font><br /></strong><br />
public void Test(string a, string b)<br />
{<br />
...<br />
}<br /><br /><br />
} 
<br />
}</pre>
        <p>
Isn't this nice? :)
</p>
        <p>
Some little notes:
</p>
        <ul>
          <li>
'partial' is helpfull to emit the generated unit test in the same class... but not
the same file. Pex also support another mode where partial is not required. 
</li>
          <li>
the Pex attributes do not 'interfere' with the MbUnit ones. Your unit tests will still
run exactly the same with MbUnit.</li>
        </ul>
        <img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=d6d459a4-3da3-4fc3-b52c-b23b0bbe949e" />
      </body>
      <title>Pex It: Reusing MbUnit parameterized unit tests with Pex</title>
      <guid isPermaLink="false">http://blog.dotnetwiki.org/PermaLink,guid,d6d459a4-3da3-4fc3-b52c-b23b0bbe949e.aspx</guid>
      <link>http://blog.dotnetwiki.org/2007/04/11/PexItReusingMbUnitParameterizedUnitTestsWithPex.aspx</link>
      <pubDate>Wed, 11 Apr 2007 04:47:27 GMT</pubDate>
      <description>&lt;p&gt;
&lt;a href="http://www.mbunit.com"&gt;MbUnit&lt;/a&gt; supports different flavors of parameterized
unit tests: &lt;a href="http://www.mertner.com/confluence/display/MbUnit/RowTestAttribute"&gt;RowTest&lt;/a&gt;, &lt;a href="http://www.mertner.com/confluence/display/MbUnit/CombinatorialTestAttribute"&gt;CombinatorialTest&lt;/a&gt;,
etc...&amp;nbsp;If you are already using those features, it&amp;nbsp;would be very easy for
you to '&lt;a href="http://research.microsoft.com/pex"&gt;pexify&lt;/a&gt;' them:
&lt;/p&gt;
&lt;pre&gt;namespace MyTest&lt;br&gt;
{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; using MbUnit.Framework;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; using Microsoft.Pex.Framework;&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [TestFixture, &lt;strong&gt;&lt;font color=#ff0000&gt;PexClass&lt;/font&gt;&lt;/strong&gt;] 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; public &lt;strong&gt;&lt;font color=#ff0000&gt;partial&lt;/font&gt; &lt;/strong&gt;class
MyTestFixture 
&lt;br&gt;
{&lt;br&gt;
[RowTest]&lt;br&gt;
[Row("a", "b")]&lt;br&gt;
&lt;strong&gt; &lt;font color=#ff0000&gt;[PexTest]&lt;/font&gt;
&lt;br&gt;
&lt;/strong&gt;
&lt;br&gt;
public void Test(string a, string b)&lt;br&gt;
{&lt;br&gt;
...&lt;br&gt;
}&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
} 
&lt;br&gt;
}&lt;/pre&gt;
&lt;p&gt;
Isn't this nice? :)
&lt;/p&gt;
&lt;p&gt;
Some little notes:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
'partial' is helpfull to emit the generated unit test in the same class... but not
the same file. Pex also support another mode where partial is not required. 
&lt;li&gt;
the Pex attributes do not 'interfere' with the MbUnit ones. Your unit tests will still
run exactly the same with MbUnit.&lt;/li&gt;
&lt;/ul&gt;
&lt;img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=d6d459a4-3da3-4fc3-b52c-b23b0bbe949e" /&gt;</description>
      <comments>http://blog.dotnetwiki.org/CommentView,guid,d6d459a4-3da3-4fc3-b52c-b23b0bbe949e.aspx</comments>
      <category>MbUnit</category>
      <category>Pex</category>
      <category>Testing</category>
    </item>
    <item>
      <trackback:ping>http://blog.dotnetwiki.org/Trackback.aspx?guid=336f3972-81f1-4dac-b122-94b699ffeaa2</trackback:ping>
      <pingback:server>http://blog.dotnetwiki.org/pingback.aspx</pingback:server>
      <pingback:target>http://blog.dotnetwiki.org/PermaLink,guid,336f3972-81f1-4dac-b122-94b699ffeaa2.aspx</pingback:target>
      <dc:creator>Jonathan de Halleux</dc:creator>
      <wfw:comment>http://blog.dotnetwiki.org/CommentView,guid,336f3972-81f1-4dac-b122-94b699ffeaa2.aspx</wfw:comment>
      <wfw:commentRss>http://blog.dotnetwiki.org/SyndicationService.asmx/GetEntryCommentsRss?guid=336f3972-81f1-4dac-b122-94b699ffeaa2</wfw:commentRss>
      <slash:comments>2</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
This is the first post about <a href="http://research.microsoft.com/Pex/">Pex </a>and
how to use it. Since Pex is a fairly large project, I'll probably stretch the content
of a large number of entries. Stay tunned...
</p>
        <p>
          <strong>Pex gives you Parameterized Unit Tests</strong>
        </p>
        <p>
Data-driven tests are not something new. They exist under different forms such as
MbUnit's <a href="http://www.mertner.com/confluence/display/MbUnit/RowTestAttribute">RowTest</a> or <a href="http://www.mertner.com/confluence/display/MbUnit/CombinatorialTestAttribute">CombinatorialTest</a>,
VSTS's <a href="http://msdn2.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.datasourceattribute(VS.80).aspx">DataSource</a>,
etc... So what's so special about Pex? The major difference is that <strong>Pex
finds the parameter inputs for you</strong> (and generates a unit test out of it).
</p>
        <p>
Whereas the user had to (smartly) guess a set of input for the data-driven tests,
Pex tries to compute the relevant inputs to those tests automatically (and may
also suggest fixes). 
</p>
        <blockquote dir="ltr" style="MARGIN-RIGHT: 0px">
          <p>
            <em>Note:</em> The way Pex finds the input will be covered in more details later.
In a couple words, Pex performs a systematic white box analysis of the program behavior.
It tries to generate a minimal test suite with maximum coverage.
</p>
        </blockquote>
        <p dir="ltr">
          <strong>Parameterized Unit Tests, what does it feel like?</strong>
        </p>
        <p dir="ltr">
Parameterized unit tests are methods with parameters. There's nothing magic about
that. Pex provides a set of custom attributes so that you can author them side-by-side
with classic unit tests:
</p>
        <blockquote dir="ltr" style="MARGIN-RIGHT: 0px">
          <p>
            <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">[TestClass, <strong>PexClass</strong>]
// VSTS fixture containing parameterized tests<br />
public class AccountTest<br />
{<br /></span>
            <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">   
// parameterized unit test<br />
    // account money should never be negative, for any 'money',
'withdraw'<br />
    [<strong>PexTest</strong>]<br />
    public void TransferFunds(int money, int withdraw) {...}<br />
}</span>
          </p>
        </blockquote>
        <p dir="ltr">
When Pex finds interresting data to feed the parameterized unit test, it generates
a unit test method that calls the parameterized unit test. This also means that once
the unit test has been generated, you do not need Pex anymore to run the repro.
</p>
        <blockquote dir="ltr" style="MARGIN-RIGHT: 0px">
          <p dir="ltr">
            <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">[TestClass,
PexClass] // VSTS fixture containing parameterized tests<br />
public class AccountTest<br />
{<br /></span>
            <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">   
[PexTest]<br />
    public void TransferFunds(int money, int withdraw) {...}<br />
    ...<br /><strong>    [TestMethod, GeneratedBy("Pex", "1.0.0.0")]<br />
    public void TransferFunds_12345() { this.TransferFunds(12, 13);
}</strong></span>
            <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
              <br />
}</span>
          </p>
        </blockquote>
        <p dir="ltr">
          <strong>Why do I need parameterized unit tests anyway?</strong>
        </p>
        <p dir="ltr">
A parameterized unit tests generally captures more program behaviors than a single
unit test which is like a micro-scenario. This will become more apparent when we start
looking at some examples. 
</p>
        <p dir="ltr">
If you were already using [RowTest] or [DataSource] as part of your testing, then
you will definitely like Pex.
</p>
        <img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=336f3972-81f1-4dac-b122-94b699ffeaa2" />
      </body>
      <title>Pex It: Parameterized Unit Tests</title>
      <guid isPermaLink="false">http://blog.dotnetwiki.org/PermaLink,guid,336f3972-81f1-4dac-b122-94b699ffeaa2.aspx</guid>
      <link>http://blog.dotnetwiki.org/2007/04/01/PexItParameterizedUnitTests.aspx</link>
      <pubDate>Sun, 01 Apr 2007 22:52:37 GMT</pubDate>
      <description>&lt;p&gt;
This is the first post about &lt;a href="http://research.microsoft.com/Pex/"&gt;Pex &lt;/a&gt;and
how to use it. Since Pex is a fairly large project, I'll probably stretch the content
of a large number of entries. Stay tunned...
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Pex gives you Parameterized Unit Tests&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
Data-driven tests are not something new. They exist under different forms such as
MbUnit's &lt;a href="http://www.mertner.com/confluence/display/MbUnit/RowTestAttribute"&gt;RowTest&lt;/a&gt;&amp;nbsp;or &lt;a href="http://www.mertner.com/confluence/display/MbUnit/CombinatorialTestAttribute"&gt;CombinatorialTest&lt;/a&gt;,
VSTS's &lt;a href="http://msdn2.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.datasourceattribute(VS.80).aspx"&gt;DataSource&lt;/a&gt;,
etc... So what's so special about Pex?&amp;nbsp;The major difference is that&amp;nbsp;&lt;strong&gt;Pex
finds the parameter inputs for you&lt;/strong&gt; (and generates a unit test out of it).
&lt;/p&gt;
&lt;p&gt;
Whereas the user had to (smartly) guess a set of input for the data-driven tests,
Pex tries to compute the relevant inputs&amp;nbsp;to those tests automatically (and may
also suggest fixes). 
&lt;/p&gt;
&lt;blockquote dir=ltr style="MARGIN-RIGHT: 0px"&gt; 
&lt;p&gt;
&lt;em&gt;Note:&lt;/em&gt; The way Pex finds the input will be covered in more details later.
In a couple words,&amp;nbsp;Pex performs a systematic white box analysis of the program&amp;nbsp;behavior.
It tries to generate a minimal test suite with maximum coverage.
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p dir=ltr&gt;
&lt;strong&gt;Parameterized Unit Tests, what does it feel like?&lt;/strong&gt;
&lt;/p&gt;
&lt;p dir=ltr&gt;
Parameterized unit tests are methods with parameters. There's nothing magic about
that. Pex provides a set of custom attributes so that you can author them side-by-side
with classic unit tests:
&lt;/p&gt;
&lt;blockquote dir=ltr style="MARGIN-RIGHT: 0px"&gt; 
&lt;p&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;[TestClass, &lt;strong&gt;PexClass&lt;/strong&gt;]
// VSTS fixture containing parameterized tests&lt;br&gt;
public class AccountTest&lt;br&gt;
{&lt;br&gt;
&lt;/span&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;
// parameterized unit test&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;// account money should never be negative, for any 'money',
'withdraw'&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; [&lt;strong&gt;PexTest&lt;/strong&gt;]&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public void TransferFunds(int money, int withdraw) {...}&lt;br&gt;
}&lt;/span&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p dir=ltr&gt;
When Pex finds interresting data to feed the parameterized unit test, it generates
a unit test method that calls the parameterized unit test. This also means that once
the unit test has been generated, you do not need Pex anymore to run the repro.
&lt;/p&gt;
&lt;blockquote dir=ltr style="MARGIN-RIGHT: 0px"&gt; 
&lt;p dir=ltr&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;[TestClass,
PexClass] // VSTS fixture containing parameterized tests&lt;br&gt;
public class AccountTest&lt;br&gt;
{&lt;br&gt;
&lt;/span&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;
[PexTest]&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public void TransferFunds(int money, int withdraw) {...}&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;...&lt;br&gt;
&lt;strong&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; [TestMethod, GeneratedBy("Pex", "1.0.0.0")]&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; public void TransferFunds_12345() { this.TransferFunds(12, 13);
}&lt;/strong&gt;&lt;/span&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;
&lt;br&gt;
}&lt;/span&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p dir=ltr&gt;
&lt;strong&gt;Why do I need parameterized unit tests anyway?&lt;/strong&gt;
&lt;/p&gt;
&lt;p dir=ltr&gt;
A parameterized unit tests generally captures more program behaviors than a single
unit test which is like a micro-scenario. This will become more apparent when we start
looking at some examples.&amp;nbsp;
&lt;/p&gt;
&lt;p dir=ltr&gt;
If you were already using [RowTest] or [DataSource] as part of your testing, then
you will definitely like Pex.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.dotnetwiki.org/aggbug.ashx?id=336f3972-81f1-4dac-b122-94b699ffeaa2" /&gt;</description>
      <comments>http://blog.dotnetwiki.org/CommentView,guid,336f3972-81f1-4dac-b122-94b699ffeaa2.aspx</comments>
      <category>Pex</category>
      <category>Testing</category>
    </item>
  </channel>
</rss>