SlideShare a Scribd company logo
NUnit – A Unit Test Framework for .Net
Topics What is NUnit  ? Features  Writing a Unit Test Code Examples Pros and Cons Conclusion
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.
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
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 .
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)
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.
The Input & Output of nUnit NUnit Dll, exe file  XML file (Optional) Processing details on the GUI or Command prompt
Writing Unit Test Code Every Class must have a corresponding Test Class named Test ClassName One test method per public method, named test MethodName. 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
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.
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.
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
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.
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)
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  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: /?)
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)
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.
NUNIT Thank you

More Related Content

PPTX
Unit Testing Using N Unit
PDF
An Introduction to Unit Test Using NUnit
PPTX
Unit testing with NUnit
PPT
Selenium Automation Framework
PPTX
Katalon Studio - GUI Overview
PPTX
Hybrid automation framework
PPTX
How to select the right automated testing tool
Unit Testing Using N Unit
An Introduction to Unit Test Using NUnit
Unit testing with NUnit
Selenium Automation Framework
Katalon Studio - GUI Overview
Hybrid automation framework
How to select the right automated testing tool

What's hot (20)

PDF
Selenium with Cucumber
PDF
Behavior Driven Development and Automation Testing Using Cucumber
PDF
An introduction to unit testing
PPT
Test Automation Framework Designs
PPTX
Unit tests & TDD
PDF
e2e testing with cypress
PPT
Test automation using selenium
PPT
Selenium
PDF
JUnit & Mockito, first steps
PPTX
Unit Testing And Mocking
PDF
Page Object Model and Implementation in Selenium
PPTX
Automation testing & Unit testing
PPTX
Automation - web testing with selenium
PPTX
Automation Testing by Selenium Web Driver
PPTX
Selenium-Locators
PPTX
TestNG Framework
PPTX
Test Automation and Selenium
PPTX
Automation Testing With Appium
PPT
Basic software-testing-concepts
PPTX
Software Testing - Part 1 (Techniques, Types, Levels, Methods, STLC, Bug Life...
Selenium with Cucumber
Behavior Driven Development and Automation Testing Using Cucumber
An introduction to unit testing
Test Automation Framework Designs
Unit tests & TDD
e2e testing with cypress
Test automation using selenium
Selenium
JUnit & Mockito, first steps
Unit Testing And Mocking
Page Object Model and Implementation in Selenium
Automation testing & Unit testing
Automation - web testing with selenium
Automation Testing by Selenium Web Driver
Selenium-Locators
TestNG Framework
Test Automation and Selenium
Automation Testing With Appium
Basic software-testing-concepts
Software Testing - Part 1 (Techniques, Types, Levels, Methods, STLC, Bug Life...
Ad

Viewers also liked (20)

PPTX
NUnit Features Presentation
PPT
Nunit
PDF
How and what to unit test
DOC
Day 1 writing
PPT
Design and Implementation of Speech Based Scientific Calculator
PDF
Nuget is easier than you think and you should be using it as both a consumer ...
PPTX
Using NuGet the way you should - TechDays NL 2014
PPTX
Using nu get the way you should svcc
DOCX
Basic calculator tutorial
PPTX
Developing NuGet
DOCX
Vhdl code and project report of arithmetic and logic unit
PPTX
Unit Testing (C#)
PPT
Introduction to the Web API
ODP
Easymock Tutorial
PPT
Task 1 preperation
PPTX
Types of pattern
PDF
Scientific calculator project in c language
PPT
Excellent rest using asp.net web api
PPTX
ASP.NET WEB API
PDF
Domain-Driven Design with ASP.NET MVC
NUnit Features Presentation
Nunit
How and what to unit test
Day 1 writing
Design and Implementation of Speech Based Scientific Calculator
Nuget is easier than you think and you should be using it as both a consumer ...
Using NuGet the way you should - TechDays NL 2014
Using nu get the way you should svcc
Basic calculator tutorial
Developing NuGet
Vhdl code and project report of arithmetic and logic unit
Unit Testing (C#)
Introduction to the Web API
Easymock Tutorial
Task 1 preperation
Types of pattern
Scientific calculator project in c language
Excellent rest using asp.net web api
ASP.NET WEB API
Domain-Driven Design with ASP.NET MVC
Ad

Similar to N Unit Presentation (20)

DOCX
Test Driven Development
PDF
Game Programming 06 - Automated Testing
PPTX
8-testing.pptx
PPT
Comparative Development Methodologies
PPS
JUnit Presentation
PPS
J unit presentation
PPT
Unit testing framework
PDF
Unit Testing in Flutter - From Workflow Essentials to Complex Scenarios
PDF
Modern Python Testing
DOC
Getting started with test complete 7
PPTX
Coded ui - lesson 4 - coded ui test
PPT
PPT
Testing Options in Java
PPTX
Apache Ant
PPTX
Apache Ant
PDF
PresentationqwertyuiopasdfghUnittest.pdf
PPT
Stopping the Rot - Putting Legacy C++ Under Test
DOCX
Project3build.xml Builds, tests, and runs the project .docx
PPTX
Junit4&testng presentation
PPT
FlexUnit 4 for contributors
Test Driven Development
Game Programming 06 - Automated Testing
8-testing.pptx
Comparative Development Methodologies
JUnit Presentation
J unit presentation
Unit testing framework
Unit Testing in Flutter - From Workflow Essentials to Complex Scenarios
Modern Python Testing
Getting started with test complete 7
Coded ui - lesson 4 - coded ui test
Testing Options in Java
Apache Ant
Apache Ant
PresentationqwertyuiopasdfghUnittest.pdf
Stopping the Rot - Putting Legacy C++ Under Test
Project3build.xml Builds, tests, and runs the project .docx
Junit4&testng presentation
FlexUnit 4 for contributors

More from priya_trivedi (7)

PPT
Success By Shiv Khera Samir Shah
PPT
Swot Analysis
PPS
Power Of Positive Attitude
PPS
Final Automation Testing
PPS
Why Unit Testingl
PPS
Why Unit Testingl
Success By Shiv Khera Samir Shah
Swot Analysis
Power Of Positive Attitude
Final Automation Testing
Why Unit Testingl
Why Unit Testingl

N Unit Presentation

  • 1. NUnit – A Unit Test Framework for .Net
  • 2. Topics What is NUnit ? Features Writing a Unit Test Code Examples Pros and Cons Conclusion
  • 3. 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.
  • 4. 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
  • 5. 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 .
  • 6. 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)
  • 7. 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.
  • 8. The Input & Output of nUnit NUnit Dll, exe file XML file (Optional) Processing details on the GUI or Command prompt
  • 9. Writing Unit Test Code Every Class must have a corresponding Test Class named Test ClassName One test method per public method, named test MethodName. 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
  • 10. Select the Solution explorer mode
  • 11. Right click references and select Add reference
  • 13. Goto the nunit\bin directory, choose nunit.framework.dll and press open.
  • 14. Now press ok in the Add reference page
  • 15. The dll has been added to your reference list.
  • 16. In your code, add using nunit.framework.
  • 17. Add testfixture to your program
  • 18. 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
  • 19. 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
  • 20. 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; } }
  • 21. 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
  • 22. 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.
  • 23. 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.
  • 25. 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.
  • 26. 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
  • 27. 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.
  • 28. Select open in the NUNIT GUI
  • 29. Select the dll which you want to test(In this case, we have moved the dll to the nunit\bin folder)
  • 32. To save the output in an XML file
  • 33. 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.
  • 34. 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
  • 35. The different options are Nunit-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: /?)
  • 38. 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.
  • 39. Now lets Examine the Output file (test passed)
  • 43. 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.