SlideShare a Scribd company logo
1 of 39
Unit Testing
Unit Testing
โ€ข What is automated testing?
โ€ข Is it a replacement for manual testing?
โ€ข Do I really need it ?
โ€ข How should I do it?
What is automated testing?
The practice of writing code to test our code, and then run those
tests in an automated fashion .
Unit Testing
Unit Testing
Automated tests are
repeatable
Benefits of Automated test
Why on earth should be write code to test
our code when we can just run the
application and see the result
Benefits of Automated test
โ€ข Test your code frequently , in less time
โ€ข Catch bugs before deploying
โ€ข Deploy with confidence
โ€ข Reduce the number of bugs that will go
into production
โ€ข Refactor code with confidence
Benefits of Automated test
โ€ข Refactoring means changing the structure
of the code with out changing its
behaviour
โ€ข When you donโ€™t have automated test
every time you refactor your code you
have to manually test every part of the
application
Unit test
Tests a unit of an application without its external dependencies
Such as files , databases ,web services ..
โ€ข Execute fast
โ€ข Donโ€™t give a lot of confidence about the application
Integration test
Test the application with its external dependencies
โ€ข Take long to execute (read / write in DB)
โ€ข Give us more confidence about the application
End to End test
Drives the application through its UI
There are a specific tools build for creating end to end test
Selenium
Allows us to record the interaction of the user with application
And check if the application return the right result or not ..
โ€ข Give us a great confidence
โ€ข Very slow
โ€ข Small change can easily break this test
At the end of the day
Use your own judgment to determine what
kind of test you need
Tooling
Nunit
earliest framework out there
MSTest
Microsoft testing framework built into visual studio
Xunit
gain more popularity over the past couple of years
Characteristics of Good
Unit Test
โ€ข Clean , readable and maintainable
โ€ข No Logic (if, loops,โ€ฆ.etc)
โ€ข Isolated
What to test and what not to
test
Unit testing and clean code go hand in hand
Naming and organising tests
Attributes
โ€ข Setup : call the method decorated with (SetUp attribute) before run each test
โ€ข TearDown : call the method decorated with (TearDown attribute) after run
each test, often use with ingeneration test
โ€ข TestCase : with this we can supply different argument for test method (this
attribute in NUnit only ) .
โ€ข Ignore : ignore this test method .. And show us the reason why we ignore
this method . Better than deleting the method ..
Parameterized Tests
โ€ข Instead 3 or for method we can have one method takes parameters
TrustWorthy
Test you can relay on .. If the test passes you know that your code is working
And if it fails you know that is something goes wrong with your test
How can we write TrustWorthy test?
There is to ways
โ€ข Test driven development ..TDD
โ€ข Code first
Test driven development
..TDD
Test Frist
with TDD you write your test before writing
production code
create a bug in your code if test pass that is
untrustworthy test
Unit Testing Techniques
โ€ข Testing Strings
โ€ข Testing Arrays and Collections
โ€ข Testing the Return Type of Methods
โ€ข Testing Void Methods
โ€ข Testing Methods that Throw Exceptions
โ€ข Testing Methods that Raise an Event
โ€ข Testing Private Methods
If your code is not clean writing unit test is hard or
impossible
Loosely-coupled and
Testable Code
Most legacy applications are built without
unit testing .. So in order to unit test them
You need to refactor them
For testable and loosely-couple Design
Loosely-coupled and
Testable Code
it's a description of a relationship between two
classes, where each class knows the very least
about the other and each class could potentially
continue to work just fine whether the other is
present or not and without dependency on the
particular implementation of the other class.
There are 3 steps we need to follow to
achieve a testable and loosely-coupled
design
Loosely-coupled and
Testable Code
โ€ข Extract the code that uses the external
resource into a separate class and isolate
it from a rest of you code
Next
Extract an interface from that class
Why an Interface ??
Because an interface like a contract .and
there is probably one or more classes that
implement this contract
Execute
โ€ข Refactoring Towards a Loosely-coupled Design
โ€ข Dependency Injection via Method Parameters , Properties , Constructor
โ€ข Creating Mock Objects Using Moq
An Introduction to Unit Testing
An Introduction to Unit Testing

More Related Content

What's hot

Type mock isolator
Type mock isolatorType mock isolator
Type mock isolator
MaslowB
ย 
#1 unit testing
#1 unit testing#1 unit testing
#1 unit testing
eleksdev
ย 

What's hot (18)

