Unit Testing using NunitIntroduction to Art of TestingPresenter:GauravAroraEmail: admin@hyderabadtechies.infoNote: This presentation is presented with courtesy of HyderabadTechies.info
TopicsWhat is unit testing?Step-by-step OverviewWhat is Nunit?Hands-on using Nunit
What is unit testing?Unit Testing is a way/method by which individual units of source code/application are tested to determine if they are fit for use.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.]
Who is responsible for testing?Primarily job for Unit Testing is completely for Developers. But, when we talk about Quality or testing then it would be a different answer.Everyone who is a developer
Everyone who is a team member
Even it’s the responsibility of customerMain benefits of unit testingMain goal of unit testing to make sure that each and every tested part [i.e. unit] is correct. In Test-Drive-application -Helpful in writing Smaller, tighter, decoupled code
Helpful in Documentation of requirementsWhat do you need?Testing frameworkNUnit, MSTest, MbUnitTest runnerNUnit, MSTest, ReSharperMocking frameworkRhino Mocks, Moq, TypeMockWhy to choose NUnit?A generally asked question, there is a lengthy discussion and story for this, due to shortage of time, we can’t go with topic.Here are some links, go and grab the answer of this question :http://osherove.com/blog/2010/3/5/nunit-vs-mstest-nunit-wins-for-unit-testing.htmlhttp://stackoverflow.com/questions/3678783/mbunit-vs-nunithttp://stackoverflow.com/questions/261139/nunit-vs-mbunit-vs-mstest-vs-xunit-net
What is Nunit?Nunit is unit-testing framework and supports all .Net languages. It has been written entirely in C# [csharp].Refer - http://www.nunit.org/
Start with NunitBefore start, we need nUnit framework:Download from :http://www.nunit.org/index.php?p=downloadhttp://sourceforge.net/projects/nunit/[after download, install per your choice, not going in details of this chapter here…]For more info :http://www.nunit.org/
Pictorial Flow of NunitnUnitDll, exe file XML file (Optional)Processing details on the GUI or Command promptImage : showing [Input & Output]
Nunit : two operational flavorsNunit is having two operational modes:GUI modeGraphical user interface, nothing to type just click button and check the results.CUI [i.e. console] modeConsole mode, runs from command prompt, you need to write commands and check the results.
Nunit : Hands-onBefore this make sure, nunit framework has been downloaded has properly installed at your system.Preferable, download Nunit-2.5.10.msi
Add references to your project
Write Test methods
Run your tests either with GUI or CUI modeNunit : Hands-on… continue…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].Coming slides will tell step-by-step creation of project …
Nunit : Hands-on… BMI TestStart your IDE to develop .Net application. In this session we are using VS2010
Select a Class Library project [why choose class library, we are creating a separate project for our tests]
Add reference of nunit.framework.dll in your project
Include ‘NUnit.Framework’ namespace in your projectHands-on: BMI Test
Hands-on: BMI Test-StructureUsingTestFixture AttributeTest ClassSetup attributeTearDown attributeTest attribute
Hands-on : Points of interestIn previous slide we notified :Test class is named with *Tests
Method names are long, defining scenarios
Attribute TestFixture tells the fixture of test class
Method with Setup attribute calls at very first and TearDown calls after every TestMethod, no matters if there will be an exceptionHands-on : ‘AAA’ methodNunit ‘AAA’ method is nothing butArrange :arrange objects, creating and setting them up as necessaryAct :Act on an objectAssertThat something is expected
Lets take a look on Code!
Nunit : CUIConsole mode is one of the modes available of Nunit, lets discuss how to run from command [console window]:The command to execute the exe in console mode is	nunit-console.exe [filename] [option]
Nunit : CUI - Output
Nunit : CUI - 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/xmlConsoledisplay 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 waitfor input before closing console window /nologoDo not display the logo/help Display help (short format: /?)

