Академический Документы
Профессиональный Документы
Культура Документы
When is it Performed ?
Who performs it?
Why do we need Unit Testing
What are tips should be followed
How do I do Unit Testing?
Types of unit testing
Example of Nunit Test cases
Integration Testing
System Testing :
When is it performed?
TIPS
TIPS
TIPS
you run the test itself, and probably all the other tests
in that part of the system, or even the entire systems tests if
that can be done relatively quickly. Its important that all the
tests pass, not just the new one. This kind of basic regression
Something about
nUnit
Something about
nUnit
http://www.nunit.org
http://nunit.org/download.html
http://sourceforge.net/projects/nuni
t/
nUnit
XML file
(Optional)
Processing details
on the GUI or
Command prompt
Select Browse
TextFixture Attribute
The TestFixture attribute is an indication that a class contains test methods.
When you mention this attribute to a class in your project, the Test Runner
application will scan it for test methods. The following code illustrates the usage
of this attribute as in the following:
usingSystem;
usingNUnit.Framework;
usingSystem.Text;
namespaceUNitTesting
{
[TestFixture]
publicclassProgram
{
}
}
Test Attribute
The Test attribute indicates that a method within a test fixture
should be run by the Test Runner application. The method must be
public, return void, and will not be run when the Test Fixture is run.
The following code depicts the use of this attribute as in the
following:
[TestFixture]
publicclassProgram
{
[Test]
publicvoidTest()
{
...
}
}
Assert Class
the Assert class is used to confirm whether the test cases is
producing the expected result or not using its auxiliary
methods such as AreEqual() or AreNotEqual().
ExpectedException Attribute
You could fortify your code to handle various exceptions by using try...Catch
blocks. But sometimes you have some circumstances where you actually want
to ensure that an exception occurs. To overcome such a problem you should use
the ExpectedException attribute as in the following;
[TestFixture]
publicclassProgram
{
[Test]
[ExpectedException(typeof(DivideByZeroException))]
publicvoidTest()
{
inti=10,j=0,x;
x = i / j;
}
}
In the previous code, we are intentionally committing a divide by zeroexception
that is to be detected during the NUnit Test execution.
Ignore Attribute
The Ignore attribute is required to indicate that a test should not be run on a
particular method. Use the Ignore attribute as in the following:
[TestFixture]
publicclassProgram
{
[Test]
[Ignore("This method is skipping")]
publicvoidTest()
{
...
}
}
SetUp
This attribute is used inside aTestFixtureto provide a common set of functions
that are performed just before each test method is called.
TearDown
This attribute is used inside a TestFixture to provide a common set of functions
that are performed after each test method is run.
[TestFixture]
public class NUnit
{
MyTest test=null;
[SetUp]
public void Initialize()
{
test = new MyTest();
}
[TearDown]
public void CleanUp() { test = null; }
TestFixture Example
[TestFixture]
public class calcTest{
[Test]
public void AdditionTests(){
calculator cal=new calculator();
cal.Addition(5,10); //CASE 1
Assert.AreEqual(15,cal.output());
cal.Addition(-5,10); // CASE 2
Assert.AreEqual(15,cal.output());
}
[Test]
public void DivisionTests(){
calculator cal=new calculator();
cal.Division(10,2); // CASE 1
Assert.AreEqual(5,cal.output());
cal.Division(10,0); //CASE 2
Assert.AreEqual(2,cal.output());
}
}//for testfixture
using NUnit.Framework;
Namespace sample1{
public class sampleclass(){
public void func1(){}
}//for sampleclass
[TestFixture]
public class sampleTest(){
[Test]
public void test1(){
testcase1;
testcase2;
}//for test1()
}//for sampleTest textfixture
}//for namespace
Calculator Example
using System;
using NUnit.Framework;
namespace ClassLibrary1
{
public class calculator
{
private int result;
public void Addition(int num1,int num2){
result=num1+num2;
}
public int output(){
return result;
}
public void Division(int num1,int num2){
if(num2==0){
throw new DivideByZeroException();
}
result = num1/num2;
}
}
LOCATION of dll
GUI MODE
Add referencenunit.framework.dll
using System.Web;
using NUnit.Framework;
using TestNUnit;
namespace UnitTest
{
[TestFixture]
{ MyTest test=null;
[SetUp]
[TearDown]
Runs Perfectly
Tests Fail
Console mode
Fixture to test
project configuration to load
Name of XML output file
name of transform file
display XML to the console
File to receive test output (short :/out=STR)
File to receive test error output
Label each test in stdout
list of categories to include
list of categories to exclude
disable shadow option
runs thread on a separate thread
wait for input before closing console window
Do not display the logo
Display help (short format: /?)
Runs Perfectly
Test Fails
Output file
Test Failed
Failure message(1
message)
Method Description
Assert.AreEqual Verifies the two objects are equal.
Ex.
Assert.AreEqual(expected, actual)
Assert.AreEqual(expected, actual, "The expected and
actual are not equal.")
Assert.AreNotEqual Verifies that two objects are not
equal.
Ex.
Assert.AreNotEqual(expected, actual)
Assert.AreNotEqual(expected, actual, "The expected
and actual are equal.")
NUNIT
Thank you