An Introduction to Unit Testing
An Introduction to Unit TestingAn Introduction to Unit Testing
An Introduction to Unit Testing
ย 
Unit testing - the hard parts
Unit testing - the hard partsUnit testing - the hard parts
Unit testing - the hard parts
ย 
Type mock isolator
Type mock isolatorType mock isolator
Type mock isolator
ย 
Unit testing
Unit testing Unit testing
Unit testing
ย 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
ย 
Case Study: Automated Code Reviews In A Grown SAP Application Landscape At EW...
Case Study: Automated Code Reviews In A Grown SAP Application Landscape At EW...Case Study: Automated Code Reviews In A Grown SAP Application Landscape At EW...
Case Study: Automated Code Reviews In A Grown SAP Application Landscape At EW...
ย 
Unit tests & TDD
Unit tests & TDDUnit tests & TDD
Unit tests & TDD
ย 
TDD in the ABAP world - sitNL 2013 edition
TDD in the ABAP world - sitNL 2013 editionTDD in the ABAP world - sitNL 2013 edition
TDD in the ABAP world - sitNL 2013 edition
ย 
RIA 05 - Unit Testing by Ajinkya Prabhune
RIA 05 - Unit Testing by Ajinkya PrabhuneRIA 05 - Unit Testing by Ajinkya Prabhune
RIA 05 - Unit Testing by Ajinkya Prabhune
ย 
Agile Mumbai 2020 Conference | How to get the best ROI on Your Test Automati...
Agile Mumbai 2020 Conference |  How to get the best ROI on Your Test Automati...Agile Mumbai 2020 Conference |  How to get the best ROI on Your Test Automati...
Agile Mumbai 2020 Conference | How to get the best ROI on Your Test Automati...
ย 
#1 unit testing
#1 unit testing#1 unit testing
#1 unit testing
ย 
Understanding Unit Testing
Understanding Unit TestingUnderstanding Unit Testing
Understanding Unit Testing
ย 
Tdd for php
Tdd for phpTdd for php
Tdd for php
ย 
Why Unit Testingl
Why Unit TestinglWhy Unit Testingl
Why Unit Testingl
ย 
Automated Testing for Web Applications - Wurbe #36
Automated Testing for Web Applications - Wurbe #36Automated Testing for Web Applications - Wurbe #36
Automated Testing for Web Applications - Wurbe #36
ย 
Nunit
NunitNunit
Nunit
ย 
ABAP Code Retreat Frankfurt 2016: TDD - Test Driven Development
ABAP Code Retreat Frankfurt 2016: TDD - Test Driven DevelopmentABAP Code Retreat Frankfurt 2016: TDD - Test Driven Development
ABAP Code Retreat Frankfurt 2016: TDD - Test Driven Development
ย 
Unit testing with NUnit
Unit testing with NUnitUnit testing with NUnit
Unit testing with NUnit
ย 

Similar to An Introduction to Unit Testing

Test-Driven Development
Test-Driven DevelopmentTest-Driven Development
Test-Driven Development
Meilan Ou
ย 
Becoming a better programmer - unit testing
Becoming a better programmer - unit testingBecoming a better programmer - unit testing
Becoming a better programmer - unit testing
Duy Tan Geek
ย 
Type mock isolator
Type mock isolatorType mock isolator
Type mock isolator
MaslowB
ย 

Similar to An Introduction to Unit Testing (20)

