Unit Testing Using N Unit


Published on

Presentation is describing very basic overview for Unit Testing using Nunit. This is the online session slide held in HyderbadTechies.info

Published in: Technology, Education
  • Be the first to comment

  • Be the first to like this

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

No notes for slide

Unit Testing Using N Unit

  1. 1. Unit Testing using NunitIntroduction to Art of Testing<br />Presenter:<br />GauravArora<br />Email: admin@hyderabadtechies.info<br />Note: This presentation is presented with courtesy of HyderabadTechies.info<br />
  2. 2. Topics<br />What is unit testing?<br />Step-by-step Overview<br />What is Nunit?<br />Hands-on using Nunit<br />
  3. 3. What is unit testing?<br />Unit Testing is a way/method by which individual units of source code/application are tested to determine if they are fit for use.<br />In other words, we can say method by which individual test one thing at a time [i.e. units] without touching external sources [DB, external files, libraries etc.]<br />
  4. 4. Who is responsible for testing?<br />Primarily job for Unit Testing is completely for Developers. But, when we talk about Quality or testing then it would be a different answer.<br /><ul><li>Everyone who is a developer
  5. 5. Everyone who is a team member
  6. 6. Even it’s the responsibility of customer</li></li></ul><li>Main benefits of unit testing<br />Main goal of unit testing to make sure that each and every tested part [i.e. unit] is correct. In Test-Drive-application -<br /><ul><li>Helpful in writing Smaller, tighter, decoupled code
  7. 7. Helpful in Documentation of requirements</li></li></ul><li>What do you need?<br />Testing framework<br /><ul><li>NUnit, MSTest, MbUnit</li></ul>Test runner<br /><ul><li>NUnit, MSTest, ReSharper</li></ul>Mocking framework<br /><ul><li>Rhino Mocks, Moq, TypeMock</li></li></ul><li>Why to choose NUnit?<br />A generally asked question, there is a lengthy discussion and story for this, due to shortage of time, we can’t go with topic.<br />Here are some links, go and grab the answer of this question :<br />http://osherove.com/blog/2010/3/5/nunit-vs-mstest-nunit-wins-for-unit-testing.html<br />http://stackoverflow.com/questions/3678783/mbunit-vs-nunit<br />http://stackoverflow.com/questions/261139/nunit-vs-mbunit-vs-mstest-vs-xunit-net<br />
  8. 8. What is Nunit?<br />Nunit is unit-testing framework and supports all .Net languages. It has been written entirely in C# [csharp].<br />Refer - http://www.nunit.org/<br />
  9. 9. Start with Nunit<br />Before start, we need nUnit framework:<br />Download from :<br />http://www.nunit.org/index.php?p=download<br />http://sourceforge.net/projects/nunit/<br />[after download, install per your choice, not going in details of this chapter here…]<br />For more info :<br />http://www.nunit.org/<br />
  10. 10. Pictorial Flow of Nunit<br />nUnit<br />Dll, exe file <br />XML file (Optional)<br />Processing details on the GUI or Command prompt<br />Image : showing [Input & Output]<br />
  11. 11. Nunit : two operational flavors<br />Nunit is having two operational modes:<br /><ul><li>GUI mode</li></ul>Graphical user interface, nothing to type just click button and check the results.<br /><ul><li>CUI [i.e. console] mode</li></ul>Console mode, runs from command prompt, you need to write commands and check the results.<br />
  12. 12. Nunit : Hands-on<br />Before this make sure, nunit framework has been downloaded has properly installed at your system.<br /><ul><li>Preferable, download Nunit-2.5.10.msi
  13. 13. Add references to your project
  14. 14. Write Test methods
  15. 15. Run your tests either with GUI or CUI mode</li></li></ul><li>Nunit : Hands-on… continue…<br />In our Test project we are going to write simple test, which will test our methods for BMI application [a simple Body Mass Index calculator].<br />Coming slides will tell step-by-step creation of project …<br />
  16. 16. Nunit : Hands-on… BMI Test<br /><ul><li>Start your IDE to develop .Net application. In this session we are using VS2010
  17. 17. Select a Class Library project [why choose class library, we are creating a separate project for our tests]
  18. 18. Add reference of nunit.framework.dll in your project
  19. 19. Include ‘NUnit.Framework’ namespace in your project</li></li></ul><li>Hands-on: BMI Test<br />
  20. 20. Hands-on: BMI Test-Structure<br />Using<br />TestFixture Attribute<br />Test Class<br />Setup attribute<br />TearDown attribute<br />Test attribute<br />
  21. 21. Hands-on : Points of interest<br />In previous slide we notified :<br /><ul><li>Test class is named with *Tests
  22. 22. Method names are long, defining scenarios
  23. 23. Attribute TestFixture tells the fixture of test class
  24. 24. Method with Setup attribute calls at very first and TearDown calls after every TestMethod, no matters if there will be an exception</li></li></ul><li>Hands-on : ‘AAA’ method<br />Nunit ‘AAA’ method is nothing but<br /><ul><li>Arrange :</li></ul>arrange objects, creating and setting them up as necessary<br /><ul><li>Act :</li></ul>Act on an object<br /><ul><li>Assert</li></ul>That something is expected<br />
  25. 25. Lets take a look on Code!<br />
  26. 26. Nunit : CUI<br />Console mode is one of the modes available of Nunit, lets discuss how to run from command [console window]:<br />The command to execute the exe in console mode is <br /> nunit-console.exe [filename] [option]<br />
  27. 27. Nunit : CUI - Output<br />
  28. 28. Nunit : CUI - Options<br />*Options<br />/Fixture=STR Fixture to test<br />/config=STR project configuration to load<br />/XML=STR Name of XML output file<br />/transform=STR name of transform file<br />/xmlConsoledisplay XML to the console<br />/output=STR File to receive test output (short :/out=STR) <br />/err=STR File to receive test error output<br />/labels Label each test in stdout<br />/include = STR list of categories to include<br />/exclude = STR list of categories to exclude<br />/noshadow disable shadow option<br />/thread runs thread on a separate thread<br />/wait waitfor input before closing console window <br />/nologoDo not display the logo<br />/help Display help (short format: /?) <br />
  29. 29. Nunit : GUI <br /> Go to your installed path of nunit framework and from bin folder select ‘nunit.exe’<br /> If you installed nunit from msi package just go to program and select Nunit<br />
  30. 30. Nunit : Load Test <br />
  31. 31. Nunit : Test Result<br />
  32. 32. Lets take a look on Nunit GUI<br />
  33. 33. References<br /><ul><li>“Art of Unit Testing” - Roy Osherove
  34. 34. “Test-Driven Development in Microsoft .NET” – James Newkirk
  35. 35. “Pragmatic Unit Testing in C# with NUnit” – Andy Hunt, Dave Thomas</li></li></ul><li>Thanks<br />