<?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>Martijn's C# Programming Blog &#187; visual studio express C# 2008</title>
	<atom:link href="http://www.dijksterhuis.org/tag/visual-studio-express-c-2008/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dijksterhuis.org</link>
	<description>Information, news about programming in C#</description>
	<lastBuildDate>Fri, 07 Aug 2009 21:26:47 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Setting up NUnit for C# Unit Testing with Visual Studio C# Express 2008</title>
		<link>http://www.dijksterhuis.org/setting-up-nunit-for-c-unit-testing-with-visual-studio-c-express-2008/</link>
		<comments>http://www.dijksterhuis.org/setting-up-nunit-for-c-unit-testing-with-visual-studio-c-express-2008/#comments</comments>
		<pubDate>Thu, 27 Nov 2008 04:50:43 +0000</pubDate>
		<dc:creator>Martijn</dc:creator>
				<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[Learn C#]]></category>
		<category><![CDATA[nunit]]></category>
		<category><![CDATA[unit testing]]></category>
		<category><![CDATA[visual studio express C# 2008]]></category>

		<guid isPermaLink="false">http://www.dijksterhuis.org/?p=292</guid>
		<description><![CDATA[
Unit testing helps you verify that each individual part of your code is working as expected and keeps doing that as you change your software. You do this by adding small bits of testing code and have the unit testing frame work execute them in order.  I am currently writing some code that needs [...]<p>This is a post from <a href="http://www.dijksterhuis.org">Martijn's C# Coding Blog</a>. </p>
]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.dijksterhuis.org/wp-content/uploads/2008/11/unittesting.gif" alt="" title="Unit Testing with Visual Studio Express 2008" width="500" height="278" class="alignnone size-full wp-image-302" /></p>
<p><em>Unit testing helps you verify that each individual part of your code is working as expected and keeps doing that as you change your software. You do this by adding small bits of testing code and have the unit testing frame work execute them in order.  I am currently writing some code that needs to have a basic unit testing framework and wanted to install the NUnit framework. This post is about how you can install NUnit, use it and run it with / install it for Visual Studio C# 2008 Express. I will leave the nuts and bolts of unit testing to a future post but this post should get you up and running.<br />
</em></p>
<p><span id="more-292"></span></p>
<p>Installing NUnit itself is straightforward and only requires a few steps:</p>
<ul>
<li>Download the NUnit framework from the <a href="http://www.nunit.org/index.php?p=download">NUnit website</a> (download <a href="http://prdownloads.sourceforge.net/nunit/NUnit-2.4.8-net-2.0.msi?download">NUnit-2.4.8-net-2.0.msi</a>)
<li>Install the software on your Windows XP / Vista machine
</ul>
<p><strong>Creating your first NUnit test</strong></p>
<p>Now we need to create a new project to test if this works, and we need to add a reference to NUnit to be able to use the code.</p>
<p>Click &#8220;Project > Add Reference&#8230;&#8221;</p>
<p><img src="http://www.dijksterhuis.org/wp-content/uploads/2008/11/nunit1.jpg" alt="" title="Adding NUnit To Visual Studio Express 2008" width="400" height="291" class="alignnone size-full wp-image-293" /></p>
<p>Select &#8220;Browse&#8221; and navigate to &#8220;C:\Program Files\NUnit 2.4.8\bin&#8221; and select the <strong>nunit.framework.dll</strong>.</p>
<p><img src="http://www.dijksterhuis.org/wp-content/uploads/2008/11/nunit2.gif" alt="" title="Adding the NUnit Framework DLL to Visual Studio C# Express 2008" width="400" height="300" class="alignnone size-full wp-image-294" /></p>
<p>This allows you to use NUnit.Framework in your programs. </p>
<p><strong>Testing the NUnit Demo code</strong></p>
<p>Below I have included a little bit of demo code that tests if this works properly. The code is mostly borrowed from the NUnit website demo, I have just for the sake of it included a class containing &#8220;Main&#8221; so that the code actually compiles and runs.</p>
<pre class="brush: c#">
using System;
using NUnit.Framework;

namespace bank
{
    public class Account
    {
        private float balance;
        public void Deposit(float amount)
        {
            balance += amount;
        }

        public void Withdraw(float amount)
        {
            balance -= amount;
        }

        public void TransferFunds(Account destination, float amount)
        {
        }

        public float Balance
        {
            get { return balance; }
        }
    }
}

namespace bank
{
    [TestFixture]
    public class AccountTest
    {
        [Test]
        public void TransferFunds()
        {
            Account source = new Account();
            source.Deposit(200.00F);
            Account destination = new Account();
            destination.Deposit(150.00F);

            source.TransferFunds(destination, 100.00F);
            Assert.AreEqual(250.00F, destination.Balance);
            Assert.AreEqual(100.00F, source.Balance);

        }

        [Test]
        public void DepositFunds()
        {
            Account source = new Account();
            source.Deposit(200.00F);
            Assert.AreEqual(200.00F, source.Balance);
        }

    }
}

namespace UnitTestDemo
{
    public class MyAccountingSoftware
    {

        public static void Main()
        {
            bank.Account DemoAccount = new bank.Account();
            DemoAccount.Deposit(1000.00F);
            DemoAccount.Withdraw(500.50F);
            Console.WriteLine(&quot;Our account balance is {0}&quot;, DemoAccount.Balance);
        }

    }
}
</pre>
<p>In the above code the actual unit testing is done by the &#8220;AccountTest&#8221; class, for simplicity we keep this in the same file but usually you would keep a separate directory with all of your unit tests, separate from the actual code. </p>
<p>Note that because the &#8220;<strong>WithDraw</strong>&#8221; function in the code above is poorly implemented (eg. its empty) the Unit test will hopefully (!!) fail. The second unit test in <strong>DepositFunds</strong> should succeed.</p>
<p><strong>How to run the NUnit GUI runner from within Visual Studio C# Express 2008</strong></p>
<p>We can run NUnit from the Menu option it installed under [Start/Programs/NUnit] but it is more convenient to do so from Visual Studio Express itself. </p>
<p>Visual Studio C# Express allows you to define outside &#8220;Tools&#8221; which can be run from the menu. This is exactly what we would like to do with the NUnit GUI Runner. The GUI Runner is a tool which loads our program and executes all the unit tests it finds and reports the results in a seperate window.</p>
<p>Click on &#8220;Tools > External Tools&#8230;&#8221; to add NUnit&#8217;s GUI Runner.</p>
<p><img src="http://www.dijksterhuis.org/wp-content/uploads/2008/11/nunit3.gif" alt="" title="Adding NUnit to the VisuaL Studio Express Tools Menu" width="500" height="191" class="alignnone size-full wp-image-296" /></p>
<p>The following definition allows us to run the tests in our current project:</p>
<p><img src="http://www.dijksterhuis.org/wp-content/uploads/2008/11/nunit4.gif" alt="" title="Starting NUnit from the Visual Studio Tools Menu" width="460" height="486" class="alignnone size-full wp-image-299" /></p>
<p>To run the unit test, simply click &#8220;NUnit&#8221; from the Tools Menu.</p>
<p><a href="http://www.dijksterhuis.org/wp-content/uploads/2008/11/nunit5.gif"><img src="http://www.dijksterhuis.org/wp-content/uploads/2008/11/nunit5-300x243.gif" alt="" title="Running NUnit with our Visual Studio C# project" width="300" height="243" class="alignnone size-medium wp-image-300" /></a></p>
<p>[Click the picture for a larger image] </p>
<p>If everything went correctly you should see NUnit run 2 tests, one of which will fail. </p>
<p>This is a post from <a href="http://www.dijksterhuis.org">Martijn's C# Coding Blog</a>. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.dijksterhuis.org/setting-up-nunit-for-c-unit-testing-with-visual-studio-c-express-2008/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>
