<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0"><channel><title>Unit Testing @ timstall.dotnetdevelopersjournal.com</title><link>http://timstall.dotnetdevelopersjournal.com/</link><description>(Unit Testing) </description><copyright>Copyright 2008 timstall.dotnetdevelopersjournal.com</copyright><generator></generator><lastBuildDate>Sat, 06 Sep 2008 21:21:00 GMT</lastBuildDate><image><title>Unit Testing @ timstall.dotnetdevelopersjournal.com</title><url>http://res.sys-con.com/portlet/163/featured-blog-graphic-145.gif</url><link>http://timstall.dotnetdevelopersjournal.com/</link></image><ttl>360</ttl><docs>http://backend.userland.com/rss</docs><item><title>MSBuild error MSB3073 &quot;The batch file cannot be found&quot;</title><guid isPermaLink="true">http://timstall.dotnetdevelopersjournal.com/msbuild_error_msb3073_the_batch_file_cannot_be_found.htm</guid><link>http://timstall.dotnetdevelopersjournal.com/msbuild_error_msb3073_the_batch_file_cannot_be_found.htm</link><pubDate>Thu, 02 Feb 2006 23:18:00 GMT</pubDate><comments>http://timstall.dotnetdevelopersjournal.com/console/comments/popup/?f=msbuild%5Ferror%5Fmsb3073%5Fthe%5Fbatch%5Ffile%5Fcannot%5Fbe%5Ffound</comments><dc:creator>Tim Stall</dc:creator><description><![CDATA[<p>We've migrated to MSTest, and are running tests through MSBuild. I came across an interesting issue (that seems like a bug).</p>
<p>Normally I would expect that if the test passes in Test Viewer (the UI), then it would also pass when I run it in MSTest from the command line, and it would still pass when I call it from MSBuild. However, I found this not always so.</p>
<p>One of my tests wrote out files to the temp directory, as generated by &quot;System.IO.Path.GetTempPath()&quot;. I then cleaned up the test output by deleting the directory, and recreating it. This worked fine in both UI, and MSTest, but failed in MSBuild with the following error:</p>
<p><font face="Courier New" size="2">Run Configuration: Default Run Configuration<br /><strong>The batch file cannot be found.</strong><br />C:\myProject\myBuild.msbuild(21,5): <strong>error MSB3073</strong>: </font></p>
<p><font face="Courier New" size="2">The command &quot;&quot;C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\MsTest&quot;<br />/testcontainer:C:\myTests.dll /resultsfile:myOutput\UnitTests.trx&quot; exited with code 1.<br />Done building target &quot;UnitTest&quot; in project &quot;myBuild.msbuild&quot; -- FAILED.</font></p>
<p>By appending a sub directory to the temp directory, like &quot;System.IO.Path.GetTempPath()&quot; + &quot;temp\&quot;, the tests all passed again. My guess is that MSBuild sent something to this output directory that my cleanup method was inadvertently deleting.</p>]]></description></item><item><title>Tips to Efficiently create database unit tests</title><guid isPermaLink="true">http://timstall.dotnetdevelopersjournal.com/tips_to_efficiently_create_database_unit_tests.htm</guid><link>http://timstall.dotnetdevelopersjournal.com/tips_to_efficiently_create_database_unit_tests.htm</link><pubDate>Tue, 26 Jul 2005 20:45:00 GMT</pubDate><comments>http://timstall.dotnetdevelopersjournal.com/console/comments/popup/?f=tips%5Fto%5Fefficiently%5Fcreate%5Fdatabase%5Funit%5Ftests</comments><dc:creator>Tim Stall</dc:creator><description><![CDATA[<p>A while ago I published an article and blogged on how to <a href="/unit_testing_the_data_access_layer.htm">Unit Test the Data Layer</a>. Recall that the basic approach is:</p><ol><li>Create a test-only instance of the database</li><li>Initialize the table data to a specific state</li><li>Run your object</li><li>Check either the return value of that object (for selects) or the new state of the database (for insert/update/delete).</li></ol><p>A common problem I keep facing is that these tests can take long to create, and longer to modify. Below are a few tips that I've been learning.</p><p>Creating the initialization script is usually tedious and time-consuming. One approach is to create or find a tool that takes a select statement and generates inserts from its result set (I built such a tool, but I can't release it because it's proprietary to my company, <a href="http://www.ameripay.com/">Ameripay</a>). This lets you easily copy existing data for testing purposes. </p><p>I modified this tool to <i>put tabs after each column</i>, and place the insert and values sections on separate lines.</p><p><table id="AutoNumber3" style="BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-COLLAPSE: collapse; BORDER-RIGHT-WIDTH: 0px" bordercolor="#111111" cellpadding="2" width="100%" border="1"><tbody><tr><td style="BORDER-RIGHT: medium none; BORDER-TOP: medium none; BORDER-BOTTOM: medium none" width="100%"><blockquote><p><font face="Courier New" size="2">insert into myTable ( myId,    col1,     col2)<br />values ( '1',     'abc',     'def')</font></p></blockquote></td></tr></tbody></table></p><p>This lets me easily copy the text into Excel, which then automatically formats it in a tabular way that lines up column headers with contents, like so:</p><p><table id="AutoNumber8" style="BORDER-COLLAPSE: collapse" bordercolor="#c0c0c0" cellpadding="2" border="1"><tbody><tr><td><font face="Courier New" size="2">insert into myTable(</font></td><td><font face="Courier New" size="2">myId,</font></td><td><font face="Courier New" size="2">col1,</font></td><td><font face="Courier New" size="2">col2</font></td><td><font face="Courier New" size="2">)</font></td></tr><tr><td><font face="Courier New" size="2">values(</font></td><td><font face="Courier New" size="2">'1',</font></td><td><font face="Courier New" size="2">'abc',</font></td><td><font face="Courier New" size="2">'def'</font></td><td><font face="Courier New" size="2">)</font></td></tr></tbody></table></p><p>It's much easier to edit this way!</p><p>Therefore to maintain the data, I only need the raw SQL script itself - not some fancy generator/mapping tool or spreadsheet. I can now take the raw text and swap it between:</p><ul><li>Query Analyzer -  to easily test it</li><li>Visual Studio - in a source file as the contents of an ExecuteNonQuery(@&quot;...&quot;) method that runs it to initialize the test</li><li>Excel - to put it in tabular form and maintain it.</li></ul><p>Two applications of this:</p><ol><li>I created a MasterInserts Excel sheet that contains standard inserts for each table. Therefore if I have 5 objects all requiring test data from the same table, I go to this master sheet as the definitive source, copy the lines I need, and modify them appropriately. This prevents me from wasting time re-creating SQL inserts for tables.</li><li>Say I have multiple tests for a single object. 90% of the base data for each test is the same - usually it's just tweaking a value like making sure a SP handles a null column. Therefore I refactor the common base script to a single method that runs it in ExecuteNonQuery. Then each test can call this method and merely do the simple update or additional insert that it needs.</li></ol><p>The sky is always the limit, but it's getting there.</p>]]></description></item><item><title>Unit Testing Void Methods</title><guid isPermaLink="true">http://timstall.dotnetdevelopersjournal.com/unit_testing_void_methods.htm</guid><link>http://timstall.dotnetdevelopersjournal.com/unit_testing_void_methods.htm</link><pubDate>Sun, 08 May 2005 23:59:00 GMT</pubDate><comments>http://timstall.dotnetdevelopersjournal.com/console/comments/popup/?f=unit%5Ftesting%5Fvoid%5Fmethods</comments><dc:creator>Tim Stall</dc:creator><description><![CDATA[<p>I was recently asked the good question &quot;Should you create tests for void methods?&quot;. My answer: It depends. Let's look at the following example:</p><table id="AutoNumber3" style="BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-COLLAPSE: collapse; BORDER-RIGHT-WIDTH: 0px" bordercolor="#111111" cellpadding="2" width="100%" border="1"><tbody><tr><td style="BORDER-RIGHT: medium none; BORDER-TOP: medium none; BORDER-BOTTOM: medium none" width="100%"><blockquote><p><font face="Courier New" size="2"><span id="output"><span class="kwrd"><font color="#0000ff">public</font></span> <span class="kwrd"><font color="#0000ff">class</font></span> Foo1 <br />{<br />    <span class="kwrd"><font color="#0000ff">public</font></span> Foo1() <br />    {<br />        _intCounter = 0;<br />    }<br /><br />    <span class="kwrd"><font color="#0000ff">private</font></span> <span class="kwrd"><font color="#0000ff">int</font></span> _intCounter;<br />    <span class="kwrd"><font color="#0000ff">public</font></span> <span class="kwrd"><font color="#0000ff">int</font></span> Counter <br />    {<br />        get <br />        {<br />            <span class="kwrd"><font color="#0000ff">return</font></span> _intCounter;<br />        }<br />    }<br /><br />    <span class="kwrd"><font color="#0000ff">public</font></span> <span class="kwrd"><font color="#0000ff">void</font></span> Increment() <br />    {<br />        _intCounter++;<br />    }<br /><br />}</span></font></p></blockquote></td></tr></tbody></table><p>In this case, the void method Increment() changes the state of the Foo1 object, which can be measured by the Counter property. So, the following would be a decent test:</p><table id="AutoNumber3" style="BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-COLLAPSE: collapse; BORDER-RIGHT-WIDTH: 0px" bordercolor="#111111" cellpadding="2" width="100%" border="1"><tbody><tr><td style="BORDER-RIGHT: medium none; BORDER-TOP: medium none; BORDER-BOTTOM: medium none" width="100%"><blockquote><p><span id="output"><font face="Courier New" size="2">[Test] <span class="kwrd"><font color="#0000ff">public</font></span> <span class="kwrd"><font color="#0000ff">void</font></span> TestVoidMethod() <br />{<br />    Foo1 f = <span class="kwrd"><font color="#0000ff">new</font></span> Foo1();<br />    Assert.AreEqual(0,f.Counter);<br />    f.Increment();<br />    Assert.AreEqual(1,f.Counter);<br />}</font></span></p></blockquote></td></tr></tbody></table><p>The intent is that we want to always test deterministic logic, such as code that contains conditions, loops, or operators. While this is straight-forward with static functions (plug in x, get y), it can still apply to void methods. The difference is that a void method may be updating another property of an instantiated object instead of a return value.</p><p>However note that void methods may lack any logic to test. For example, the method below simply wraps some architecture block that writes out files. There's no logic here to test.</p><table id="AutoNumber3" style="BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-COLLAPSE: collapse; BORDER-RIGHT-WIDTH: 0px" bordercolor="#111111" cellpadding="2" width="100%" border="1"><tbody><tr><td style="BORDER-RIGHT: medium none; BORDER-TOP: medium none; BORDER-BOTTOM: medium none" width="100%"><blockquote><p><font face="Courier New"><span id="output"><span class="kwrd"><font color="#0000ff" size="2">public</font></span><font size="2"> <span class="kwrd"><font color="#0000ff">void</font></span> WriteFile(<span class="kwrd"><font color="#0000ff">string</font></span> strContent) <br />{<br />    ArchitectureBlock.WriteToFile(strContent);<br />}</font></span></font></p></blockquote></td></tr></tbody></table>]]></description></item><item><title>Unit Testing the Data Access Layer</title><guid isPermaLink="true">http://timstall.dotnetdevelopersjournal.com/unit_testing_the_data_access_layer.htm</guid><link>http://timstall.dotnetdevelopersjournal.com/unit_testing_the_data_access_layer.htm</link><pubDate>Tue, 05 Apr 2005 23:59:00 GMT</pubDate><comments>http://timstall.dotnetdevelopersjournal.com/console/comments/popup/?f=unit%5Ftesting%5Fthe%5Fdata%5Faccess%5Flayer</comments><dc:creator>Tim Stall</dc:creator><description><![CDATA[<font size="3"><font face="Times New Roman"><p>I had the opportunity to write an article for 4guysfromrolla - &quot;Unit Testing the Data Access Layer&quot;.</p><p><i>&quot;Unit testing is becoming very popular among .NET projects, but unit testing the Data Access Layer (DAL) still remains a common obstacle. The problem stems from the very nature of databases: they are slow, common, persistent, external, and relational. This conflicts the nature of unit tests, which should be quick, independent, and repeatable...&quot;</i></p><p>You can read the entire article on the 4GFR website at:</p><p><a href="http://aspnet.4guysfromrolla.com/articles/040605-1.aspx">http://aspnet.4guysfromrolla.com/articles/040605-1.aspx</a></p></font></font><font face="Times New Roman" size="3"></font>]]></description></item><item><title>How to Test Private and Protected methods</title><guid isPermaLink="true">http://timstall.dotnetdevelopersjournal.com/how_to_test_private_and_protected_methods.htm</guid><link>http://timstall.dotnetdevelopersjournal.com/how_to_test_private_and_protected_methods.htm</link><pubDate>Tue, 01 Mar 2005 23:59:00 GMT</pubDate><comments>http://timstall.dotnetdevelopersjournal.com/console/comments/popup/?f=how%5Fto%5Ftest%5Fprivate%5Fand%5Fprotected%5Fmethods</comments><dc:creator>Tim Stall</dc:creator><description><![CDATA[<p>I published an article on CodeProject <a href="http://www.codeproject.com/csharp/TestNonPublicMembers.asp">How to Test Private and Protected methods</a>. </p><p>This article explains some theory behind testing/not testing private methods, and then provides and walks through a downloadable code sample to demonstrate these testing techniques. </p>]]></description></item><item><title>Advanced Techniques with NUnitAsp</title><guid isPermaLink="true">http://timstall.dotnetdevelopersjournal.com/advanced_techniques_with_nunitasp.htm</guid><link>http://timstall.dotnetdevelopersjournal.com/advanced_techniques_with_nunitasp.htm</link><pubDate>Thu, 27 Jan 2005 13:51:00 GMT</pubDate><comments>http://timstall.dotnetdevelopersjournal.com/console/comments/popup/?f=advanced%5Ftechniques%5Fwith%5Fnunitasp</comments><dc:creator>Tim Stall</dc:creator><description><![CDATA[<p>I just had an article published on 4guysfromrolla, a popular ASP.Net website. It's about using NUnitAsp to test WebForms.</p><p><a href="http://aspnet.4guysfromrolla.com/articles/012605-1.aspx">http://aspnet.4guysfromrolla.com/articles/012605-1.aspx</a></p><p>I think this is a pretty good tool, especially because it's open source and has a familiar API.</p>]]></description></item><item><title>Good books on Unit Testing</title><guid isPermaLink="true">http://timstall.dotnetdevelopersjournal.com/good_books_on_unit_testing.htm</guid><link>http://timstall.dotnetdevelopersjournal.com/good_books_on_unit_testing.htm</link><pubDate>Sat, 22 Jan 2005 17:53:00 GMT</pubDate><comments>http://timstall.dotnetdevelopersjournal.com/console/comments/popup/?f=good%5Fbooks%5Fon%5Funit%5Ftesting</comments><dc:creator>Tim Stall</dc:creator><description><![CDATA[<p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><font face="Times New Roman" size="3">I’ve just finished skimming two great books: Pragmatic Unit Testing in C# with NUnit (Andrew Hunt and David Thomas), and Test Driven Development in Microsoft .NET by James W. Newkirk and Alexei.</font></p><p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><font face="Times New Roman" size="3">These are practical guides that show how to use Unit Testing to ensure that the code actually worked in the first place and fortify your code against regression bugs. Both books refer to NUnit (</font><a href="http://www.nunit.org/"><font face="Times New Roman" size="3">http://www.nunit.org/</font></a><font face="Times New Roman" size="3">), a Unit Testing tool for .Net based on JUnit. I’ll write the next couple blog entries about useful tips I found from these books.</font></p><p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><font face="Times New Roman" size="3"></font> </p><p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><font face="Times New Roman" size="3"><strong>Where to put your Tests:</strong> put them in a separate assembly so that they don’t ship with production code. Although if you put them in the same assembly, you can test for protected members, there is a work around: make a test class that inherits the production class, make the new class have public accessors. I use to use the #if DEBUG … #endif, but I like this approach a lot more.</font></p><p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><font face="Times New Roman" size="3"></font> </p><p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="COLOR: black"><font size="3"><font face="Times New Roman"><strong>Write the tests first:</strong> I use to develop first, and then write tests to cover my back. Test Driven Development is about writing the tests first, they fail (the implementing code hasn’t been written yet), and then write the code to make the tests pass. This makes you program from a user’s perspective (you need to use your own methods to write them in the tests), and gives you a very objective standard to adhere to. You don’t burden yourself with everything that might happen down the road – just make the initial tests pass. Because you should have sufficient tests to cover all the functionality of your program, you can confidently refactor and add features later without fear of breaking things.</font></font></span></p><p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><span style="COLOR: black"><font face="Times New Roman" size="3"></font></span> </p><p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><font face="Times New Roman" size="3"><strong>Useful checklists (from Pragmatic Unit Testing in C#):</strong> </font></p><p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><font face="Times New Roman" size="3"><u>What to test, use your Right-BICEP:</u></font></p><p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><font size="3"><font face="Times New Roman"><span style="mso-spacerun: yes">  </span>Right</font></font></p><p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><font size="3"><font face="Times New Roman"><span style="mso-spacerun: yes">  </span>Boundary conditions</font></font></p><p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><font size="3"><font face="Times New Roman"><span style="mso-spacerun: yes">  </span>Inverse relationships</font></font></p><p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><font size="3"><font face="Times New Roman"><span style="mso-spacerun: yes">  </span>Cross-Check results with other means</font></font></p><p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><font size="3"><font face="Times New Roman"><span style="mso-spacerun: yes">  </span>Error Conditions occur</font></font></p><p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><font size="3"><font face="Times New Roman"><span style="mso-spacerun: yes">  </span>Performance within bounds</font></font></p><p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><font face="Times New Roman" size="3"><u>Good Tests are A TRIP:</u></font></p><p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><font size="3"><font face="Times New Roman"><span style="mso-spacerun: yes">  </span>Automatic</font></font></p><p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><font size="3"><font face="Times New Roman"><span style="mso-spacerun: yes">  </span>Thorough</font></font></p><p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><font size="3"><font face="Times New Roman"><span style="mso-spacerun: yes">  </span>Repeatable</font></font></p><p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><font size="3"><font face="Times New Roman"><span style="mso-spacerun: yes">  </span>Independent</font></font></p><p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><font size="3"><font face="Times New Roman"><span style="mso-spacerun: yes">  </span>Professional</font></font></p><p class="MsoNormal" style="MARGIN: 0in 0in 0pt" /><p><font face="Times New Roman" size="3"> </font></p><p /><p class="MsoNormal" style="MARGIN: 0in 0in 0pt"><font size="3"><font face="Times New Roman">More later</font></font></p>]]></description></item></channel></rss>