<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" 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:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>TDD</title>
        <link>http://geekswithblogs.net/marcel/category/3858.aspx</link>
        <description>Test Driven Development</description>
        <language>en-US</language>
        <copyright>Marcin Celej</copyright>
        <managingEditor>marcin.celej@gmail.com</managingEditor>
        <generator>Subtext Version 0.0.0.0</generator>
        <item>
            <title>Design by Contract in .NET</title>
            <link>http://geekswithblogs.net/marcel/archive/2006/02/15/69633.aspx</link>
            <description>&lt;P __designer:dtid="281474976710656"&gt;Design by Contract is a methodology that was populated by &lt;A href="http://www.eiffel.com/" __designer:dtid="281474976710657"&gt;Eiffel&lt;/A&gt; language. The DbC ideas have common aims with Test Driven Development (TDD). Both focus on software quality and tests written with code in parallel.&lt;/P&gt;
&lt;P __designer:dtid="281474976710658"&gt;The idea of DbC is based on contract - contract between client code and service. Each class is a service that provides set of methods (nothing new). Each method call is a client. Contract between them says that if the client satisfies some preconditions then service will ensure some postconditions. That's all. The contracts (or assertions) should be declared easily with the method as there may be lots of them. C# does not support such assertions easily. Some people do it in this way:&lt;/P&gt;
&lt;P __designer:dtid="281474976710659"&gt;&lt;PRE lang=cs __designer:dtid="281474976710660"&gt;public decimal AccountBalance
{
    [&lt;STRONG __designer:dtid="281474976710661"&gt;Ensures&lt;/STRONG&gt;(&lt;SPAN style="COLOR: maroon" __designer:dtid="281474976710662"&gt;"result &amp;gt; 0"&lt;/SPAN&gt;)]
    get;
    [&lt;STRONG __designer:dtid="281474976710663"&gt;Requires&lt;/STRONG&gt;(&lt;SPAN style="COLOR: maroon" __designer:dtid="281474976710664"&gt;"value &amp;gt; 0", "Balance must be greater than zero"&lt;/SPAN&gt;)]
    [&lt;STRONG __designer:dtid="281474976710665"&gt;Ensures&lt;/STRONG&gt;(&lt;SPAN style="COLOR: maroon" __designer:dtid="281474976710666"&gt;"AccountBalance == value"&lt;/SPAN&gt;)]
    set;
}&lt;/PRE&gt;
&lt;P&gt;&lt;/P&gt;The Requires attribute represents precondition assertion and the Ensures attribute checks postconditions. The notation is not perfect here but it shows how the assertions (tests) are embedded within code. Let's ignore some performance implications of the methodology and think how it simplifies test-centric development. 
&lt;P __designer:dtid="281474976710667"&gt;Unfortunatelly there is no good implementation of the methodology in .NET yet. There is a research project in Microsoft that tries to introduce such language (&lt;A href="http://research.microsoft.com/SpecSharp/" __designer:dtid="281474976710668"&gt;Spec#&lt;/A&gt;) to the .NET platform. Sample assertions in the Spec# looks like this:&lt;/P&gt;
&lt;P __designer:dtid="281474976710669"&gt;&lt;PRE lang=cs __designer:dtid="281474976710670"&gt;public class ArrayList
{
    public void Insert(int index, object value)
        &lt;STRONG __designer:dtid="281474976710671"&gt;requires&lt;/STRONG&gt; 0 &amp;lt;= index &amp;amp;&amp;amp; index &amp;lt; Count
        &lt;STRONG __designer:dtid="281474976710672"&gt;requires&lt;/STRONG&gt; !IsReadOnly &lt;STRONG __designer:dtid="281474976710673"&gt;otherwise&lt;/STRONG&gt; NotSupportedException
    {
        ...
    }
}&lt;/PRE&gt;
&lt;P&gt;&lt;/P&gt;As you can see the difference is in syntax only, not in idea of the methodology. So Microsoft goes this way. 
&lt;P __designer:dtid="281474976710674"&gt;I believe the DbC is a powerful mechanism that can save you tears (as many other test-centric mechanisms). I hope the Spec# will become commonly used C# extension (I only played with it yet). Finally: everyone intrested in TDD methodologies should carrefully overview the DbC techniques.&lt;/P&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=69633"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=69633" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/marcel/aggbug/69633.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Marcin Celej</dc:creator>
            <guid>http://geekswithblogs.net/marcel/archive/2006/02/15/69633.aspx</guid>
            <pubDate>Thu, 16 Feb 2006 06:17:00 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/marcel/comments/69633.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/marcel/archive/2006/02/15/69633.aspx#feedback</comments>
            <slash:comments>2</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/marcel/comments/commentRss/69633.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/marcel/services/trackbacks/69633.aspx</trackback:ping>
        </item>
        <item>
            <title>Some principles of Unit Test creation</title>
            <link>http://geekswithblogs.net/marcel/archive/2006/02/02/67988.aspx</link>
            <description>&lt;P&gt;I am big fun of Test Driven Development (TDD). I recently read quite good article about unit testing: &lt;A href="http://msdn.microsoft.com/msdnmag/issues/06/01/UnitTesting/default.aspx"&gt;Write Maintainable Unit Tests That Will Save You Time And Tears&lt;/A&gt;. Here is an extract of the article:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Fail First (before you write piece of logic, write fail test first) - do not write any test, but simulate production environment that will use the logic so the test will remain as regression test of your logic. &lt;/LI&gt;
&lt;LI&gt;Requirements conflict (old UT fails when new funct. is introduced) - remove or enhance old UT. &lt;/LI&gt;
&lt;LI&gt;If you can remove a line in your tested class and all tests still pass - you have not enough unit tests. &lt;/LI&gt;
&lt;LI&gt;Remeber that tests must be maintained as any other code - so 'Keep It Simple, Stupid' (KISS) &lt;/LI&gt;
&lt;LI&gt;Make tests easy to run (or at least some subset of tests) so developers can run it often. &lt;/LI&gt;
&lt;LI&gt;Do not let peaple to stop trust the tests - the less false-positives in failed tests the better. &lt;/LI&gt;
&lt;LI&gt;Test only public interface of component.&lt;/LI&gt;
&lt;LI&gt;Reuse your creation, manipulation, and assertion code when possible. &lt;/LI&gt;
&lt;LI&gt;Don't create instances of classes directly inside a unit test - use factory even more than normally.&lt;/LI&gt;
&lt;LI&gt;Refactor unit tests - mainly to KISS them .&lt;/LI&gt;
&lt;LI&gt;&lt;SPAN class=clsGloss&gt;Avoid dependencies between tests - any test should work run in any order .&lt;/SPAN&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;SPAN class=clsGloss&gt;Create readable tests - write comments in Asserts and in UT. Write descriptive method names (even very long). Use local variables in Asserts. Use constants. UT should be readable like a book.&lt;/SPAN&gt;&lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;&lt;SPAN class=clsGloss&gt;Here is an extract of one of many UT articles. I would like to create some check list to be able to do code review of unit tests based on the list and on the principles contained in it.&lt;/P&gt;&lt;/SPAN&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=67988"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=67988" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/marcel/aggbug/67988.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Marcin Celej</dc:creator>
            <guid>http://geekswithblogs.net/marcel/archive/2006/02/02/67988.aspx</guid>
            <pubDate>Fri, 03 Feb 2006 06:58:00 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/marcel/comments/67988.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/marcel/archive/2006/02/02/67988.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/marcel/comments/commentRss/67988.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/marcel/services/trackbacks/67988.aspx</trackback:ping>
        </item>
    </channel>
</rss>