C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » General »

Use of NUnit is better way in the TDD


Posted Date: 11 Jun 2004    Resource Type: Articles    Category: General
Author: Swanand UmaranikarMember Level: Bronze    
Rating: 1 out of 5Points: 7



Introduction

NUnit is a very useful tool to have a better TDD, Test Driven Development. Though NUnit is simple to use, if not used properly can be expensive. This article is to have a compact way to write NUnit test script which will reduce the coding effort of programmer.

Sample Project

To have a better understanding, we will have a sample project for a Class Library say NUnitTest.

Here is the code for NUnitTest.cs

using System;

namespace NUnitTest3
{
public class AddIt
{
public int Add(int a, int b) {return a+b;}
}

public class RemoveIt
{
public int Remove(int a, int b) {return a-b;}
}

public class MyName
{
public static string myname;
public string getMyName() {return myname;}
public void setMyName(string my_name) {myname = my_name;}
}
}



Writing a Test Case

Here is the magic, which will reduce the effort of re-coding the tests. We will write the test code for the library inside the library itself. Many times programmer write aal test cases in different project and actucal code in different project. The test project then has a test code for entire solution. Writing the test script in the library itself is useful when we want to reuse the library, we can simply copy the script in new solution. Where as if the test script resids in different project, which has all test for entire solution, then we have to extract the tests and then copy in new solution.

So we will have TestClass.cs file in the same project, Programmer can give btter names for class and test class to co-relate the class fiels, viz. if Class name is Table, then test file can have name TestTable.cs.

The code for TestClass.cs can be,

using System;
using NUnit.Framework;
using NUnitTest3;


namespace NUNitTest3
{
[TestFixture]
public class AddItTest
{
[Test]
public void Add()
{
AddIt obj_add = new AddIt();
Assertion.Assert(obj_add.Add(10, 20) == 30);
}
}
[TestFixture]
public class RemoveItTest
{
[Test]
public void Remove()
{
RemoveIt obj_remove = new RemoveIt();
Assertion.Assert(obj_remove.Remove(20, 10) == 10);
}
}

[TestFixture]
public class MyNameTest
{
[Test]
public void settest()
{
MyName obj_myname = new MyName();
obj_myname.setMyName("Swanand");
Assertion.Assert(obj_myname.getMyName() == "Swanand");
}
[Test]
public void gettest()
{
MyName obj_myname = new MyName();
Assertion.Assert(obj_myname.getMyName() == null);
obj_myname.setMyName("Swanand");
Assertion.Assert(obj_myname.getMyName() == "Swanand");
}

}
}



Now complie the code and start NUnit-GUI.exe. Open the .dll file generated by the above compilation in NUnit UI, If there is no proble, then the Run button will be enabled and you can run the tests.

NOTE : While compilation, we need to add the Referance of nunit.framework in the project.

Summary

The above articale can be usefule for those who are new to NUnit environment. If some one wants to debug the Test script code, he can write a dummy application which will invode NUnit-GUI.exe, and can put break points in his code.



Responses

Author: Noby Abraham    11 Jun 2004Member Level: Silver   Points : 0
The article is nice and it is a good topic for an article. THere is not many article available about NUNit. If you can post few other articles about what is TDD, What is the role of NUNit, how to use it etc, that would be really great.


Author: Prajeesh.Kottarath.S    14 Jun 2004Member Level: Bronze   Points : 0
Very good basics!
Can u explain how to test database (select,insert, update, delete,etc) using NUnit ?


Author: Ramya    07 Oct 2004Member Level: Bronze   Points : 0
This article is just to the basics of NUnit.It will be helpful if it can provide in detail,all other aspects of NUnit.Especially there is no information regarding performing the tests using NUnit on pop-up windows, which are common in web applications.This article can also be improved by incorporating few more examples, that cover different scenarios.


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Tips for (DOTNET) Beginners
Previous Resource: Executing Commands
Return to Discussion Resource Index
Post New Resource
Category: General


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use