<?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>Nephandus &#187; web development</title>
	<atom:link href="http://www.nephandus.com/tag/web-development/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.nephandus.com</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Tue, 03 Jan 2012 16:33:48 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Unit Testing C# Webpages</title>
		<link>http://www.nephandus.com/2010/05/06/unit-testing-c-webpages/</link>
		<comments>http://www.nephandus.com/2010/05/06/unit-testing-c-webpages/#comments</comments>
		<pubDate>Thu, 06 May 2010 14:04:57 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[software development]]></category>
		<category><![CDATA[tdd]]></category>
		<category><![CDATA[test driven development]]></category>
		<category><![CDATA[watir]]></category>
		<category><![CDATA[web development]]></category>

		<guid isPermaLink="false">http://www.nephandus.com/?p=21</guid>
		<description><![CDATA[Unit testing is good; test driven development is better.  As Knuth once famously quipped &#8220;Beware of the above code. I have only proven it correct, not tested it.&#8221; There really is no substitution for good, solid testing. Unfortunately, at least in C#, webpages don&#8217;t like to be unit tested.  I approach this post with an [...]]]></description>
			<content:encoded><![CDATA[<div id="attachment_22" class="wp-caption alignleft" style="width: 223px"><a href="http://www.flickr.com/photos/beatkueng/2680294816/"><img class="size-medium wp-image-22" title="2680294816_e710a43d3b" src="http://www.nephandus.com/wp-content/uploads/2010/05/2680294816_e710a43d3b-213x300.jpg" alt="" width="213" height="300" /></a><p class="wp-caption-text">Image Credit: PixelPlacebo via Flickr and Creative Commons</p></div>
<p>Unit testing is good; test driven development is better.  As Knuth once famously <a href="http://www-cs-faculty.stanford.edu/~knuth/faq">quipped</a> &#8220;<em>Beware of the above code. I have only proven it correct, not tested it.&#8221;</em> There really is no substitution for good, solid testing.</p>
<p>Unfortunately, at least in C#, webpages don&#8217;t like to be unit tested.  I approach this post with an uncomfortable realization that I am about to lay out the issues and problems I&#8217;ve had while the solution I am presently using is far from satisfactory.<span id="more-21"></span></p>
<p>There is a right way and a wrong way to undertake a lot of unit testing.  If you want to learn about that, I recommend <a href="http://www.ryanhagan.net/2009/10/test-driven-development/">this post by Ryan Hagan</a> as a great jumping off point.  My problem is not in writing good unit tests, however, so much as having a place to write them.</p>
<p>Axiomatic truth of Unit Testing: Your test code should exist in a different place than your production code.  This just makes good sense.  You don&#8217;t want to publish your tests every time you push code out the door and, since you want all of your published code to be tested you don&#8217;t want to have to write tests for your tests.  In C# the way one generally accomplishes this is the creation of a separate test project within the current solution.  That way the solution contains both your code and your tests.</p>
<p>Now generally, when you&#8217;re writing tests for C# this isn&#8217;t a big deal.  Since your project gets bundled up every time you build you can add a reference to the Test project that points at your main project; that way, the test harness knows what to make of all the custom types and whatnot that your various tests will have to evaluate.</p>
<p>This does not work for webpages.</p>
<p>When C# builds a webpage there&#8217;s no handy-dandy compiled thing at which to point your test code.  That means your test code has no idea what to make of any custom objects you&#8217;ve created and that makes any meaningful testing pretty much impossible.  Consider the following short example:</p>
<pre class="brush: csharp; title: ;">
public void CreateTest()
        {
            string WidgetName = string.Empty;
            Widget expected = null;
            Widget actual;
            actual = WidgetFactory_Accessor.Create(WidgetName);
            Assert.AreNotEqual(expected, actual);
       }
</pre>
<p>In this example, &#8220;Widget&#8221; and &#8220;WidgetFactory&#8221; are both custom types.  C# has conveniently generated an accessor for my Factory but it has no reference to my main project and thus doesn&#8217;t know what a Widget is.  Consequently, this test won&#8217;t compile and I can&#8217;t run it.</p>
<p>There is a work-around solution I&#8217;ve found wherein I publish the Main Project and then create a reference from the Test Project to the published AppCode.dll file.  This works but requires that I publish the DLL every time I wish to re-evaluate my source code which makes the entire testing process prohibitively time consuming.</p>
<p>My suspicion at this point is that what I am trying to do is in fact neither possible nor recommended and that unit testing of a C# webpage should be accomplished by creating a sharp distinction between the interface and presentation layers, testing the presentation layer traditionally and then using interface testing suites like <a href="http://watir.com/">Watir</a>.  This seems the sort of thing that should be verbosely stated somewhere if it is, indeed, the case.</p>
<h2>Update</h2>
<p>As per Ryan&#8217;s suggestion below and some other feedback I&#8217;ve gotten I am moving much of the logic code out of the project and creating a project-level distinction between the interface and the presentation layers of the application.  This should allow me to more sanely test the presentation layer and leverage Watin in to handle the interface testing.</p>
<p>It&#8217;s not the sort of separation recommended, but the architecture on this particular system is fairly dated and a full out re-factoring is out of the question (for now).</p>
<p>As it&#8217;s conspicuously missing from many other write-ups I&#8217;ve seen I&#8217;ll say it here.  You can not meaningfully test anything except the interface of a C# web project.  If you want to test your code, move it into a DLL project and allow your web project to call that DLL.  Ideally the Web project should be as thin as possible.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.nephandus.com/2010/05/06/unit-testing-c-webpages/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

