44
NUnit – A Unit Test Framework for .Net

NUnit Presentation

Embed Size (px)

Citation preview

Page 1: NUnit Presentation

NUnit – A Unit Test Framework for .Net

Page 2: NUnit Presentation

Topics What is NUnit ? Features Writing a Unit Test Code Examples Pros and Cons Conclusion

Page 3: NUnit Presentation

NUnit framework NUnit framework is port of JUnit framework from java and Extreme

Programming (XP). This is an open source product. You can download it from http://www.nunit.org.

The NUnit framework is developed from ground up to make use of .NET framework functionalities.

NUnit is a unit-testing framework for all .Net languages. It is written entirely in C#. NUnit is much the same as all the Extreme Programming test frameworks

(xUnits) NUnit was initially ported from JUnit. It uses an Attribute based programming model. It loads test assemblies in

separate application domain hence we can test an application without restarting the NUnit test tools.

The NUnit further watches a file/assembly change events and reload it as soon as they are changed. With these features in hand a developer can perform develop and test cycles sides by side.

Page 4: NUnit Presentation

What NUnit Framework is not:

It is not Automated GUI tester. It is not a scripting language, all test are written in .NET supported language

e.g. C#, VC, VB.NET, J# etc. It is not a benchmark tool. Passing the entire unit test suite does not mean software is production

ready.

NUnit is simple to understand and easy to learn. Object Oriented. NUnit is flexible - Test code can be in any .Net language. NUnit uses the "attribute" feature of .NET to identify tests.

Features

Page 5: NUnit Presentation

Download & Installation http://www.nunit.org http://nunit.org/download.html http://sourceforge.net/projects/nunit/ Download Nunit-2.2.0.msi from http://nunit.org/download.html and

install nunit. 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 6: NUnit Presentation

Simple concepts

. Test fixture : a class that contains one ore more test methods . Test method : a method that executes a specific test . Test runner : an application that finds and executes test methods on test fixtures . Assertion : a Boolean expression that describes what must be true when some action has been executed . Expected Exception : the type of an Exception we expect to be thrown during execution of a test method . Setup : Code that is run before every test method is executed (eg, logging in as a particular user or initializing a singleton) . Teardown : Code that is run after every test method has finished (eg, deleting rows from a table that were inserted during the test)

Page 7: NUnit Presentation

Cons C based extensions not available. Lack of awareness among .Net Community.

Pros NUnit is Open Source and highly extensible. Platform independent. Simple and easy to learn syntax. When integrated with NAnt, can be used for incremental projects.

Page 8: NUnit Presentation

The Input & Output of nUnit

NUnit Dll, exe file XML file (Optional)

Processing details on the GUI or Command prompt

Page 9: NUnit Presentation

Writing Unit Test Code

Every Class must have a corresponding Test Class named TestClassName

One test method per public method, named testMethodName. In each test method, test good results and failures Every Test Class must have the attribute [TestFixture] Each Test Method must have the attribute [Test] Other Attributes include

SetUp / Teardown ExpectedException ( typeof( Exception)) Explicit Ignore

Page 10: NUnit Presentation

Select the Solution explorer mode

Page 11: NUnit Presentation

Right click references and select Add reference

Page 12: NUnit Presentation

Select Browse

Page 13: NUnit Presentation

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

Page 14: NUnit Presentation

Now press ok in the Add reference page

Page 15: NUnit Presentation

The dll has been added to your reference list.

Page 16: NUnit Presentation

In your code, add using nunit.framework.

Page 17: NUnit Presentation

Add testfixture to your program

Page 18: NUnit Presentation

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 19: NUnit Presentation

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 20: NUnit Presentation

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 21: NUnit Presentation

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 22: NUnit Presentation

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 23: NUnit Presentation

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 24: NUnit Presentation

Location of exe files

Page 25: NUnit Presentation

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 26: NUnit Presentation

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 27: NUnit Presentation

GUI MODE

The steps to be followed areLoad the Dll file.Select Run.View the errors if any on the GUI screen.Save the output XML if necessary.

Page 28: NUnit Presentation

Select open in the NUNIT GUI

Page 29: NUnit Presentation

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

folder)

Page 30: NUnit Presentation

Runs Perfectly

Page 31: NUnit Presentation

Tests Fail

Page 32: NUnit Presentation

To save the output in an XML file

Page 33: NUnit Presentation

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 34: NUnit Presentation

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 35: NUnit Presentation

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 36: NUnit Presentation

Runs Perfectly

Page 37: NUnit Presentation

Test Fails

Page 38: NUnit Presentation

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 39: NUnit Presentation

Now lets Examine the Output file (test passed)

Page 40: NUnit Presentation

Test Failed

Page 41: NUnit Presentation

Failure message(1 message)

Page 42: NUnit Presentation

Test Failure(2 tests)

Page 43: NUnit Presentation

Proposed Architecture

The architecture which has been proposed by the architect isThe 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 44: NUnit Presentation

NUNIT

Thank you