SlideShare a Scribd company logo
1 of 28
Unit Testing
Abhinav Gujjar

@abhinavgujjar
RULES!

Mobiles on Silent mode
Ask
Ask
Ask again
Agenda
What is Unit Testing
Why unit test ?
Unit Testing Frameworks for .Net
Best Practices for unit testing
Test Driven Development (TDD)
Unit testing in Agile
Urgh – Not my job
I’m a Developer.
I don’t test.

I pay testers for
testing
In The Real World
In The Real World
Different Types of Testing
Unit Testing

Development

Functional
Testing

Testing

Acceptance
Testing

Deployment

Integration
Testing
DEMO

Simple Interest Calculation
What?

You are already doing it
… But not correctly

Prove the correctness of a small isolated piece of
functionality in a larger system
Why?

Higher Quality
Better Code
Documentation
How?

Unit Testing Frameworks

.Net
nUnit
xUnit
msTest
Demo

Creating unit tests
Anatomy
Arrange
Unit Test
Framework
Unit Tests

Act

Assert
Red-Green Refactor Cycle

Red

Refactor

Green
One Assert per test

One logical assertion per test
May need more than one Assert statement
Qualities of a Unit Test

Repeatable
Independent
Exercise

Writing Tests
Testing the Sad Path
Assertions !

Simple Assertions
Collection Asserts
String Asserts
Test Setup and Tear Downs

Assembly
Class
Test
Isolating Code
Isolation Techniques
Dummies
Stubs
Fake – Maintain State
Spy – Maintains state and is available for assertion

Mock – Complex behavior
Mocking Frameworks

Moq
RhinoMocks
nMock
Silverlight Unit Testing

Tests are run on the browser
Moq available! Yay !
But - Only if you use the MVVM pattern
View Model First Approach
Refactoring
When is done done ?
Code may be correct
But is it healthy?

 Readability
 Maintainability
 What about performance ?
 Technical Debt
Code Smells
 Duplicated Code
 Long Method
 Long Parameter List

 Large Class
 Comments !!
 Switch statements

 Dead Code
…
…
Testing Legacy Code

Why?
When?
How?
Happy Unit Testing!

Blog: www.rubiksquare.com
Discussion: http://www.rubiksquare.com/p/discuss.html
LinkedIn : http://www.linkedin.com/in/abhinavgujjar
mail: Abhinavgujjar@gmail.com

More Related Content

What's hot

Unit Testing Guidelines
Unit Testing GuidelinesUnit Testing Guidelines
Unit Testing Guidelines
Joel Hooks
 
Exploratory Testing in an Agile Context
Exploratory Testing in an Agile ContextExploratory Testing in an Agile Context
Exploratory Testing in an Agile Context
Elisabeth Hendrickson
 

What's hot (20)

Writing Better Tests - Applying Clean-Code TDD at 99designs
Writing Better Tests - Applying Clean-Code TDD at 99designsWriting Better Tests - Applying Clean-Code TDD at 99designs
Writing Better Tests - Applying Clean-Code TDD at 99designs
 
Unit testing
Unit testing Unit testing
Unit testing
 
Unit Testing Guidelines
Unit Testing GuidelinesUnit Testing Guidelines
Unit Testing Guidelines
 
Open source bridge testing antipatterns presentation
Open source bridge testing antipatterns presentationOpen source bridge testing antipatterns presentation
Open source bridge testing antipatterns presentation
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
 
Exploratory Testing in an Agile Context
Exploratory Testing in an Agile ContextExploratory Testing in an Agile Context
Exploratory Testing in an Agile Context
 
Clean and Quality Unit Tests
Clean and Quality Unit TestsClean and Quality Unit Tests
Clean and Quality Unit Tests
 
Unit testing - An introduction
Unit testing - An introductionUnit testing - An introduction
Unit testing - An introduction
 
Exploratory Testing in Practice
Exploratory Testing in PracticeExploratory Testing in Practice
Exploratory Testing in Practice
 
