Wednesday, March 26, 2014

Introduction NUNIT

Introduction:

>>NUnit is an automated unit testing framework for .NET

>>NUnit is free to use with your .NET projects.

>>NUnit is ported from another product called JUnit, which is a unit testing framework for Java applications.

>> NUnit provides user interface application to execute the test scripts and displays result

>>NUnit provides a class library which includes some classes and methods to help you write test scripts.

>>NUnit provides graphical user interface application to execute all the test scripts and show the result as a success/failure report (instead of message boxes from the test script).

>>NUnit is the most popular unit testing framework for .net applications.

>>NUnit does not create any test scripts by itself. You have to write test scripts by yourself, but NUnit allows you to use it's tools and classes to make the unit testing easier.

>>NUnit is an open source product. You can get this tool along with source code and modify the source code if required for your specific needs. However, 99% of the programmers do not mess with the source code. Instead, you can download only the executable and class library and implement unit test scripts for your application.

>>Now Microsoft offers a team collaboration system called 'Visual Studio Team System' (VSTS). This tool includes a unit testing framework also. So, eventually most of the developers may switch to VSTS instead of NUnit. However, NUnit will continue to exist because it is free and easy to use.



What is unit testing


Unit testing is a development procedure where programmers create tests as they develop software. The tests are simple short tests that test functionality of a particular unit or module of their code, such as a class or function. The general behavior of a unit test is that it sends a predefined message to the class and verifies the response. Yes, you heard it correct! Developers are responsible for unit testing, which means you have to code extra! Some 20% extra! Hold on, there are even more benefits.

Features of NUnit
 

Assertions
• Attributes
 

1. Assertions


Definition


Assertions are central to unit testing in any of the xUnit frameworks, and NUnit is no exception. NUnit provides a rich set of assertions as static methods of the Assert class.
The most commonly used assertions.
• Equality Asserts
• Identity Asserts
• Comparison Asserts
• Type Asserts
• Condition tests
• Utility methods
• StringAssert
• CollectionAssert
• FileAssert
• Equal Constraint (Available NUnit 2.4)
• Same As Constraint (Available NUnit 2.4)
• Condition Constraints (Available NUnit 2.4)
• Comparison Constraints (Available NUnit 2.4)
• Type Constraints (Available NUnit 2.4)
• String Constraints (Available NUnit 2.4)
• Collection Constraints (Available NUnit 2.4)
• Compound Constraints (Available NUnit 2.4)
• Custom Constraints (Available NUnit 2.4)
 

2. Attributes
 

Category
• Description
• ExpectedException
• Explicit
• Ignore
• Platform
• Property
• SetUp
• SetUpFixture
• Suite
• TearDown
• Test
• TestFixture
• TestFixtureSetUp
• TestFixtureTearDown


Working of Nuint with ASP:
 

Step 1


Install Download Nuint which is an open source .
 

Step 2
 

Install Nuint in your system
 

Step 3


Open your c# project and Create c# project then write a bussinessLogic in Class.cs file
For e.g.:
Create Invoice class for customer placing an order
 

Step 4


Add References in web site
Browse references from
C:\Program Files\NUnit 2.5.2\bin\net-2.0\framework
i.e. nunit.framework.dll
 

Step 5


Add another cs file in same web site.
Add namespace of Nuint
 
i.e. using NUnit.Framework;
create class TestNuint
above class TestNuint add attribute
 
[TestFixture]
i.e.
[TestFixture]
class TestNuint
{
}

Step 6


Create TestMethod to test businesslogic
 
i.e
public void TestInvoice()
{
}
Above this method Add attribute [Test]
i.e
[Test]
public void TestInvoice()
{
}

Step 7


Create object for bussinessLogic
i.e
 
clsInvoice objInvoice = new clsInvoice();
create your own expected result variable
i.e
//Expected Result
double _expectedTotalCost;
string _expectedProductDetails;
string _expectedInvoice;
string _CustomerName = "";
string _ProductName = "";
double _ProductCost = 0;
double _Quantity = 0;
Assign same values to both your expected result variable and objInvoice varibles
 
i.e
//Set Expected Result Variables
_CustomerName = "Shyam";
_ProductName = "L G Color TV";
_ProductCost = 22000;
_Quantity = 2;
_expectedTotalCost = _ProductCost * _Quantity;
_expectedProductDetails = _ProductName + " Cost of " + _ProductCost;
_expectedInvoice = _CustomerName + " For product " + _ProductName + " Quantity " + _Quantity + " Total Cost " + _expectedTotalCost;


//Set same values to object of class businesslogic


clsInvoice objInvoice = new clsInvoice();

objInvoice.CustomerName = "Shyam";
objInvoice.ProductName = "L G Color TV";
objInvoice.ProductCost = 22000;
objInvoice.Quantity = 2;
_actualTotalCost = objInvoice.ProductCost * objInvoice.Quantity;
_actualProductDetails = objInvoice.DisplayProductDetails();
_actualInvoice = objInvoice.DisplayInvoice();


//Now to Compare two result

//add

Assert.AreEqual(_expectedInvoice, _actualInvoice);

Assert.AreEqual = It compares two objects makes special provisions so that numeric values of different types compare as expected.

Finally Build Solution
 

Step 8
 

Open your Nuint GUI Click File Create Nuint web site
then add assembly
 
Select your Build Solution .dll file from C# web site
i.e
E:\nunit\Nuint2\Nuint2\bin\Release
 
Finally Click on Run Button to test your c# web site 

Test Result
 

If test runs successfully then it will display
This test successful

Assert.AreEqual(_expectedInvoice, _actualInvoice);

_expectedInvoice and _actualInvoice values are equal


If Test is unsuccessfull having some errors then it will display
 
This test fails due to

Assert.AreEqual(_expectedInvoice, _actualInvoice);

_expectedInvoice and _actualInvoice values are unequal



No comments:

Post a Comment