N Unit Presentation


Published on

  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

N Unit Presentation

  1. 1. NUnit – A Unit Test Framework for .Net
  2. 2. Topics <ul><li>What is NUnit ? </li></ul><ul><li>Features </li></ul><ul><li>Writing a Unit Test </li></ul><ul><li>Code Examples </li></ul><ul><li>Pros and Cons </li></ul><ul><li>Conclusion </li></ul>
  3. 3. NUnit framework <ul><li>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 . </li></ul><ul><li>The NUnit framework is developed from ground up to make use of .NET framework functionalities. </li></ul><ul><li>NUnit is a unit-testing framework for all .Net languages. </li></ul><ul><li>It is written entirely in C#. </li></ul><ul><li>NUnit is much the same as all the Extreme Programming test frameworks (xUnits) </li></ul><ul><li>NUnit was initially ported from JUnit. </li></ul><ul><li>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. </li></ul><ul><li>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. </li></ul>
  4. 4. What NUnit Framework is not: <ul><li>It is not Automated GUI tester. </li></ul><ul><li>It is not a scripting language, all test are written in .NET supported language e.g. C#, VC, VB.NET, J# etc. </li></ul><ul><li>It is not a benchmark tool. </li></ul><ul><li>Passing the entire unit test suite does not mean software is production ready. </li></ul><ul><li>NUnit is simple to understand and easy to learn. </li></ul><ul><li>Object Oriented. </li></ul><ul><li>NUnit is flexible - Test code can be in any .Net language. </li></ul><ul><li>NUnit uses the &quot;attribute&quot; feature of .NET to identify tests. </li></ul>Features
  5. 5. Download & Installation <ul><li>http://www.nunit.org </li></ul><ul><li>http://nunit.org/download.html </li></ul><ul><li>http://sourceforge.net/projects/nunit/ </li></ul><ul><li>Download Nunit-2.2.0.msi from http:// nunit.org/download.html and install nunit. </li></ul><ul><li>Add nunit.frameworks.dll to your project references. This dll resides in nunitin folder. </li></ul><ul><li>The next few screen shots show how to add this dll file if your project has been opened as a classlibrary . </li></ul>
  6. 6. Simple concepts <ul><li>. Test fixture : a class that contains one ore more test methods </li></ul><ul><li>. Test method : a method that executes a specific test </li></ul><ul><li>. Test runner : an application that finds and executes test methods on test fixtures </li></ul><ul><li>. Assertion : a Boolean expression that describes what must be true when some </li></ul><ul><li>action has been executed </li></ul><ul><li>. Expected Exception : the type of an Exception we expect to be thrown during </li></ul><ul><li>execution of a test method </li></ul><ul><li>. Setup : Code that is run before every test method is executed (eg, logging in as a </li></ul><ul><li>particular user or initializing a singleton) </li></ul><ul><li>. Teardown : Code that is run after every test method has finished (eg, deleting rows </li></ul><ul><li>from a table that were inserted during the test) </li></ul>
  7. 7. <ul><li>Cons </li></ul><ul><li>C based extensions not available. </li></ul><ul><li>Lack of awareness among .Net Community. </li></ul><ul><li>Pros </li></ul><ul><li>NUnit is Open Source and highly extensible. </li></ul><ul><li>Platform independent. </li></ul><ul><li>Simple and easy to learn syntax. </li></ul><ul><li>When integrated with NAnt, can be used for incremental projects. </li></ul>
  8. 8. The Input & Output of nUnit NUnit Dll, exe file XML file (Optional) Processing details on the GUI or Command prompt
  9. 9. Writing Unit Test Code <ul><li>Every Class must have a corresponding Test Class named Test ClassName </li></ul><ul><li>One test method per public method, named test MethodName. </li></ul><ul><li>In each test method, test good results and failures </li></ul><ul><li>Every Test Class must have the attribute [TestFixture] </li></ul><ul><li>Each Test Method must have the attribute [Test] </li></ul><ul><li>Other Attributes include </li></ul><ul><ul><li>SetUp / Teardown </li></ul></ul><ul><ul><li>ExpectedException ( typeof( Exception)) </li></ul></ul><ul><ul><li>Explicit </li></ul></ul><ul><ul><li>Ignore </li></ul></ul>
  10. 10. Select the Solution explorer mode
  11. 11. Right click references and select Add reference
  12. 12. Select Browse
  13. 13. Goto the nunitin directory, choose nunit.framework.dll and press open.
  14. 14. Now press ok in the Add reference page
  15. 15. The dll has been added to your reference list.
  16. 16. In your code, add using nunit.framework.
  17. 17. Add testfixture to your program
  18. 18. TestFixture Example <ul><li>[TestFixture] </li></ul><ul><li>public class calcTest{ </li></ul><ul><li>[Test] </li></ul><ul><li>public void AdditionTests(){ </li></ul><ul><li>calculator cal=new calculator(); </li></ul><ul><li>cal.Addition(5,10); //CASE 1 </li></ul><ul><li>Assert.AreEqual(15,cal.output()); </li></ul><ul><li>cal.Addition(-5,10); // CASE 2 </li></ul><ul><li>Assert.AreEqual(15,cal.output()); </li></ul><ul><li>} </li></ul><ul><li>[Test] </li></ul><ul><li>public void DivisionTests(){ </li></ul><ul><li>calculator cal=new calculator(); </li></ul><ul><li>cal.Division(10,2); // CASE 1 </li></ul><ul><li>Assert.AreEqual(5,cal.output()); </li></ul><ul><li>cal.Division(10,0); //CASE 2 </li></ul><ul><li>Assert.AreEqual(2,cal.output()); </li></ul><ul><li>} </li></ul><ul><li>}//for testfixture </li></ul>
  19. 19. How to write your test code <ul><li>Suppose you want to test the functions in a class, your testfixture(test stub) should look somewhat like this </li></ul><ul><li>using NUnit.Framework; </li></ul><ul><li>Namespace sample1{ </li></ul><ul><li>public class sampleclass(){ </li></ul><ul><li>public void func1(){} </li></ul><ul><li>}//for sampleclass </li></ul><ul><li>[TestFixture] </li></ul><ul><li>public class sampleTest(){ </li></ul><ul><li>[Test] </li></ul><ul><li>public void test1(){ </li></ul><ul><li>testcase1; </li></ul><ul><li>testcase2; </li></ul><ul><li>}//for test1() </li></ul><ul><li>}//for sampleTest textfixture </li></ul><ul><li>}//for namespace </li></ul>
  20. 20. Calculator Example <ul><li>using System; </li></ul><ul><li>using NUnit.Framework; </li></ul><ul><li>namespace ClassLibrary1 </li></ul><ul><li>{ </li></ul><ul><li>public class calculator </li></ul><ul><li>{ </li></ul><ul><li>private int result; </li></ul><ul><li>public void Addition(int num1,int num2){ </li></ul><ul><li>result=num1+num2; </li></ul><ul><li>} </li></ul><ul><li>public int output(){ </li></ul><ul><li>return result; </li></ul><ul><li>} </li></ul><ul><li>public void Division(int num1,int num2){ </li></ul><ul><li>if(num2==0){ </li></ul><ul><li>throw new DivideByZeroException(); </li></ul><ul><li>} </li></ul><ul><li>result = num1/num2; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  21. 21. Calculator Example cont… <ul><li>[TestFixture] </li></ul><ul><li>public class calcTest </li></ul><ul><li>{ </li></ul><ul><li>[Test] </li></ul><ul><li>public void AdditionTests(){ </li></ul><ul><li>calculator cal=new calculator(); </li></ul><ul><li>cal.Addition(5,10);Console.Write(&quot;TESTING 5+10 &quot;); </li></ul><ul><li>Assert.AreEqual(15,cal.output()); </li></ul><ul><li>cal.Addition(-5,10);Console.Write(&quot;TESTING -5+10 &quot;); </li></ul><ul><li>Assert.AreEqual(5,cal.output()); </li></ul><ul><li>} </li></ul><ul><li>[Test] </li></ul><ul><li>public void DivisionTests(){ </li></ul><ul><li>calculator cal=new calculator(); </li></ul><ul><li>cal.Division(10,2); Console.Write(&quot;TESTING 10div2 &quot;); </li></ul><ul><li>Assert.AreEqual(5,cal.output()); </li></ul><ul><li>cal.Division(10,0); Console.Write(&quot;TESTING 10div0 &quot;); </li></ul><ul><li>Assert.AreEqual(0,cal.output()); </li></ul><ul><li>} </li></ul><ul><li>}//for testfixture </li></ul><ul><li>}//for namespace </li></ul>
  22. 22. Build the Project <ul><li>After the testfixture is complete, build the project. This will create a dll file which will reside in projectnameindebug folder. </li></ul>
  23. 23. LOCATION of dll <ul><li>In this example I am copying the dll file from projectnameindebug folder to nunitin folder for my convenience. </li></ul><ul><li>But this is not a requirement when we are testing and the dll can be run from projectnameindebug folder. </li></ul>
  24. 24. Location of exe files
  25. 25. Starting the GUI from console <ul><li>The GUI can be started from the console by executing </li></ul><ul><li>nunit-gui.exe [ inputfilename ][ options ] </li></ul><ul><li>Ex: nunit-gui.exe nunit.tests.dll –run </li></ul><ul><li>This option will load the dll file and run the test. </li></ul><ul><li>* Options with their values are separated by an equal, colon or a space. </li></ul>
  26. 26. Starting the GUI from console <ul><li>nunit-gui.exe [inputfilename][options] </li></ul><ul><li>Options: </li></ul><ul><li>/help short format: /? </li></ul><ul><li>/config=STR project config to load </li></ul><ul><li>/noload suppress loading of last proj </li></ul><ul><li>/run Automatically run after load </li></ul><ul><li>/fixture=STR Fixture to test </li></ul>
  27. 27. GUI MODE <ul><li>The steps to be followed are </li></ul><ul><ul><li>Load the Dll file. </li></ul></ul><ul><ul><li>Select Run. </li></ul></ul><ul><ul><li>View the errors if any on the GUI screen. </li></ul></ul><ul><ul><li>Save the output XML if necessary. </li></ul></ul>
  28. 28. Select open in the NUNIT GUI
  29. 29. Select the dll which you want to test(In this case, we have moved the dll to the nunitin folder)
  30. 30. Runs Perfectly
  31. 31. Tests Fail
  32. 32. To save the output in an XML file
  33. 33. Writing in the output file <ul><li>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. </li></ul><ul><li>But in Console mode , a XML file is created even if not specified by the user. </li></ul>
  34. 34. Console mode <ul><li>The command to execute the exe in console mode is </li></ul><ul><li>nunit-console.exe [filename] [option] </li></ul><ul><li>Example: </li></ul><ul><li>nunit-console.exe classlibrary1.dll -XML=ourfile.xml </li></ul>
  35. 35. The different options are <ul><li>Nunit-console [inputfiles][options] </li></ul><ul><li>Options </li></ul><ul><li>/Fixture=STR Fixture to test </li></ul><ul><li>/config=STR project configuration to load </li></ul><ul><li>/XML=STR Name of XML output file </li></ul><ul><li>/transform=STR name of transform file </li></ul><ul><li>/xmlConsole display XML to the console </li></ul><ul><li>/output=STR File to receive test output (short :/out=STR) </li></ul><ul><li>/err=STR File to receive test error output </li></ul><ul><li>/labels Label each test in stdout </li></ul><ul><li>/include = STR list of categories to include </li></ul><ul><li>/exclude = STR list of categories to exclude </li></ul><ul><li>/noshadow disable shadow option </li></ul><ul><li>/thread runs thread on a separate thread </li></ul><ul><li>/wait wait for input before closing console window </li></ul><ul><li>/nologo Do not display the logo </li></ul><ul><li>/help Display help (short format: /?) </li></ul>
  36. 36. Runs Perfectly
  37. 37. Test Fails
  38. 38. Output file <ul><li>As mentioned earlier, in console mode the xml file is automatically created as testresult.xml in the nunitin folder. </li></ul><ul><li>The user can change the name of the output file by saying -XML = newfilename.xml as shown in the previous slide. </li></ul>
  39. 39. Now lets Examine the Output file (test passed)
  40. 40. Test Failed
  41. 41. Failure message(1 message)
  42. 42. Test Failure(2 tests)
  43. 43. Proposed Architecture <ul><li>The architecture which has been proposed by the architect is </li></ul><ul><ul><li>The user shall run his tests in the console mode of the nUnit tool after the dll has been created by the nAnt tool. </li></ul></ul><ul><ul><li>The output XML file shall be sent to the User Interface to accommodate information about the user, date, time and other related information. </li></ul></ul>
  44. 44. NUNIT <ul><li>Thank you </li></ul>