Tutorial NUnit
Tutorial NUnit
Tutorial NUnit
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
1
Tutorial – Test Account
Step 1. Create a new C# Console Application project. Then, add a class: Account.
2
Step 3. Add NUnit framework 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).
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);
}
}
[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 9. Try to fix the bug in the Account class and you will get an all pass (Figure 5).
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