Вы находитесь на странице: 1из 69

What is Unit Testing

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

What Is Unit Testing

Unit Testingis alevel of software testingwhere


individual units/ components of a software are tested.
The purpose is to validate that each unit of the software
performs as designed.

A unit is the smallest testable part of software. It usually


has one or a few inputs and usually a single output.

What Is Unit Testing Cont..

What Is Unit Testing Cont..

Integration Testing

It is alevel of software testing where individual units are


combined and tested as a group

System Testing :

It is a level of software testing where a complete and integrated


software is tested.
The purpose of this test is to evaluate the systems compliance
with the specified requirements.
Example:
During the process of manufacturing a ballpoint pen, the cap, the
body, the tail, the ink cartridge and the ballpoint are produced
separately and unit tested separately. When two or more units are
ready, they are assembled and Integration Testing is performed. When
the complete pen is integrated, System Testing is performed.

What Is Unit Testing Cont..


ACCEPTANCE TESTING:

Acceptance Testingis alevel of software testingwhere a


system is tested for acceptability.
The purpose of this test is to evaluate the systems
compliance with the business requirements and assess
whether it is acceptable for delivery.

What Is Unit Testing Continue..

When is it performed?

Unit Testing is the first level of testing and is performed prior


to Integration Testing

Who performs it?


Unit Testing is normally performed by software developers
themselves or their peers. In rare cases it may also be
performed by independent software testers.

Why should we have Unit Testing in Project


?

Unit testing increases confidence in changing/


maintaining code. If good unit tests are written and if
they are run every time any code is changed, we will be
able topromptly catchany defects introduced due to
the change. Also, if codes are already made less
interdependent to make unit testing possible, the
unintended impact of changes to any code is less.

Why should we have Unit Testing in


Project ?

Codes are more reusable. In order to make unit testing


possible, codes need to be modular. This means that
codes are easier to reuse.
The cost of fixing a defect detected during unit testing
is lesser in comparison to that of defects detected at
higher levels. Compare the cost (time, effort,
destruction, humiliation) of a defect detected during
acceptance testing or when the software is live.

TIPS

Find a tool/ framework for your language.


Do not create test cases for everything. Instead,
focus on the tests that impact the behavior of
the system.
Isolate the development environment from the
test environment.
Use test data that is close to that of production.

TIPS

Before fixing a defect, write a test that exposes


the defect. Why? First, you will later be able to
catch the defect if you do not fix it properly.
Second, your test suite is now more
comprehensive. Third, you will most probably be
too lazy to write the test after you have already
fixed the defect.

TIPS

Aim at covering all paths through the unit. Pay


particular attention to loop conditions.
Make sure you are using a version control
system to keep track of your test scripts.
Perform unit tests continuously and frequently.
In addition to writing cases to verify the
behavior, write cases to ensure performance of
the code.

How do I do Unit Testing?

The first step is to decide how to test the method in question


before writing the code itself. With at least a rough idea of
how to proceed, you can then write the test code itself, either
before or concurrently with the implementation code. If youre
writing unit tests for existing code, thats fine too, but you may
find you need to refactor it more often than with new code in
order to make things testable.

How Do I Do Unit Testing?

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

testing helps you avoid any collateral damage as well as any


immediate, local bugs.

Something about
nUnit

There are many different types of


Unit testing framework such as
Mstest,Nunit, Xunit, Junit.

Something about
nUnit

Nunit is a unit-testing framework


for all .Net languages.
It has been written entirely in C#.

Important nUnit Links

http://www.nunit.org
http://nunit.org/download.html
http://sourceforge.net/projects/nuni
t/

The Input & Output of


nUnit

Dll, exe file

nUnit

XML file
(Optional)

Processing details
on the GUI or
Command prompt

Things to do before testing

Download Nunit.msi from


http://nunit.org/download.html and
install nunit.

Things to do before testing


cont.

Add nunit.frameworks.dll to your


project references. This dll resides
in nunit\bin folder.
The next few screen shots show
how to add this dll file if your
project has been opened as a
classlibrary.

Select the Solution


explorer mode

Right click references and


select Add reference

Select Browse

Goto the nunit\bin directory, choose


nunit.framework.dll and press open.

Now press ok in the Add


reference page

The dll has been added to


your reference list.

In your code, add using


nunit.framework.

NUnit Testing Framework Attribute

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
{
}
}

NUnit Testing Framework Attribute

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()
{
...
}
}

NUnit Testing Framework Attribute

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().

NUnit Testing Framework Attribute

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.

NUnit Testing Framework Attribute

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()
{
...
}
}

NUnit Testing Framework Attribute

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; }

Add testfixture to your


program

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

How to write your test


code

Suppose you want to test the functions


in a class, your testfixture(test stub)
should look somewhat like this

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;
}
}

Calculator Example cont


