nunit navaneeth rajkumar open source tools team project central.net

42
NUNIT Navaneeth Rajkumar Open Source Tools Team Project Central.Net

Upload: isaac-oneal

Post on 21-Jan-2016

216 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: NUNIT Navaneeth Rajkumar Open Source Tools Team Project Central.Net

NUNIT

Navaneeth RajkumarOpen Source Tools Team

Project Central.Net

Page 2: NUNIT Navaneeth Rajkumar Open Source Tools Team Project Central.Net

Something about nUnit…

NUnit is a unit-testing framework for all .Net languages.

It has been written entirely in C#.

Page 3: NUNIT Navaneeth Rajkumar Open Source Tools Team Project Central.Net

Important nUnit Links

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

it/

Page 4: NUNIT Navaneeth Rajkumar Open Source Tools Team Project Central.Net

The Input & Output of nUnit

nUnit Dll, exe file XML file

(Optional)

Processing details on the GUI or Command prompt

Page 5: NUNIT Navaneeth Rajkumar Open Source Tools Team Project Central.Net

The 2 operational modes

GUI mode Console mode (Project Central.

net)

Page 6: NUNIT Navaneeth Rajkumar Open Source Tools Team Project Central.Net

Things to do before testing

Download Nunit-2.2.0.msi from http://nunit.org/download.html and install nunit.

Page 7: NUNIT Navaneeth Rajkumar Open Source Tools Team Project Central.Net

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.

Page 8: NUNIT Navaneeth Rajkumar Open Source Tools Team Project Central.Net

Select the Solution explorer mode

Page 9: NUNIT Navaneeth Rajkumar Open Source Tools Team Project Central.Net

Right click references and select Add reference

Page 10: NUNIT Navaneeth Rajkumar Open Source Tools Team Project Central.Net

Select Browse

Page 11: NUNIT Navaneeth Rajkumar Open Source Tools Team Project Central.Net

Goto the nunit\bin directory, choose nunit.framework.dll and press open.

Page 12: NUNIT Navaneeth Rajkumar Open Source Tools Team Project Central.Net

Now press ok in the Add reference page

Page 13: NUNIT Navaneeth Rajkumar Open Source Tools Team Project Central.Net

The dll has been added to your reference list.

Page 14: NUNIT Navaneeth Rajkumar Open Source Tools Team Project Central.Net

In your code, add using nunit.framework.

Page 15: NUNIT Navaneeth Rajkumar Open Source Tools Team Project Central.Net

Add testfixture to your program

Page 16: NUNIT Navaneeth Rajkumar Open Source Tools Team Project Central.Net

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

Page 17: NUNIT Navaneeth Rajkumar Open Source Tools Team Project Central.Net

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

Page 18: NUNIT Navaneeth Rajkumar Open Source Tools Team Project Central.Net

Calculator Exampleusing 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;

}}

Page 19: NUNIT Navaneeth Rajkumar Open Source Tools Team Project Central.Net

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

Page 20: NUNIT Navaneeth Rajkumar Open Source Tools Team Project Central.Net

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.

Page 21: NUNIT Navaneeth Rajkumar Open Source Tools Team Project Central.Net

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.

Page 22: NUNIT Navaneeth Rajkumar Open Source Tools Team Project Central.Net

Location of exe files

Page 23: NUNIT Navaneeth Rajkumar Open Source Tools Team Project Central.Net

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.

Page 24: NUNIT Navaneeth Rajkumar Open Source Tools Team Project Central.Net

Starting the GUI from console

nunit-gui.exe [inputfilename][options]Options:/help short format: /?/config=STR project config to load/noload suppress loading of last proj/run Automatically run after load/fixture=STR Fixture to test

Page 25: NUNIT Navaneeth Rajkumar Open Source Tools Team Project Central.Net

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.

Page 26: NUNIT Navaneeth Rajkumar Open Source Tools Team Project Central.Net

Select open in the NUNIT GUI

Page 27: NUNIT Navaneeth Rajkumar Open Source Tools Team Project Central.Net

Select the dll which you want to test(In this case, we have moved the

dll to the nunit\bin folder)

Page 28: NUNIT Navaneeth Rajkumar Open Source Tools Team Project Central.Net

Runs Perfectly

Page 29: NUNIT Navaneeth Rajkumar Open Source Tools Team Project Central.Net

Tests Fail

Page 30: NUNIT Navaneeth Rajkumar Open Source Tools Team Project Central.Net

To save the output in an XML file

Page 31: NUNIT Navaneeth Rajkumar Open Source Tools Team Project Central.Net

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.

Page 32: NUNIT Navaneeth Rajkumar Open Source Tools Team Project Central.Net

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

Page 33: NUNIT Navaneeth Rajkumar Open Source Tools Team Project Central.Net

The different options areNunit-console [inputfiles][options]Options

/Fixture=STR Fixture to test/config=STR project configuration to load/XML=STR Name of XML output file/transform=STR name of transform file/xmlConsole display XML to the console/output=STR File to receive test output (short :/out=STR) /err=STR File to receive test error output/labels Label each test in stdout/include = STR list of categories to include/exclude = STR list of categories to exclude/noshadow disable shadow option/thread runs thread on a separate thread/wait wait for input before closing console window /nologo Do not display the logo/help Display help (short format: /?)

Page 34: NUNIT Navaneeth Rajkumar Open Source Tools Team Project Central.Net

Runs Perfectly

Page 35: NUNIT Navaneeth Rajkumar Open Source Tools Team Project Central.Net

Test Fails

Page 36: NUNIT Navaneeth Rajkumar Open Source Tools Team Project Central.Net

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.

Page 37: NUNIT Navaneeth Rajkumar Open Source Tools Team Project Central.Net

Now lets Examine the Output file (test passed)

Page 38: NUNIT Navaneeth Rajkumar Open Source Tools Team Project Central.Net

Test Failed

Page 39: NUNIT Navaneeth Rajkumar Open Source Tools Team Project Central.Net

Failure message(1 message)

Page 40: NUNIT Navaneeth Rajkumar Open Source Tools Team Project Central.Net

Test Failure(2 tests)

Page 41: NUNIT Navaneeth Rajkumar Open Source Tools Team Project Central.Net

Proposed Architecture The architecture which has been

proposed by the architect is The user shall run his tests in the

console mode of the nUnit tool after the dll has been created by the nAnt tool.

The output XML file shall be sent to the User Interface to accommodate information about the user, date, time and other related information.

Page 42: NUNIT Navaneeth Rajkumar Open Source Tools Team Project Central.Net

NUNIT

Thank you