Week 14 Unit Testing.pptx
Week 14  Unit Testing.pptxWeek 14  Unit Testing.pptx
Week 14 Unit Testing.pptx
ย 
Test-Driven Development
Test-Driven DevelopmentTest-Driven Development
Test-Driven Development
ย 
Unit testing
Unit testingUnit testing
Unit testing
ย 
An Introduction to Unit Test Using NUnit
An Introduction to Unit Test Using NUnitAn Introduction to Unit Test Using NUnit
An Introduction to Unit Test Using NUnit
ย 
Unit testing
Unit testingUnit testing
Unit testing
ย 
Getting started with Test Driven Development
Getting started with Test Driven DevelopmentGetting started with Test Driven Development
Getting started with Test Driven Development
ย 
Unit Testing talk
Unit Testing talkUnit Testing talk
Unit Testing talk
ย 
Getting started with Test Driven Development - Ferdous Mahmud Shaon
Getting started with Test Driven Development - Ferdous Mahmud ShaonGetting started with Test Driven Development - Ferdous Mahmud Shaon
Getting started with Test Driven Development - Ferdous Mahmud Shaon
ย 
Becoming a better programmer - unit testing
Becoming a better programmer - unit testingBecoming a better programmer - unit testing
Becoming a better programmer - unit testing
ย 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
ย 
Test Driven Development - a Practitionerโ€™s Perspective
Test Driven Development - a Practitionerโ€™s PerspectiveTest Driven Development - a Practitionerโ€™s Perspective
Test Driven Development - a Practitionerโ€™s Perspective
ย 
Tdd
TddTdd
Tdd
ย 
Type mock isolator
Type mock isolatorType mock isolator
Type mock isolator
ย 
Automated testing overview
Automated testing overviewAutomated testing overview
Automated testing overview
ย 
Agile Acceptance testing with Fitnesse
Agile Acceptance testing with FitnesseAgile Acceptance testing with Fitnesse
Agile Acceptance testing with Fitnesse
ย 
Lecture 21
Lecture 21Lecture 21
Lecture 21
ย 
Why Unit Testingl
Why Unit TestinglWhy Unit Testingl
Why Unit Testingl
ย 
Why unit testingl
Why unit testinglWhy unit testingl
Why unit testingl
ย 
Agile Software Testing the Agilogy Way
Agile Software Testing the Agilogy WayAgile Software Testing the Agilogy Way
Agile Software Testing the Agilogy Way
ย 
Unit testing (Exploring the other side as a tester)
Unit testing (Exploring the other side as a tester)Unit testing (Exploring the other side as a tester)
Unit testing (Exploring the other side as a tester)
ย 

Recently uploaded

