Steps to perform Automated Unit Testing using NUnit | URDINESH

Software Programming, Tutorials, Interview Preparations,Stock Market,BSE/NSE, General informations

Sunday, April 27, 2014

Steps to perform Automated Unit Testing using NUnit

Automated Testing Using NUnit
                                                                                                                 
Steps to be followed:

1.      Download NUnit fromwww.nunit.org
2.      Extract the zip file.
3.      Create a new “test project” and add a reference to the nunit.framework.dll file which is found under the folder \NUnit-2.6.2\bin\framework.
4.      Ensure that you have included “using nunit.framework;” namespace in order to proceed testing with NUnit.
5.      Delete the UnitTest1.cs file which will be available under the test project.
6.      Create a new class (Right click on the newly created test project and select Add->Class..) and mark the class as public.
7.      Add the [TestFixture] attribute to the new class which contains test methods and other variables.
8.      Add the [TestCase] attribute to the method which is contained inside the newly created class such that the method can be used for testing the functionality.
9.      Add the NUnit to the Visual Studio from ToolsàExternal Tools. A dialog box opens. Add title as NUnit. In the command box, add nunit.exe from \NUnit-2.6.2\bin\nunit.exe. Click Ok. NUnit is added to the external tools.
10.  Compile.
11.  Go to Tools in Visual Studio and click NUnit to open the nunit.
12.  Then Go to FileàOpen ProjectàMyDocumentsàVisual StudioàProjectsàYourTestProjectàDebugàBinàYourTestProject.dll.
13.  The Test Project created by you is loaded in the nunit with the functionalities you have written.
14.  Click “Run” button in the nunit window.
15.  If there is no error in the business logic, the progress bar in green color will appear indicating the test is passed successfully. Else, the progress bar with red color will appear indicating flaw in the method which is tested. 

Example for Simple TestCase of Addition and Subtraction:

[TestFixture]
public class TestClass
    {
        [TestCase]
public void AddTest()
        {
MathsHelper helper = new MathsHelper();
int result = helper.Add(20, 10);
Assert.AreEqual(30, result);
        }

        [TestCase]
public void SubtractTest()
        {
MathsHelper helper = new MathsHelper();
int result = helper.Subtract(20, 10);
Assert.AreEqual(10, result);                                            
        }
    }

In Program.cs:       

class Program
    {
static void Main(string[] args)
        {
         }
}
public class MathsHelper
    {
publicMathsHelper() { }
publicint Add(int a, int b)
        {
int x = a + b;
return x;
        }

publicint Subtract(int a, int b)
        {
int x = a - b;
return x;
        }
    }






ADVANTAGES OF NUNIT

·         Tests can be conducted and repeated as often as may benecessary (reproducible)
·         the Unit Test tool enhances the quality of codes with ashort period and at lower development costs
·         can be automated
·         the defragmenting of the software in smaller modules andcomponents reduces its complexity

LIMITS OF NUNIT

·         It does not offer the option to test for private membersas the TestCase category from which all tests in Nunitas derived lies in a separate test category
·         Tests must be written as codes in a file. Modification of these test suites requires programming knowledge.
·         NUnit can only analyse the business logic. However, Special frameworks that test GUI elements, such as NUnitASP and NUnitForms are available.
·         Nunit cannot be used in ASP.Net.

·         Tests can only highlight errors. They do not notify theinexistence of the same.

No comments:

Post a Comment

Thanks for your valuable comments

Followers