Tutorial NUnit

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

NUnit Framework and UnitRun Tutorial

Spirit Du
th
March 20 2007 Ver. 1.1.0

Contents
About This Document .................................................................................................... 1
Tutorial – Test Account ................................................................................................... 2
Appendix A – NUnit Installation ..................................................................................... 6
Appendix B – ReSharper UnitRun Installation ............................................................... 6

About This Document


This document would guide you how to test a class: Account which records the
amount of someone’s money in a bank. If you don’t have the environment yet, in
your home PC for example, you can refer the appendix and setup the environment
before running the tutorial. In the tutorial, an account can do three jobs:
 Withdraw - to remove money from a place of deposit
 Deposit - to put money in the account
 Balance – accumulate the money still in the account

1
Tutorial – Test Account
Step 1. Create a new C# Console Application project. Then, add a class: Account.

Figure 1 Add a class into project

Step 2. Change its visibility to public and add following implementation.

public class Account {


protected double _amount;
public Account(double initAmount) {
_amount = initAmount;
}
public void deposit(double amount) {
_amount += amount;
}
public void withdraw(double amount) {
if(amount > _amount)
Console.WriteLine("Sir, you don't have so much money!");
_amount -= amount;
}
public double balance() {
return _amount;
}
}

2
Step 3. Add NUnit framework reference into project.

Figure 2 Add NUnit reference into project

Step 4. Add a new class: AccountTests and also change its visibility to public and key
in the following codes1, red ones, into AccountTests class.

using NUnit.Framework;
[TestFixture]
public class AccountTests {
[SetUp]
public void setUp() {}
[TearDown]
public void tearDown() {}
[Test]
public void testDeposit() {}
[Test]
public void testWithdraw() {}
[Test]
public void testBalance() {}
}

1
If you skip the Step 3, the error underline will appear on keying in [TestFixture].
3
Step 5. During keying in the codes, an icon will be on the left side2. Click the icon
and choose Run AccountTests option (Figure 3) to run test runner (Figure 4).

Figure 3 The UnitRun Add-in icon

Step 6. There is no any test code in AccountTest class, so that the test runner will tell
you all pass. Now add some test data into AccountTest class.

[TestFixture]
public class AccountTests {
protected Account _testee;
protected double INIT_AMOUNT = 100.0d;
protected double DEPOSITED = 200.58d;
protected double BIG_WITHDRAW = 300.0d;
public void setUp() {
_testee = new Account(INIT_AMOUNT);
}
}

Step 7. Then add test scenario for each function

[Test]
public void testDeposit() {
Assert.AreEqual(INIT_AMOUNT, _testee.balance());
_testee.deposit(DEPOSITED);
Assert.AreEqual(INIT_AMOUNT + DEPOSITED, _testee.balance());
}

[Test]
public void testBalance() {
Assert.AreEqual(INIT_AMOUNT, _testee.balance());
}

2
There are two kinds of icon, the icon means that UnitRun will run all test in the test fixture.
Another icon means that UnitRun will only run the test defined in [Test] attribute.
4
[Test]
public void testWithdraw() {
_testee.withdraw(BIG_WITHDRAW);
Assert.AreEqual(INIT_AMOUNT, _testee.balance());
_testee.deposit(DEPOSITED);
Assert.AreEqual(INIT_AMOUNT + DEPOSITED, _testee.balance());
_testee.withdraw(BIG_WITHDRAW);
double balance = INIT_AMOUNT + DEPOSITED - BIG_WITHDRAW;
Assert.AreEqual(balance, _testee.balance());
}

Step 8. The test runner tell you one test failed.

Figure 4 Hmm...A test failed

Step 9. Try to fix the bug in the Account class and you will get an all pass (Figure 5).

Figure 5 Wow~ All Pass


– End Tutorial –

5
Appendix A – NUnit Installation
NUnit is a unit-testing framework for all .Net languages. Initially ported from
JUnit, the current production release, version 2.2, is the fourth major release of this
xUnit based unit testing tool for Microsoft .NET. It is written entirely in C# and has
been completely redesigned to take advantage of many .NET language features, for
example custom attributes and other reflection related capabilities. NUnit brings
xUnit to all .NET languages. Now, we start to install the NUnit:
1. Download the NUnit framework from website:
http://www.nunit.org/index.php?p=download
2. Execute the installer

Appendix B – ReSharper UnitRun Installation


NUnit provides a unit-testing framework for all .Net languages, but it does not
provide an integration add-in for Visual Studio 2005. In order to run unit test,
without add-in, you should run an independent program: nunit-gui.exe and load your
codes. Here, UnitRun is an add-in for Visual Studio 2005, supporting both NUnit
framework and csUnit framework, which let developer do coding/compiling/running
test in the Visual Studio 2005 at once.
1. Download the ReSharper UnitRun 1.0 from website:
http://www.jetbrains.com/unitrun/download/index.html
2. Register and get the free personal license:
http://www.jetbrains.com/unitrun/download/registration.html
3. Execute the installer
4. When you first time execute Visual Studio 2005, the Visual Studio will rise
a warring about that they can’t find the menu bar cause by traditional
Chinese. Check the ignore option and don’t remind this exception again
option.
5. Input the license you got in the above step.

You might also like