Note :This post is first published on June-2011 in my previous blog Techkindle. Moving the content here.
Steps to use NUnit
- First download current version of NUnit here.
- Install it in your machine.
- Create one sample Class Library in visual studio, and name it Calculations.
- Now, we just write some small calculation methods like Addition and Subtraction etc..
Code(.cs file) will be like this
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Calculations { public class Calc { private int sum; private int subtr; public int Addition(int a, int b) { sum = a + b; return sum; } public int Subtraction(int a, int b) { subtr = a - b; return subtr; } } }
Then Create another class library, where we can write the test cases for the above methods.Let us assume it as a NunitSampleTest.cs
-
- For this Class library we need to add nunit reference.
If you have installed NUnit in C drive, You can find this reference from the below path
C:Program FilesNUnit 2.5.10binnet-2.0frameworknunit.framework.dll - Then we need to add Calculations reference.
- For this Class library we need to add nunit reference.
Now we need to write the test cases as below
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Calculations; using NUnit.Framework; namespace NUnitSampleTest { [TestFixture] public class NUnitTestClass { [Test] public void AdditionTest() { Calc newobj = new Calc(); int sum = newobj.Addition(20, 10); Assert.AreEqual(30, sum); } [Test] public void SubtractionTest() { Calc newobj = new Calc(); int subtr = newobj.Subtraction(20, 10); Assert.AreEqual(10, subtr); } } }
Note: In the above code we can find out two new attributes, [TestFixture] and [Test] and one class Assert.
The TestFixture attribute is specified for the classes, When we attach this attribute to a class, the Test Runner application will scan it for test methods.
The only restrictions on classes that use the TestFixture attribute are that they must have a public default constructor (or no constructor which is the same thing).
The Test attribute marks a specific method inside a class that has already been marked as a TestFixture, as a test method.
Assertions are central to unit testing. The Assert class provides a variety of static methods, using which we can test the result with the actual expected value.
this all completes the coding part
Now to test this application, we need to open NUnit UI, this we can find out at the below path
(C:Program FilesNUnit 2.5.10binnet-2.0)
or
Goto start->All Programs ->NUnit 2.5.10
The window will be like as shown below
Now Goto File->open Project , then Go to the path and click on the NunitSampleTest.Dll
It will open the methods as shown below..
so right click on the individual methods or whole class and click on Run
If all the test cases are passed successfully, then we can see the green color Line as shown below
But if get Red color line, then it means TestCase is failed.
The above figure indicates that we have an issue in Subtraction Method..
Note: To Make things simpler we have hard coded the values in the TestCase methods.
Rather than hard coding the values, we can create one separate class with values and can pass them to respective methods.
Happy Learning 🙂