SCJP Preparation Tips
SCJP Preparation TipsSCJP Preparation Tips
SCJP Preparation Tips
 
Unit Testing in Action - C#, NUnit, and Moq
Unit Testing in Action - C#, NUnit, and MoqUnit Testing in Action - C#, NUnit, and Moq
Unit Testing in Action - C#, NUnit, and Moq
 
An insight to test driven development and unit testing
An insight to test driven development and unit testingAn insight to test driven development and unit testing
An insight to test driven development and unit testing
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Pertanyaan dan jawaban (graham et.al 2011) part 3
Pertanyaan dan jawaban (graham et.al 2011) part 3Pertanyaan dan jawaban (graham et.al 2011) part 3
Pertanyaan dan jawaban (graham et.al 2011) part 3
 
What is What is Professional Testing?
What is What is Professional Testing?What is What is Professional Testing?
What is What is Professional Testing?
 
Do we really need game testers?
Do we really need game testers?Do we really need game testers?
Do we really need game testers?
 
Assertj
AssertjAssertj
Assertj
 
Unit Testing, TDD and the Walking Skeleton
Unit Testing, TDD and the Walking SkeletonUnit Testing, TDD and the Walking Skeleton
Unit Testing, TDD and the Walking Skeleton
 
Getting started with Test Driven Development
Getting started with Test Driven DevelopmentGetting started with Test Driven Development
Getting started with Test Driven Development
 

Viewers also liked

презентация сёть1
презентация сёть1презентация сёть1
презентация сёть1
Kseniya Skryabina
 
Working smarter
Working smarterWorking smarter
Working smarter
smithjmk3
 

Viewers also liked (11)

Wzory
WzoryWzory
Wzory
 
презентация сёть1
презентация сёть1презентация сёть1
презентация сёть1
 
Uday_P_Singh
Uday_P_SinghUday_P_Singh
Uday_P_Singh
 
AHCapabilities - Quebec, Canada (Liquids)
AHCapabilities - Quebec, Canada (Liquids)AHCapabilities - Quebec, Canada (Liquids)
AHCapabilities - Quebec, Canada (Liquids)
 
Working smarter
Working smarterWorking smarter
Working smarter
 
Alcrea+SourceOne Present VESIsorb ExpoWest2013
Alcrea+SourceOne Present VESIsorb ExpoWest2013Alcrea+SourceOne Present VESIsorb ExpoWest2013
Alcrea+SourceOne Present VESIsorb ExpoWest2013
 
Tarotmag n°19
Tarotmag n°19Tarotmag n°19
Tarotmag n°19
 
[FW Invest Janvier 2017] Près de 200 millions d’euros levés par des entrepris...
[FW Invest Janvier 2017] Près de 200 millions d’euros levés par des entrepris...[FW Invest Janvier 2017] Près de 200 millions d’euros levés par des entrepris...
[FW Invest Janvier 2017] Près de 200 millions d’euros levés par des entrepris...
 
Rapport d'orientation 2016
Rapport d'orientation 2016Rapport d'orientation 2016
Rapport d'orientation 2016
 
rapportDigital-TV
rapportDigital-TVrapportDigital-TV
rapportDigital-TV
 
ref105
ref105ref105
ref105
 

Similar to Unit testing

Automated Unit Testing for Mere Mortals
Automated Unit Testing for Mere MortalsAutomated Unit Testing for Mere Mortals
Automated Unit Testing for Mere Mortals
Jess Chadwick
 

Similar to Unit testing (20)

Automated Unit Testing for Mere Mortals
Automated Unit Testing for Mere MortalsAutomated Unit Testing for Mere Mortals
Automated Unit Testing for Mere Mortals
 
Unit testing, principles
Unit testing, principlesUnit testing, principles
Unit testing, principles
 
JavaScript Unit Testing
JavaScript Unit TestingJavaScript Unit Testing
JavaScript Unit Testing
 
Real Life Unit Testing
Real Life Unit TestingReal Life Unit Testing
Real Life Unit Testing
 
