Unit Testing Using N Unit

28
Unit Testing using Nunit Introduction to Art of Testing Presenter: Gaurav Arora Email: [email protected] Note: This presentation is presented with courtesy of HyderabadTechies.info

description

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

Transcript of Unit Testing Using N Unit

Page 1: Unit Testing Using N Unit

Unit Testing using NunitIntroduction to Art of Testing

Presenter:Gaurav Arora

Email: [email protected]: This presentation is presented with courtesy of HyderabadTechies.info

Page 2: Unit Testing Using N Unit

Topics

What is unit testing?

Step-by-step Overview

What is Nunit?

Hands-on using Nunit

Page 3: Unit Testing Using N Unit

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.]

Page 4: Unit Testing Using N Unit

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

Page 5: Unit Testing Using N Unit

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

Page 6: Unit Testing Using N Unit

What do you need?

1. Testing framework

NUnit, MSTest, MbUnit

2. Test runner

NUnit, MSTest, ReSharper

3. Mocking framework

Rhino Mocks, Moq, TypeMock

Page 7: Unit Testing Using N Unit

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

Page 8: Unit Testing Using N Unit

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/

Page 9: Unit Testing Using N Unit

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/

Page 10: Unit Testing Using N Unit

Pictorial Flow of Nunit

nUnit Dll, exe file XML file

(Optional)

Processing details on the GUI or Command prompt

Image : showing [Input & Output]

Page 11: Unit Testing Using N Unit

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.

Page 12: Unit Testing Using N Unit

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

Page 13: Unit Testing Using N Unit

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 …

Page 14: Unit Testing Using N Unit

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

Page 15: Unit Testing Using N Unit

Hands-on: BMI Test

Page 16: Unit Testing Using N Unit

Hands-on: BMI Test-Structure

Usin

gTest ClassTestFixture Attribute

Setup attribute

TearDown

attributeTest

attribute

Page 17: Unit Testing Using N Unit

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

Page 18: Unit Testing Using N Unit

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

Page 19: Unit Testing Using N Unit

Lets take a look on Code!

Page 20: Unit Testing Using N Unit

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]

Page 21: Unit Testing Using N Unit

Nunit : CUI - Output

Page 22: Unit Testing Using N Unit

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: /?)

Page 23: Unit Testing Using N Unit

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

Page 24: Unit Testing Using N Unit

Nunit : Load Test

Page 25: Unit Testing Using N Unit

Nunit : Test Result

Page 26: Unit Testing Using N Unit

Lets take a look on Nunit GUI

Page 27: Unit Testing Using N Unit

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

Page 28: Unit Testing Using N Unit

Thanks