NUnit - Test Driven Methodology
Mart 17, 08 by aligorkemIn the software development process is very confused. Although most developers write code after debug and test. However, this methodology is very classic.
Test Driven Methodology is advocating writes once test after write code. Today, many developers using C# , VB or Java, all programming languages have many test development methodologies but Test Driven Methodology of the highest importance for us. If we use this methodology, we will very quickly development, and it is amazing.
This article contains :
Setting up the development environment
Create Projects;
First Class Library
Second Class Library
Attributes
Custom Assert Class
Result
Download Source Code
We have many advantages when we use the Test Driven Methodology. We get quick result for our projects and codes, because we can see our codes run or do not run in minutes. We have been clean and useable codes when use the TDD.
Which framework support our programming languages?
Language TDD
C# NUnit
Java Junit
PHP PHPUnit
C++ cppUnit
Setting up the development environment
We must be setup the development environment before use the NUnit. Firstly, download NUnit framework from www.nunit.org and install it.
My development environment:
Visual Studio 2008
.Net Framework 3.5
NUnit
Resharper (doesn’t requires for NUnit example)
Create Projects;
I created two class library for basic NUnit example. The first project name is “Accounts” and this library have one class. This class contains to basic property for our examples.
The second project is test library and this library used from NUnit. This library contains one class and this class have just a test methods. I wrote many comments for basic usage. I hope this information very useful for NUnit.
First Class Library
Second Class Library
Second library have different test methods and we have to use different attribute for NUnit Framework.
[TestFixture]
public class AccountTest
{
If we add the attribute we will explain this class contains test code.
[Test]
public void OpenAccount()
{
//Create basic account
Account account = new Account("Ali",12345);
}
OpenAccount method has a [Test] attribute. This attribute associated with a test method for NUnit.
Assert.AreEqual(50,account.Money,"I have 50 dollar.");
//We have to declare to EcpectedException
[ExpectedException(typeof(ArgumentNullException))]
public void OpenAccount_Exception()
{
//Create basic account
Account account = new Account(null, 12345);
}
Attributes
If we want to several parameters pass to test class or open external source, use class libraries.We will use to Initialize method and marked as SetUp.
[SetUp]
public void Initialize()
{
//Setup block firstl started from NUnit when we run the test
//We can’t see in the NUnit GUI
}
Sometimes, we can’t test some methods or application on some platform. Maybe .Net 2.0 platform hasn’t several methods or process.
[Test]
[Platform(Exclude = "Net-2.0")]
public void Attribute_Exclude()
{
//This test won’t pass when we run the NUnit GUI.
//but we add Platform attribute and exclude to set of .Net 2.0 platform
Account account = new Account(null, 12345);
}
Culture Attribute :
If we use the custom culture object such as money or date, and we don’t want to test this process.We will use Culture attribute.
[Test]
[Culture("fr-FR")]
//[Culture("en-EN")]
public void Attribute_Culture()
{
//This test won’t pass when we run the NUnit GUI.
//but we add Culture attribute and set of fr-FR value
//If we change the value to en-EN , we won’t see to pass the test
Account account = new Account(null, 12345);
}
Oftentimes, we want to test several custom objects or process but default assertion methods maybe don’t have sufficient for this request. Custom assertion class is static because we will call in Test methods.
and other test methods :
{
//Create basic account
Account account = new Account("Ali", 12345);
//If we can’t signed aggrement , we won’t pass the test
account.SignAccountAgreement();
//Assert class is checking the account signed.
CustomAssert.IsSignedAggrement(account);
}
[Test]
public void CustomAssert_EnoughMoney()
{
//Create basic account
Account account = new Account("Ali", 12345);
//If we can’t signed aggrement , we won’t pass the test
account.SignAccountAgreement();
//He is very rich man.
account.Deposit(600);
//Assert class is checking the account signed.
//Do you have enough money for buy a new computer?
CustomAssert.HasEnoughMoney(account);
}
Result
If we run the NUnit GUI , we will see all screen is green