Unit Testing Using N Unit

  • 1.
    Unit Testing usingNunitIntroduction to Art of TestingPresenter:GauravAroraEmail: admin@hyderabadtechies.infoNote: This presentation is presented with courtesy of HyderabadTechies.info
  • 2.
    TopicsWhat is unittesting?Step-by-step OverviewWhat is Nunit?Hands-on using Nunit
  • 3.
    What is unittesting?Unit Testing is a way/method by which individual units of source code/application are tested to determine if they are fit for use.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.]
  • 4.
    Who is responsiblefor testing?Primarily job for Unit Testing is completely for Developers. But, when we talk about Quality or testing then it would be a different answer.Everyone who is a developer
  • 5.
    Everyone who isa team member
  • 6.
    Even it’s theresponsibility of customerMain benefits of unit testingMain goal of unit testing to make sure that each and every tested part [i.e. unit] is correct. In Test-Drive-application -Helpful in writing Smaller, tighter, decoupled code
  • 7.
    Helpful in Documentationof requirementsWhat do you need?Testing frameworkNUnit, MSTest, MbUnitTest runnerNUnit, MSTest, ReSharperMocking frameworkRhino Mocks, Moq, TypeMockWhy to choose NUnit?A generally asked question, there is a lengthy discussion and story for this, due to shortage of time, we can’t go with topic.Here are some links, go and grab the answer of this question :http://osherove.com/blog/2010/3/5/nunit-vs-mstest-nunit-wins-for-unit-testing.htmlhttp://stackoverflow.com/questions/3678783/mbunit-vs-nunithttp://stackoverflow.com/questions/261139/nunit-vs-mbunit-vs-mstest-vs-xunit-net
  • 8.
    What is Nunit?Nunitis unit-testing framework and supports all .Net languages. It has been written entirely in C# [csharp].Refer - http://www.nunit.org/
  • 9.
    Start with NunitBeforestart, we need nUnit framework:Download from :http://www.nunit.org/index.php?p=downloadhttp://sourceforge.net/projects/nunit/[after download, install per your choice, not going in details of this chapter here…]For more info :http://www.nunit.org/
  • 10.
    Pictorial Flow ofNunitnUnitDll, exe file XML file (Optional)Processing details on the GUI or Command promptImage : showing [Input & Output]
  • 11.
    Nunit : twooperational flavorsNunit is having two operational modes:GUI modeGraphical user interface, nothing to type just click button and check the results.CUI [i.e. console] modeConsole mode, runs from command prompt, you need to write commands and check the results.
  • 12.
    Nunit : Hands-onBeforethis make sure, nunit framework has been downloaded has properly installed at your system.Preferable, download Nunit-2.5.10.msi
  • 13.
    Add references toyour project
  • 14.
  • 15.
    Run your testseither with GUI or CUI modeNunit : Hands-on… continue…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].Coming slides will tell step-by-step creation of project …
  • 16.
    Nunit : Hands-on…BMI TestStart your IDE to develop .Net application. In this session we are using VS2010
  • 17.
    Select a ClassLibrary project [why choose class library, we are creating a separate project for our tests]
  • 18.
    Add reference ofnunit.framework.dll in your project
  • 19.
    Include ‘NUnit.Framework’ namespacein your projectHands-on: BMI Test
  • 20.
    Hands-on: BMI Test-StructureUsingTestFixtureAttributeTest ClassSetup attributeTearDown attributeTest attribute
  • 21.
    Hands-on : Pointsof interestIn previous slide we notified :Test class is named with *Tests
  • 22.
    Method names arelong, defining scenarios
  • 23.
    Attribute TestFixture tellsthe fixture of test class
  • 24.
    Method with Setupattribute calls at very first and TearDown calls after every TestMethod, no matters if there will be an exceptionHands-on : ‘AAA’ methodNunit ‘AAA’ method is nothing butArrange :arrange objects, creating and setting them up as necessaryAct :Act on an objectAssertThat something is expected
  • 25.
    Lets take alook on Code!
  • 26.
    Nunit : CUIConsolemode is one of the modes available of Nunit, lets discuss how to run from command [console window]:The command to execute the exe in console mode is nunit-console.exe [filename] [option]
  • 27.
    Nunit : CUI- Output
  • 28.
    Nunit : CUI- 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/xmlConsoledisplay 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 waitfor input before closing console window /nologoDo not display the logo/help Display help (short format: /?)
  • 29.
    Nunit : GUI Go to your installed path of nunit framework and from bin folder select ‘nunit.exe’ If you installed nunit from msi package just go to program and select Nunit
  • 30.
  • 31.
  • 32.
    Lets take alook on Nunit GUI
  • 33.
    References“Art of UnitTesting” - Roy Osherove
  • 34.
    “Test-Driven Development inMicrosoft .NET” – James Newkirk
  • 35.
    “Pragmatic Unit Testingin C# with NUnit” – Andy Hunt, Dave ThomasThanks