[TestFixture]
public class calcTest
{
[Test]
public void AdditionTests(){
calculator cal=new calculator();
cal.Addition(5,10);Console.Write("TESTING 5+10\n");
Assert.AreEqual(15,cal.output());
cal.Addition(-5,10);Console.Write("TESTING -5+10\n");
Assert.AreEqual(5,cal.output());
}
[Test]
public void DivisionTests(){
calculator cal=new calculator();
cal.Division(10,2); Console.Write("TESTING 10div2\n");
Assert.AreEqual(5,cal.output());
cal.Division(10,0); Console.Write("TESTING 10div0\n");
Assert.AreEqual(0,cal.output());
}
}//for testfixture
}//for namespace

Build the Project

After the testfixture is complete,


build the project. This will create a
dll file which will reside in
projectname\bin\debug folder.

LOCATION of dll

In this example I am copying the dll


file from projectname\bin\debug
folder to nunit\bin folder for my
convenience.
But this is not a requirement when
we are testing and the dll can be
run from projectname\bin\debug
folder.

Location of exe files

Starting the GUI from


console
The GUI can be started from the console
by executing
nunit-gui.exe [inputfilename][options]
Ex: nunit-gui.exe nunit.tests.dll run
This option will load the dll file and
run the test.
* Options with their values are separated
by an equal, colon or a space.

GUI MODE

The steps to be followed are

Load the Dll file.


Select Run.
View the errors if any on the GUI
screen.
Save the output XML if necessary.

create a new project


namespace TestNUnit
{
public class MyTest
{
public int Add(int a, int b)
{
return a + b;
}
public int Substract(int a, int b)
{ return a - b; }
public int Divide(int a, int b) { return a / b; } } }

Add referencenunit.framework.dll

Add a class inside the newly created


project to write the test methods

using System.Web;
using NUnit.Framework;
using TestNUnit;
namespace UnitTest

{
[TestFixture]

public class Nunit

{ MyTest test=null;

[SetUp]

public void Initialize()

{ test = new MyTest(); }

[TearDown]

public void CleanUp() { test = null; }

Add a class inside the newly created


project to write the test methods
Cont..
[Test]
public void Addition()
{
Assert.AreEqual(5, test.Add(2, 3));
}
[Test]
public void Substraction()
{
Assert.AreEqual(0, test.Substract(2, 2));
}
[Test]
public void Divide()
{
Assert.AreEqual(1, test.Devide(2, 2));
}

Compile the project and


openNUnit.exefrom the Start menu.

Select open in the NUNIT


GUI

Select the dll which you want to


test(In this case, we have moved the
dll to the nunit\bin folder)

From the File menu, click on Open


project, locate the.dllfile, and load
into NUnit.

The following screen loads all test


methods.

Runs Perfectly

Tests Fail

To save the output in an


XML file

Writing in the output file

In the GUI mode, the user must


specify that he/she wants to save
the output. No xml output file is
automatically created on the users
behalf.
But in Console mode, a XML file is
created even if not specified by the
user.

Console mode

The command to execute the exe in


console mode is
nunit-console.exe [filename] [option]
Example:
nunit-console.exe classlibrary1.dll
-XML=ourfile.xml

The different options are


Nunit-console [inputfiles][options]
Options
/Fixture=STR
/config=STR
/XML=STR
/transform=STR
/xmlConsole
/output=STR
/err=STR
/labels
/include = STR
/exclude = STR
/noshadow
/thread
/wait
/nologo
/help

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

As mentioned earlier, in console


mode the xml file is automatically
created as testresult.xml in the
nunit\bin folder.
The user can change the name of
the output file by saying -XML =
newfilename.xml as shown in the
previous slide.

Now lets Examine the


Output file (test passed)

Test Failed

Failure message(1
message)

Test Failure(2 tests)

Some of the frequently used Assert


methods are:

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.")

Some of the frequently used Assert


methods are:

Assert.IsNull Verifies the passed object is null.


Ex.
Assert.IsNull(actual)
Assert.IsNull(actual, "The actual is not null.")
Assert.IsNotNull Verifies the passed object is not null.
Ex.
Assert.IsNotNull(actual)
Assert.IsNotNull(actual, "The actual is null.")

Some of the frequently used Assert


methods are:

Assert.IsEmpty Verifies the passed string is empty.


Ex.
Assert.IsEmpty(actual)
Assert.IsEmpty(actual, "The passed string is not
empty.")
Assert.IsTrue Verifies the passed condition is true or not.
Ex.
Assert.IsTrue(actual)
Assert.IsTrue(actual, "The passed condition is not
true.")

Some of the frequently used Assert


methods are:

Assert.IsFalse Verifies the passed condition is false or not.


Ex.
Assert.IsFalse(actual)
Assert.IsFalse(actual, "The passed string is not false.")
Assert.IsInstanceOf Verifies the passed object is of the
particular type.
Ex.
Assert.IsInstanceOf(typeof(Employee), actual)
Assert.IsInstanceOf(typeof(Employee), actual, "The
object is of not type Employee.")

NUNIT
Thank you

Вам также может понравиться