Why Unit Testingl
Why Unit TestinglWhy Unit Testingl
Why Unit Testingl
 
Why unit testingl
Why unit testinglWhy unit testingl
Why unit testingl
 
Why Unit Testingl
Why Unit TestinglWhy Unit Testingl
Why Unit Testingl
 
Unit testing
Unit testingUnit testing
Unit testing
 
Insoft training day_1
Insoft training day_1Insoft training day_1
Insoft training day_1
 
Agile Testing, Uncertainty, Risk, and Why It All Works
Agile Testing, Uncertainty, Risk, and Why It All WorksAgile Testing, Uncertainty, Risk, and Why It All Works
Agile Testing, Uncertainty, Risk, and Why It All Works
 
Agile Testing Overview
Agile Testing OverviewAgile Testing Overview
Agile Testing Overview
 
Project Management in 3 Slides
Project Management in 3 SlidesProject Management in 3 Slides
Project Management in 3 Slides
 
STOP! You're Testing Too Much - Shawn Wallace
STOP!  You're Testing Too Much - Shawn WallaceSTOP!  You're Testing Too Much - Shawn Wallace
STOP! You're Testing Too Much - Shawn Wallace
 
Stop! you're testing too much
Stop!  you're testing too muchStop!  you're testing too much
Stop! you're testing too much
 
Introduction of unit test to management
Introduction of unit test to managementIntroduction of unit test to management
Introduction of unit test to management
 
Testing in java
Testing in javaTesting in java
Testing in java
 
Tdd Ugialtnet Jan2010
Tdd Ugialtnet Jan2010Tdd Ugialtnet Jan2010
Tdd Ugialtnet Jan2010
 
Unit & integration testing
Unit & integration testingUnit & integration testing
Unit & integration testing
 
Agile test practices
Agile test practicesAgile test practices
Agile test practices
 
An Introduction to Unit Testing
An Introduction to Unit TestingAn Introduction to Unit Testing
An Introduction to Unit Testing
 

Recently uploaded

Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Recently uploaded (20)

Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 

Unit testing

Editor's Notes

  1. IntroductionsAbhinav Gujjar from
  2. Only bad questions are the ones that were not asked
  3. Unit Testing frameworksnUnitxUnitmsTestBest PractisesIsolation Refactoring
  4. The job is to deliver a software product, not focus on roles.Especially in Agile developmentCosts 500% more to fix bugs in production than it does before leaving the developers code“Inspection at the point of creation”
  5. Functional testing is done by the QA teamThey are trying to cover user related functionalities (Similar to switching on the light)They may be looking at negative test scenarios. But they can never replicate all the circumstancesAcceptance testing is usually done by the clients.Most of the time is spent in developmentThe quicker the bugs are caught, the cheaper it is to fix themSometimes the testing phase is already too late for bugsFunctional testing
  6. How will you send the input ? Change the visual studio startup optionsGo to command line and pass in the parametersHow will you check the output ?Debug modeOR console.writelineWhat about multiple tests ?All the test scenario information is lost and thus not repeatable
  7. Unit testing frameworks are available for almost every popular language.nUnit – ported from javas junit. GrandfatherxUnit – Newcomer. Different implementation. Open SourcemsTest – integrated into visual studioMost of these will have a command line runner.Demonstrate with diagram how this might work
  8. Should be a separate project – to ensure that test are not deployed Also – more importantly to ensure that we are testing only the public interfaces of the targetTest Runner – will pass or failCan debug into the testsOne test per project, or a single test project for Always require the reference to a domain project + reference to the unit testing framework
  9. Separate test project because you may not want to ship the testing code along with
  10. Keep things simpleOnly add functionality that is required to make the test passCreating a failing testMake the test passRefactor – Improve the internal implementationRefactoring is made a lot more reliable. You will find a way to write code in a manner that makes it testable
  11. Repeatable – for instance based on a time factorRun them in any order
  12. Catching exceptions
  13. Dummies are objects that the SUT won’t be using them in a meaningful wayStubs are empty classes or objectsFakes may contain a little more complex implementationSpies