CALL ON โžฅ8923113531 ๐Ÿ”Call Girls Kakori Lucknow best sexual service Online โ˜‚๏ธ
CALL ON โžฅ8923113531 ๐Ÿ”Call Girls Kakori Lucknow best sexual service Online  โ˜‚๏ธCALL ON โžฅ8923113531 ๐Ÿ”Call Girls Kakori Lucknow best sexual service Online  โ˜‚๏ธ
CALL ON โžฅ8923113531 ๐Ÿ”Call Girls Kakori Lucknow best sexual service Online โ˜‚๏ธ
anilsa9823
ย 
CHEAP Call Girls in Pushp Vihar (-DELHI )๐Ÿ” 9953056974๐Ÿ”(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )๐Ÿ” 9953056974๐Ÿ”(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )๐Ÿ” 9953056974๐Ÿ”(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )๐Ÿ” 9953056974๐Ÿ”(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
ย 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
bodapatigopi8531
ย 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
ย 

Recently uploaded (20)

CALL ON โžฅ8923113531 ๐Ÿ”Call Girls Kakori Lucknow best sexual service Online โ˜‚๏ธ
CALL ON โžฅ8923113531 ๐Ÿ”Call Girls Kakori Lucknow best sexual service Online  โ˜‚๏ธCALL ON โžฅ8923113531 ๐Ÿ”Call Girls Kakori Lucknow best sexual service Online  โ˜‚๏ธ
CALL ON โžฅ8923113531 ๐Ÿ”Call Girls Kakori Lucknow best sexual service Online โ˜‚๏ธ
ย 
Shapes for Sharing between Graph Data Spacesย - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spacesย - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spacesย - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spacesย - and Epistemic Querying of RDF-...
ย 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
ย 
CHEAP Call Girls in Pushp Vihar (-DELHI )๐Ÿ” 9953056974๐Ÿ”(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )๐Ÿ” 9953056974๐Ÿ”(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )๐Ÿ” 9953056974๐Ÿ”(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )๐Ÿ” 9953056974๐Ÿ”(=)/CALL GIRLS SERVICE
ย 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
ย 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlanโ€™s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlanโ€™s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlanโ€™s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlanโ€™s ...
ย 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
ย 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
ย 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
ย 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
ย 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
ย 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
ย 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
ย 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
ย 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
ย 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
ย 
call girls in Vaishali (Ghaziabad) ๐Ÿ” >เผ’8448380779 ๐Ÿ” genuine Escort Service ๐Ÿ”โœ”๏ธโœ”๏ธ
call girls in Vaishali (Ghaziabad) ๐Ÿ” >เผ’8448380779 ๐Ÿ” genuine Escort Service ๐Ÿ”โœ”๏ธโœ”๏ธcall girls in Vaishali (Ghaziabad) ๐Ÿ” >เผ’8448380779 ๐Ÿ” genuine Escort Service ๐Ÿ”โœ”๏ธโœ”๏ธ
call girls in Vaishali (Ghaziabad) ๐Ÿ” >เผ’8448380779 ๐Ÿ” genuine Escort Service ๐Ÿ”โœ”๏ธโœ”๏ธ
ย 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
ย 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
ย 
Vip Call Girls Noida โžก๏ธ Delhi โžก๏ธ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida โžก๏ธ Delhi โžก๏ธ 9999965857 No Advance 24HRS LiveVip Call Girls Noida โžก๏ธ Delhi โžก๏ธ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida โžก๏ธ Delhi โžก๏ธ 9999965857 No Advance 24HRS Live
ย 

An Introduction to Unit Testing

  • 2. Unit Testing โ€ข What is automated testing? โ€ข Is it a replacement for manual testing? โ€ข Do I really need it ? โ€ข How should I do it?
  • 3. What is automated testing? The practice of writing code to test our code, and then run those tests in an automated fashion .
  • 5.
  • 7. Benefits of Automated test Why on earth should be write code to test our code when we can just run the application and see the result
  • 8. Benefits of Automated test โ€ข Test your code frequently , in less time โ€ข Catch bugs before deploying โ€ข Deploy with confidence โ€ข Reduce the number of bugs that will go into production โ€ข Refactor code with confidence
  • 9. Benefits of Automated test โ€ข Refactoring means changing the structure of the code with out changing its behaviour โ€ข When you donโ€™t have automated test every time you refactor your code you have to manually test every part of the application
  • 10.
  • 11. Unit test Tests a unit of an application without its external dependencies Such as files , databases ,web services .. โ€ข Execute fast โ€ข Donโ€™t give a lot of confidence about the application
  • 12. Integration test Test the application with its external dependencies โ€ข Take long to execute (read / write in DB) โ€ข Give us more confidence about the application
  • 13. End to End test Drives the application through its UI There are a specific tools build for creating end to end test Selenium Allows us to record the interaction of the user with application And check if the application return the right result or not .. โ€ข Give us a great confidence โ€ข Very slow โ€ข Small change can easily break this test
  • 14. At the end of the day Use your own judgment to determine what kind of test you need
  • 15.
  • 16. Tooling Nunit earliest framework out there MSTest Microsoft testing framework built into visual studio Xunit gain more popularity over the past couple of years
  • 17.
  • 18.
  • 19. Characteristics of Good Unit Test โ€ข Clean , readable and maintainable โ€ข No Logic (if, loops,โ€ฆ.etc) โ€ข Isolated
  • 20. What to test and what not to test Unit testing and clean code go hand in hand
  • 22. Attributes โ€ข Setup : call the method decorated with (SetUp attribute) before run each test โ€ข TearDown : call the method decorated with (TearDown attribute) after run each test, often use with ingeneration test โ€ข TestCase : with this we can supply different argument for test method (this attribute in NUnit only ) . โ€ข Ignore : ignore this test method .. And show us the reason why we ignore this method . Better than deleting the method ..
  • 23. Parameterized Tests โ€ข Instead 3 or for method we can have one method takes parameters
  • 24. TrustWorthy Test you can relay on .. If the test passes you know that your code is working And if it fails you know that is something goes wrong with your test
  • 25.
  • 26. How can we write TrustWorthy test? There is to ways โ€ข Test driven development ..TDD โ€ข Code first
  • 27. Test driven development ..TDD Test Frist with TDD you write your test before writing production code create a bug in your code if test pass that is untrustworthy test
  • 28.
  • 29.
  • 30. Unit Testing Techniques โ€ข Testing Strings โ€ข Testing Arrays and Collections โ€ข Testing the Return Type of Methods โ€ข Testing Void Methods โ€ข Testing Methods that Throw Exceptions โ€ข Testing Methods that Raise an Event โ€ข Testing Private Methods
  • 31. If your code is not clean writing unit test is hard or impossible
  • 32. Loosely-coupled and Testable Code Most legacy applications are built without unit testing .. So in order to unit test them You need to refactor them For testable and loosely-couple Design
  • 33. Loosely-coupled and Testable Code it's a description of a relationship between two classes, where each class knows the very least about the other and each class could potentially continue to work just fine whether the other is present or not and without dependency on the particular implementation of the other class. There are 3 steps we need to follow to achieve a testable and loosely-coupled design
  • 34. Loosely-coupled and Testable Code โ€ข Extract the code that uses the external resource into a separate class and isolate it from a rest of you code
  • 35. Next Extract an interface from that class Why an Interface ?? Because an interface like a contract .and there is probably one or more classes that implement this contract
  • 36.
  • 37. Execute โ€ข Refactoring Towards a Loosely-coupled Design โ€ข Dependency Injection via Method Parameters , Properties , Constructor โ€ข Creating Mock Objects Using Moq