Date post: | 13-May-2015 |
Category: |
Technology |
Upload: | gaurav-arora |
View: | 2,750 times |
Download: | 1 times |
Unit Testing using NunitIntroduction to Art of Testing
Presenter:Gaurav Arora
Email: [email protected]: This presentation is presented with courtesy of HyderabadTechies.info
Topics
What is unit testing?
Step-by-step Overview
What 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
customer
Main benefits of unit testing
Main 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
requirements
What do you need?
1. Testing framework
NUnit, MSTest, MbUnit
2. Test runner
NUnit, MSTest, ReSharper
3. Mocking framework
Rhino Mocks, Moq, TypeMock
Why 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 Nunit
nUnit Dll, exe file XML file
(Optional)
Processing details on the GUI or Command prompt
Image : showing [Input & Output]
Nunit : two operational flavors
Nunit 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-on
Before 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
mode
Nunit : 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 Test
• Start 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 project
Hands-on: BMI Test
Hands-on: BMI Test-Structure
Usin
gTest ClassTestFixture Attribute
Setup attribute
TearDown
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 exception
Hands-on : ‘AAA’ methodNunit ‘AAA’ method is nothing but
• Arrange :arrange objects, creating and setting them up as necessary
• Act :Act on an object
• AssertThat 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/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: /?)
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
Nunit : Load Test
Nunit : Test Result
Lets take a look on Nunit GUI
References
• “Art of Unit Testing” - Roy Osherove• “Test-Driven Development in Microsoft .NET” – James
Newkirk• “Pragmatic Unit Testing in C# with NUnit” – Andy Hunt, Dave
Thomas
Thanks