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

<channel>
	<title>sides of march &#187; NUnit</title>
	<atom:link href="http://www.sidesofmarch.com/index.php/archive/tag/nunit/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.sidesofmarch.com</link>
	<description>Thoughts on life, liberty, and information technology</description>
	<lastBuildDate>Mon, 16 Jan 2012 02:43:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Unit testing an Activerecord domain model with NUnit</title>
		<link>http://www.sidesofmarch.com/index.php/archive/2009/01/14/unit-testing-an-activerecord-domain-model-with-nunit/</link>
		<comments>http://www.sidesofmarch.com/index.php/archive/2009/01/14/unit-testing-an-activerecord-domain-model-with-nunit/#comments</comments>
		<pubDate>Wed, 14 Jan 2009 14:11:38 +0000</pubDate>
		<dc:creator>brian</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[ActiveRecord]]></category>
		<category><![CDATA[NUnit]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://www.sidesofmarch.com/?p=329</guid>
		<description><![CDATA[<p><em>[Digression] This post has been sitting in a somewhat unfinished state for a long time. It feels good to finally post it!</em></p>
<p>In rewriting my baseball game, <a href="http://www.csfbl.com">CSFBL</a>, I made the decision to use <a href="http://www.castleproject.org/activerecord">Castle ActiveRecord</a> (built on top of <a href="http://www.nhforge.org">NHibernate</a>) to handle the persistence of my domain model. As a result, I needed to find a simple but effective way to unit test this implementation.</p>
<p>The unit tests needed to accomplish the following:</p>

Test the initialization of ActiveRecord (which validates the database schema against the object mappings).
Test object validation (since I am using <a href="http://api.castleproject.org/html/N_Castle_Components_Validator.htm">Castle’s Validation component</a> and the ActiveRecordValidationBase&#60;T&#62; base class).
Ensure that a domain object can be persisted to the database.
Ensure that a domain object can be retrieved from the database.
Ensure that multiple instances of an object with the same persisted primary key are equal.

<p>How, then, do we do this?</p>
<p><strong>Testing against a real database with test data</strong></p>
<p>One thing I knew I needed to do was to test against a <span style="color:#777"> . . .<br /><br />&#8594; Read More: <a href="http://www.sidesofmarch.com/index.php/archive/2009/01/14/unit-testing-an-activerecord-domain-model-with-nunit/">Unit testing an Activerecord domain model with NUnit</a></span>]]></description>
			<content:encoded><![CDATA[<p><em>[Digression] This post has been sitting in a somewhat unfinished state for a long time. It feels good to finally post it!</em></p>
<p>In rewriting my baseball game, <a href="http://redirectingat.com?id=17923X751173&xs=1&url=http%3A%2F%2Fwww.csfbl.com&sref=rss">CSFBL</a>, I made the decision to use <a href="http://redirectingat.com?id=17923X751173&xs=1&url=http%3A%2F%2Fwww.castleproject.org%2Factiverecord&sref=rss">Castle ActiveRecord</a> (built on top of <a href="http://redirectingat.com?id=17923X751173&xs=1&url=http%3A%2F%2Fwww.nhforge.org&sref=rss">NHibernate</a>) to handle the persistence of my domain model. As a result, I needed to find a simple but effective way to unit test this implementation.</p>
<p>The unit tests needed to accomplish the following:</p>
<ol>
<li>Test the initialization of ActiveRecord (which validates the database schema against the object mappings).</li>
<li>Test object validation (since I am using <a href="http://redirectingat.com?id=17923X751173&xs=1&url=http%3A%2F%2Fapi.castleproject.org%2Fhtml%2FN_Castle_Components_Validator.htm&sref=rss">Castle’s Validation component</a> and the <code>ActiveRecordValidationBase&lt;T&gt;</code> base class).</li>
<li>Ensure that a domain object can be persisted to the database.</li>
<li>Ensure that a domain object can be retrieved from the database.</li>
<li>Ensure that multiple instances of an object with the same persisted primary key are equal.</li>
</ol>
<p>How, then, do we do this?<span id="more-329"></span></p>
<p><strong>Testing against a real database with test data</strong></p>
<p>One thing I knew I needed to do was to test against a true database instance, not a <a title="Rhino Mocks" href="http://redirectingat.com?id=17923X751173&xs=1&url=http%3A%2F%2Fayende.com%2Fprojects%2Frhino-mocks.aspx&sref=rss">mock</a>. I wanted to make sure this was a live test of the persistence model, not just a test of the conceptual model. Since CSFBL will only run off an MSSQL database, and the schema is constantly changing, and the domain model has many foreign key dependencies, unit testing against the database is important.</p>
<p>To accomplish this, I wrote a short SQL script which would insert <strong>test data</strong> into a blank copy of the database. (All database scripts and build scripts are also in <a title="Subversion version control system" href="http://redirectingat.com?id=17923X751173&xs=1&url=http%3A%2F%2Fsubversion.tigris.org&sref=rss">SVN</a>; I’ll blog about that solution another time.) By inserting well-defined test data, I can then write unit tests easily.</p>
<p>A sample of the SQL test script follows.</p>
<pre class="brush: sql; ">

-- start bb_leagues
print &#039;bb_leagues&#039;

declare @leagueid int

insert into bb_leagues ( name, createdon, simfrequency,
startyear, date, forumid, ispremier, stadiumchangeallowed )
values ( &#039;testleague&#039;, getdate(), 2, 1981, &#039;1981-01-01&#039;, null, 0, 1 )

set @leagueid = scope_identity()
-- end bb_leagues

-- start bb_conferences
print &#039;bb_conferences&#039;

declare @conferenceid int

insert into bb_conferences ( name, allowdh, leagueid )
values ( &#039;testconference&#039;, 0, @leagueid )

set @conferenceid = scope_identity()
--end bb_conferences
</pre>
<p>Notice how I saved the primary key of the first insert (a <strong>league</strong>) and used it on the second insert (a <strong>conference</strong>). By cascading down the dependency chain of my database model, I can easily build my test data.</p>
<p><strong>The <code>ActiveRecordTest</code> base class</strong></p>
<p>Once the test data is available, it’s a matter of writing the unit tests. Since there will be one test class for each model class (e.g. <code>LeagueTests</code> for the <code>League</code> class, which maps to <code>bb_leagues</code>), we can have an abstract base class for our unit tests that will provide baseline functionality.</p>
<p>The base class will be responsible for a number of tasks, each corresponding to an NUnit task.</p>
<ol>
<li>[<code>TestFixtureSetUp</code>] Initialize the ActiveRecord framework when the test fixture starts.</li>
<li>[<code>SetUp</code>] Prepare the schema before each test (to ensure each test is somewhat atomic).</li>
<li>[<code>TearDown</code>] Clean up after each test is run.</li>
<li>[<code>TestFixtureTearDown</code>] Reset ActiveRecord after each text fixture ends to ensure other tests can don’t break when running step #1.</li>
</ol>
<p>By abstracting lots of the functionality, you only need to override a few methods:</p>
<ul>
<li><code>FetchARAssembly</code>() gets the assembly which contains your ActiveRecord model classes. This should be overridden to return the assembly name containing your model classes.</li>
<li><code>FetchARConfig</code>() gets the configuration source for ActiveRecord. The default implementation uses the ActiveRecord section in your application configuration file, which often is correct.</li>
<li>The other methods – <code>TestFixtureSetUp</code>, <code>SetUp</code>, <code>TearDown</code>, <code>TestFixtureTearDown</code> – only need to be overridden if you need additional functionality at those phases of your unit test. In all cases, you should always call the base class implementation first.</li>
</ul>
<p>On to the code!</p>
<pre class="brush: c-sharp; ">

using System;
using System.Reflection;
using Castle.ActiveRecord;
using Castle.ActiveRecord.Framework;
using Castle.ActiveRecord.Framework.Config;
using NUnit.Framework;

namespace ComputerSims.Common.Tests
{
///
/// Abstract class for unit tests that depend on ActiveRecord.
/// Ensures that ActiveRecord is initialized before the tests are run.
///
public abstract class ActiveRecordTest
{
#region SetUp/TearDown
///
/// Sets up the test fixture.
///
///
/// When overriding, be sure to call the base method.
///
[TestFixtureSetUp]
protected virtual void TestFixtureSetUp()
{
InitFramework();
}

///
/// Sets up before each test.
///
///
/// When overriding, be sure to call the base method.
///
[SetUp]
protected virtual void SetUp()
{
ClearSchema();
}

///
/// Tears down after each test.
///
///
/// When overriding, be sure to call the base method.
///
[TearDown]
protected virtual void TearDown()
{
}

///
/// Tears down the test fixture.
///
///
/// When overriding, be sure to call the base method.
///
[TestFixtureTearDown]
protected virtual void TestFixtureTearDown()
{
ActiveRecordStarter.ResetInitializationFlag();
}
#endregion

#region Protected Methods
///
/// Clears (deletes) all data in the data schema to allow each
/// test to run in isolation.
///
///
/// Currently this does nothing, as schema creation is handled by SQL scripts.
/// However, to run tests in isolation, we should recreate the database using
/// default data each time a unit test is run. Implementing that functionality
/// here would support this.
///
protected virtual void ClearSchema()
{
}

///
/// Gets the ActiveRecord configuration.
///
/// The ActiveRecord configuration settings.
///
/// Default implementation uses
/// .
///
protected virtual IConfigurationSource FetchARConfig()
{
IConfigurationSource source = ActiveRecordSectionHandler.Instance;
return source;
}

///
/// Gets the assembly that contains ActiveRecord entities.
///
/// The assembly.
///
/// Default implementation returns the executing assembly.
///
protected virtual Assembly FetchARAssembly()
{
return Assembly.GetExecutingAssembly();
}
#endregion

#region Private Methods
private void InitFramework()
{
ActiveRecordStarter.Initialize(
this.FetchARAssembly(),
this.FetchARConfig());
}
#endregion
}
}
</pre>
<p>Note that there’s nothing specific to my domain model in there. This base class can therefore be used for any ActiveRecord-based domain model.</p>
<p>Writing an implementation of this class is easy &#8212; and I&#8217;ll write about that soon.</p>
<img src="http://www.sidesofmarch.com/?ak_action=api_record_view&id=329&type=feed" alt="" /><div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.sidesofmarch.com/index.php/archive/2009/01/14/unit-testing-an-activerecord-domain-model-with-nunit/' addthis:title='Unit testing an Activerecord domain model with NUnit ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.sidesofmarch.com/index.php/archive/2009/01/14/unit-testing-an-activerecord-domain-model-with-nunit/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Building with NAnt (and NUnit, and NCover, and NCoverExplorer)</title>
		<link>http://www.sidesofmarch.com/index.php/archive/2008/08/18/building-with-nant-and-nunit-and-ncover-and-ncoverexplorer/</link>
		<comments>http://www.sidesofmarch.com/index.php/archive/2008/08/18/building-with-nant-and-nunit-and-ncover-and-ncoverexplorer/#comments</comments>
		<pubDate>Mon, 18 Aug 2008 21:17:08 +0000</pubDate>
		<dc:creator>brian</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[nant]]></category>
		<category><![CDATA[NCover]]></category>
		<category><![CDATA[NCoverExplorer]]></category>
		<category><![CDATA[NUnit]]></category>
		<category><![CDATA[TestDriven.net]]></category>

		<guid isPermaLink="false">http://www.sidesofmarch.com/?p=274</guid>
		<description><![CDATA[<p>A while back, I wrote a blog post, <a href="http://http://www.sidesofmarch.com/index.php/archive/2007/03/23/a-quick-introduction-to-nant/"><em>A quick introduction to Nant</em></a>, which gave, well, a quick introduction to building C# libraries using <a href="http://nant.sourceforge.net/">NAnt</a>.</p>
<p>Since then, I&#8217;ve been using NAnt to do a lot more &#8212; notably, to run unit tests and to report on test coverage (using <a href="http://codebetter.com/blogs/david.hayden/archive/2006/10/08/NCover-and-NCoverExplorer-_2D00_-Bad-News-Is-Empowering.aspx">NCover and NCoverExplorer</a>). The usage is pretty straightforward, and I think you&#8217;ll see how each step builds on the previous step.</p>

To run a default build (excluding unit tests): nant
To build all projects and unit tests (but don&#8217;t run : nant build-tests
To build all projects and unit tests, and run tests using NUnit: nant test
To build all projects and unit tests, and run tests using NCover to generate coverage reports: nant cover
To build all projects and unit tests, run tests using NCover to generate coverage reports, and open those reports in NCoverExplorer: nant coverex

<p>The sample below is my NAnt build file. A few notes first.</p>

There are only two projects referenced: Project1 <span style="color:#777"> . . .<br /><br />&#8594; Read More: <a href="http://www.sidesofmarch.com/index.php/archive/2008/08/18/building-with-nant-and-nunit-and-ncover-and-ncoverexplorer/">Building with NAnt (and NUnit, and NCover, and NCoverExplorer)</a></span>]]></description>
			<content:encoded><![CDATA[<p>A while back, I wrote a blog post, <a href="http://http://www.sidesofmarch.com/index.php/archive/2007/03/23/a-quick-introduction-to-nant/"><em>A quick introduction to Nant</em></a>, which gave, well, a quick introduction to building C# libraries using <a href="http://redirectingat.com?id=17923X751173&xs=1&url=http%3A%2F%2Fnant.sourceforge.net%2F&sref=rss">NAnt</a>.</p>
<p>Since then, I&#8217;ve been using NAnt to do a lot more &#8212; notably, to run unit tests and to report on test coverage (using <a href="http://redirectingat.com?id=17923X751173&xs=1&url=http%3A%2F%2Fcodebetter.com%2Fblogs%2Fdavid.hayden%2Farchive%2F2006%2F10%2F08%2FNCover-and-NCoverExplorer-_2D00_-Bad-News-Is-Empowering.aspx&sref=rss">NCover and NCoverExplorer</a>). The usage is pretty straightforward, and I think you&#8217;ll see how each step builds on the previous step.</p>
<ul>
<li>To run a default build (excluding unit tests): <code>nant</code></li>
<li>To build all projects and unit tests (but don&#8217;t run : <code>nant build-tests</code></li>
<li>To build all projects and unit tests, and run tests using NUnit: <code>nant test</code></li>
<li>To build all projects and unit tests, and run tests using NCover to generate coverage reports: <code>nant cover</code></li>
<li>To build all projects and unit tests, run tests using NCover to generate coverage reports, and open those reports in NCoverExplorer: <code>nant coverex</code></li>
</ul>
<p>The sample below is my NAnt build file. A few notes first.</p>
<ul>
<li>There are only two projects referenced: Project1 and Project1.Tests. Repeat the appropriate sections to build against additional projects.</li>
<li>nunit-console.exe is expected to be in the system path.</li>
<li>nunit-console.exe, ncover.console.exe, and ncoverexplorer.exe are expected to be in the system path. I recommend you download and install <a href="http://redirectingat.com?id=17923X751173&xs=1&url=http%3A%2F%2Fwww.testdriven.net%2F&sref=rss">TestDriven.Net</a> to have all of these in a convenient place.</li>
</ul>
<p>Now, on to the build file. It should be self-explanatory, but I added some XML comments for your convenience.</p>
<p><span id="more-274"></span></p>
<pre class="brush: xml; ">

&lt; ?xml version=&quot;1.0&quot;?&gt;
&lt;project name=&quot;project build&quot; default=&quot;build&quot; basedir=&quot;.&quot;&gt;
	&lt;description&gt;Project -- NAnt Build&lt;/description&gt;
	&lt;property name=&quot;debug&quot; value=&quot;true&quot; /&gt;
	&lt;property name=&quot;build.dir&quot; value=&quot;bin&quot; /&gt;

	&lt;echo message=&quot;debug: ${debug}&quot; /&gt;
	&lt;echo message=&quot;build.dir: ${build.dir}&quot; /&gt;

	&lt;target name=&quot;init&quot; description=&quot;sets up environment for build&quot;&gt;
		&lt;mkdir dir=&quot;${build.dir}&quot; /&gt;
	&lt;/target&gt;

	&lt;target name=&quot;clean&quot; description=&quot;removes old files, copies new reference DLLs&quot;&gt;
		&lt;delete dir=&quot;${build.dir}&quot; failonerror=&quot;false&quot; /&gt;
		&lt;delete&gt;
			&lt;fileset basedir=&quot;${build.dir}&quot;&gt;
			&lt;include name=&quot;*.*&quot; /&gt;
			&lt;/fileset&gt;
		&lt;/delete&gt;
		&lt;copy todir=&quot;${build.dir}&quot;&gt;
			&lt;!-- I keep shared DLLs in a directory called References --&gt;
			&lt;fileset basedir=&quot;References&quot;&gt;
				&lt;include name=&quot;*.dll&quot; /&gt;
				&lt;include name=&quot;*.xml&quot; /&gt;
			&lt;/fileset&gt;
		&lt;/copy&gt;
	&lt;/target&gt;

	&lt;target name=&quot;build&quot; description=&quot;build projects, excluding tests (default)&quot; depends=&quot;clean,init&quot;&gt;
		&lt;csc target=&quot;library&quot; output=&quot;${build.dir}\Project1.dll&quot; debug=&quot;${debug}&quot; doc=&quot;${build.dir}\Project1.xml&quot;&gt;
			&lt;sources basedir=&quot;Project1&quot;&gt;
				&lt;include name=&quot;**/*.cs&quot; /&gt;
			&lt;/sources&gt;
			&lt;references basedir=&quot;${build.dir}&quot;&gt;
				&lt;include name=&quot;System.dll&quot; /&gt;
				&lt;!-- add additional references here --&gt;
			&lt;/references&gt;
		&lt;/csc&gt;
	&lt;/target&gt;

	&lt;target name=&quot;build-tests&quot; description=&quot;build all test projects&quot; depends=&quot;build&quot;&gt;
		&lt;csc target=&quot;library&quot; output=&quot;${build.dir}\Project1.Tests.dll&quot; debug=&quot;true&quot;&gt;
		&lt;sources basedir=&quot;Project1.Tests&quot;&gt;
			&lt;include name=&quot;**/*.cs&quot; /&gt;
		&lt;/sources&gt;
		&lt;references basedir=&quot;${build.dir}&quot;&gt;
		&lt;include name=&quot;System.dll&quot; /&gt;
		&lt;include name=&quot;NUnit.Framework.dll&quot; /&gt;
		&lt;!-- add additional references here --&gt;
		&lt;/references&gt;
		&lt;/csc&gt;
		&lt;!-- this copies your App.config to a file that can be found by NUnit when testing --&gt;
		&lt;copy file=&quot;Project1.Tests\App.config&quot; tofile=&quot;${build.dir}\Project1.Tests.dll.config&quot; /&gt;
	&lt;/target&gt;

	&lt;target name=&quot;test&quot; description=&quot;builds all and runs unit tests&quot; depends=&quot;build-tests&quot;&gt;
		&lt;exec workingdir=&quot;${build.dir}&quot;
			program=&quot;nunit-console.exe&quot;&gt;
			&lt;arg value=&quot;Project1.Tests.dll&quot; /&gt;
			&lt;arg value=&quot;/xml=Project1.Tests-results.xml&quot; /&gt;
		&lt;/exec&gt;
	&lt;/target&gt;

	&lt;target name=&quot;cover&quot; description=&quot;builds all and runs unit tests using NCover&quot; depends=&quot;build-tests&quot;&gt;
		&lt;ncover program=&quot;NCover.Console.exe&quot;
			commandLineExe=&quot;nunit-console.exe&quot;
			workingDirectory=&quot;${build.dir}&quot;
			commandLineArgs=&quot;Project1.Tests.dll /xml=Project1.Tests-results.xml&quot;
			logFile=&quot;${build.dir}\Project1.Tests-coverage.log&quot;
			coverageFile=&quot;${build.dir}\Project1.Tests-coverage.xml&quot;
			assemblyList=&quot;Project1.dll&quot;
		/&gt;
	&lt;/target&gt;

	&lt;target name=&quot;coverex&quot; depends=&quot;cover&quot;&gt;
		&lt;exec workingdir=&quot;${build.dir}&quot;
			program=&quot;ncoverexplorer.exe&quot;&gt;
			&lt;arg value=&quot;Project1.Tests-coverage.xml&quot; /&gt;
		&lt;/exec&gt;
	&lt;/target&gt;

&lt;/project&gt;
</pre>
<img src="http://www.sidesofmarch.com/?ak_action=api_record_view&id=274&type=feed" alt="" /><div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.sidesofmarch.com/index.php/archive/2008/08/18/building-with-nant-and-nunit-and-ncover-and-ncoverexplorer/' addthis:title='Building with NAnt (and NUnit, and NCover, and NCoverExplorer) ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.sidesofmarch.com/index.php/archive/2008/08/18/building-with-nant-and-nunit-and-ncover-and-ncoverexplorer/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

