SlideShare ist ein Scribd-Unternehmen logo
1 von 16
Test Driven Development & Unit Testing
- Dharmendra Prasad
◦ Is testing only a QA job?
◦ Why should we bother?
◦ What if I don't have tests?
 Testing is more of a Developer Job than QA
 It’s my responsibility towards others
 Another developer can quick start
 Another developer will not spend time debugging your issues
 Losing time on Debugging Production Issues
 No tests during development, will lead to more debug time on a production bug day.
 You will not be there all the time.
 Test Driven Design
 Enforces Design for Testability
◦ Programming to Interfaces
◦ Modularization
◦ Avoiding Statics when possible
 Test First Approach
◦ May lead to minor changes in design
 Finally TDD is a personal choice, no one can force you to use or not use
it!! 
 It puts you into the customer's shoe.
 It gives you an insight on completeness.
 Feature wise development.
 Identification of newer scenarios.
 It helps identifying & solidifying requirements.
 Once well written, it serves you for the life time of an
application.
 People are not much aware of its benefits
 Of course it is time consuming, both writing and reviewing tests.
 Maintainability is an issue if not done properly
 Unit Test - A small piece of code to check another small piece of code.
 Few criteria
◦ Must be very Simple.
◦ Must test only one thing
◦ Must help in understanding the production code
 Our Aim from Unit Testing
◦ Make it Fail
◦ Make it Work
◦ Make it Better (better here means Simple)
 Write a Test
 Run a Test
 Review Results
 REPEAT
 Making Unit Tests Trustworthy
◦ Code coverage
◦ Must test valid logic
◦ Introduce bugs and re run
◦ Start with Failing Test
◦ Don’t use production code in test
◦ Do not change production code if no failing tests
◦ Never change or remove a test (Of course there are exceptions to this rule)
◦ Must Not contain logic
◦ Know the difference between UT and IT
 Creating Maintainable Unit Test
◦ Test only public methods
◦ Enforce Test Isolation
◦ Avoid Multiple Asserts in one test
 Creating Maintainable Unit Test
◦ Follow Naming Conventions Test_Scenario_ExpectedBehavior
◦ If testing with multiple parameters still use the same name with number
suffix
 Runs in memory
 Uses no external systems
 Should be fast
 Easily Repeatable
 Consistent irrespective of where they run.
 Should not be dependent on the third party services or resources
 Write a Test
◦ @Test methods, no arguments, no returns
◦ Biggest impact is Naming Conventions
◦ Unit Testing is about Arrange, Act and Assert
 Arrange - create an instance of the class on which you want to act.
 Act - invoking the method which is to be tested
 Assert - most important part, validating the result
◦ Write tests for Valid data first
** If you do not assert it, then there is no point of this unit test. expected value to actual value.
people tend to swap them which must be taken into account.
 Run a Test (Using JUnit)
◦ Use Runners from the Frameworks
 Runners are used for running test classes
 @Runwith specifies the runner - For e.g. @RunWith(SpringJUnit4ClassRunner.class)
 JUnit tests are started using the JUnitCore class (JUnitCore.runClasses(MyTestClass.class);)
◦ You can use Suite Runners
 Suite are collection of tests
 @Suite invokes the Suite Runner
 @SuiteClasses (to specify all the classes this suite will run)
 Review Results
◦ Verification and Improvements happen here.
Frameworks like Mockito provide MockitoJUnitRunner for automated mock initialization.
 What should be the first step after checking out the code?
 How many Unit Test Cases one should have?
◦ Consider a simple method with two arguments and a Boolean return
 Mock vs Stubs
 Subject of Assertion is called a Mock Object –
◦ We can have multiple stubs for a test. But may want to have just one mock
object.
 Everything else is a Stub.
 Why do we need Mocks?
◦ We cannot simulate all possible situations, downtimes, network error,
Exceptions
 Mock Frameworks are actually Isolation Frameworks. Mockito,
JMock, EasyMock, MockServer (http://www.mock-server.com/)
Thank You

Weitere ähnliche Inhalte

Was ist angesagt?

PChomePay unit test basic
PChomePay unit test basicPChomePay unit test basic
PChomePay unit test basicEric Huang
 
Unit Testing Guidelines
Unit Testing GuidelinesUnit Testing Guidelines
Unit Testing GuidelinesJoel Hooks
 
An Introduction to Unit Testing
An Introduction to Unit TestingAn Introduction to Unit Testing
An Introduction to Unit TestingJoe Tremblay
 
Unit Testing Concepts and Best Practices
Unit Testing Concepts and Best PracticesUnit Testing Concepts and Best Practices
Unit Testing Concepts and Best PracticesDerek Smith
 
Beginners - Get Started With Unit Testing in .NET
Beginners - Get Started With Unit Testing in .NETBeginners - Get Started With Unit Testing in .NET
Beginners - Get Started With Unit Testing in .NETBaskar K
 
Unit Test Presentation
Unit Test PresentationUnit Test Presentation
Unit Test PresentationSayedur Rahman
 
Unit testing (workshop)
Unit testing (workshop)Unit testing (workshop)
Unit testing (workshop)Foyzul Karim
 
Unit Testing And Mocking
Unit Testing And MockingUnit Testing And Mocking
Unit Testing And MockingJoe Wilson
 
Project Onion unit test environment
Project Onion unit test environmentProject Onion unit test environment
Project Onion unit test environmentAbhinav Jha
 
Automated Unit Testing and TDD
Automated Unit Testing and TDDAutomated Unit Testing and TDD
Automated Unit Testing and TDDGreg Sohl
 
Benefit From Unit Testing In The Real World
Benefit From Unit Testing In The Real WorldBenefit From Unit Testing In The Real World
Benefit From Unit Testing In The Real WorldDror Helper
 
Unit Testing and TDD 2017
Unit Testing and TDD 2017Unit Testing and TDD 2017
Unit Testing and TDD 2017Xavi Hidalgo
 
TGT#9 - Lessons learned in software testing - Olga Żądło
TGT#9 - Lessons learned in software testing - Olga ŻądłoTGT#9 - Lessons learned in software testing - Olga Żądło
TGT#9 - Lessons learned in software testing - Olga ŻądłoTrójmiejska Grupa Testerska
 
Unit testing basics with NUnit and Visual Studio
Unit testing basics with NUnit and Visual StudioUnit testing basics with NUnit and Visual Studio
Unit testing basics with NUnit and Visual StudioAmit Choudhary
 

Was ist angesagt? (20)

Testing in java
Testing in javaTesting in java
Testing in java
 
PChomePay unit test basic
PChomePay unit test basicPChomePay unit test basic
PChomePay unit test basic
 
Unit Testing Guidelines
Unit Testing GuidelinesUnit Testing Guidelines
Unit Testing Guidelines
 
An Introduction to Unit Testing
An Introduction to Unit TestingAn Introduction to Unit Testing
An Introduction to Unit Testing
 
Unit Testing Concepts and Best Practices
Unit Testing Concepts and Best PracticesUnit Testing Concepts and Best Practices
Unit Testing Concepts and Best Practices
 
Beginners - Get Started With Unit Testing in .NET
Beginners - Get Started With Unit Testing in .NETBeginners - Get Started With Unit Testing in .NET
Beginners - Get Started With Unit Testing in .NET
 
Unit Test Presentation
Unit Test PresentationUnit Test Presentation
Unit Test Presentation
 
Nunit
NunitNunit
Nunit
 
Unit testing (workshop)
Unit testing (workshop)Unit testing (workshop)
Unit testing (workshop)
 
Unit test
Unit testUnit test
Unit test
 
Debugging
DebuggingDebugging
Debugging
 
Unit Testing And Mocking
Unit Testing And MockingUnit Testing And Mocking
Unit Testing And Mocking
 
Project Onion unit test environment
Project Onion unit test environmentProject Onion unit test environment
Project Onion unit test environment
 
Automated Unit Testing and TDD
Automated Unit Testing and TDDAutomated Unit Testing and TDD
Automated Unit Testing and TDD
 
Benefit From Unit Testing In The Real World
Benefit From Unit Testing In The Real WorldBenefit From Unit Testing In The Real World
Benefit From Unit Testing In The Real World
 
Unit Testing and TDD 2017
Unit Testing and TDD 2017Unit Testing and TDD 2017
Unit Testing and TDD 2017
 
TGT#9 - Lessons learned in software testing - Olga Żądło
TGT#9 - Lessons learned in software testing - Olga ŻądłoTGT#9 - Lessons learned in software testing - Olga Żądło
TGT#9 - Lessons learned in software testing - Olga Żądło
 
xUnit test patterns 0
xUnit test patterns 0xUnit test patterns 0
xUnit test patterns 0
 
Unit testing basics with NUnit and Visual Studio
Unit testing basics with NUnit and Visual StudioUnit testing basics with NUnit and Visual Studio
Unit testing basics with NUnit and Visual Studio
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
 

Andere mochten auch

Creating Content for Disruptive Marketplaces
Creating Content for Disruptive Marketplaces Creating Content for Disruptive Marketplaces
Creating Content for Disruptive Marketplaces Jeremiah McMillan
 
Google Local Marketing | Discover Seo Perth
Google Local Marketing | Discover Seo PerthGoogle Local Marketing | Discover Seo Perth
Google Local Marketing | Discover Seo Perthtomdavid2015
 
Lecture 8: Creative Organizations
Lecture 8: Creative OrganizationsLecture 8: Creative Organizations
Lecture 8: Creative OrganizationsTathagat Varma
 
Domestic violence presentation (crisis intervention)
Domestic violence presentation (crisis intervention)Domestic violence presentation (crisis intervention)
Domestic violence presentation (crisis intervention)Vicky Pulido
 
The Art of the Start
The Art of the StartThe Art of the Start
The Art of the Starthuer1278ft
 

Andere mochten auch (8)

Creating Content for Disruptive Marketplaces
Creating Content for Disruptive Marketplaces Creating Content for Disruptive Marketplaces
Creating Content for Disruptive Marketplaces
 
Manual igor
Manual igorManual igor
Manual igor
 
Google Local Marketing | Discover Seo Perth
Google Local Marketing | Discover Seo PerthGoogle Local Marketing | Discover Seo Perth
Google Local Marketing | Discover Seo Perth
 
Lecture 8: Creative Organizations
Lecture 8: Creative OrganizationsLecture 8: Creative Organizations
Lecture 8: Creative Organizations
 
Tcp(no ip) review part1
Tcp(no ip) review part1Tcp(no ip) review part1
Tcp(no ip) review part1
 
Domestic violence presentation (crisis intervention)
Domestic violence presentation (crisis intervention)Domestic violence presentation (crisis intervention)
Domestic violence presentation (crisis intervention)
 
Food Packaging, Edible Food Packaging, IDM8
Food Packaging, Edible Food Packaging, IDM8Food Packaging, Edible Food Packaging, IDM8
Food Packaging, Edible Food Packaging, IDM8
 
The Art of the Start
The Art of the StartThe Art of the Start
The Art of the Start
 

Ähnlich wie An insight to test driven development and unit testing

Unit testing
Unit testingUnit testing
Unit testingPiXeL16
 
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)Abhijeet Vaikar
 
Software Testing
Software TestingSoftware Testing
Software TestingAdroitLogic
 
Writing Tests with the Unity Test Framework
Writing Tests with the Unity Test FrameworkWriting Tests with the Unity Test Framework
Writing Tests with the Unity Test FrameworkPeter Kofler
 
Making the Unstable Stable - An Intro To Testing
Making the Unstable Stable - An Intro To TestingMaking the Unstable Stable - An Intro To Testing
Making the Unstable Stable - An Intro To TestingCameron Presley
 
An Introduction to Unit Testing
An Introduction to Unit TestingAn Introduction to Unit Testing
An Introduction to Unit TestingSahar Nofal
 
Unit Testing & TDD Training for Mobile Apps
Unit Testing & TDD Training for Mobile AppsUnit Testing & TDD Training for Mobile Apps
Unit Testing & TDD Training for Mobile AppsMarcelo Busico
 
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 NUnitweili_at_slideshare
 
Unit Testing in .NET Core 7.0 with XUnit.pptx
Unit Testing in .NET Core 7.0 with XUnit.pptxUnit Testing in .NET Core 7.0 with XUnit.pptx
Unit Testing in .NET Core 7.0 with XUnit.pptxKnoldus Inc.
 
Test-Driven Development
Test-Driven DevelopmentTest-Driven Development
Test-Driven DevelopmentMeilan Ou
 
JUnit- A Unit Testing Framework
JUnit- A Unit Testing FrameworkJUnit- A Unit Testing Framework
JUnit- A Unit Testing FrameworkOnkar Deshpande
 
Test driven development in .Net - 2010 + Eclipse
Test driven development in .Net - 2010 + EclipseTest driven development in .Net - 2010 + Eclipse
Test driven development in .Net - 2010 + EclipseUTC Fire & Security
 
TDD and Unit Testing in Golang
TDD and Unit Testing in GolangTDD and Unit Testing in Golang
TDD and Unit Testing in GolangSofian Hadiwijaya
 
Junit Interview Questions-ppt
Junit Interview Questions-pptJunit Interview Questions-ppt
Junit Interview Questions-pptMayank Kumar
 
Unit Testing in Angular
Unit Testing in AngularUnit Testing in Angular
Unit Testing in AngularKnoldus Inc.
 
Test Driven Development with Sql Server
Test Driven Development with Sql ServerTest Driven Development with Sql Server
Test Driven Development with Sql ServerDavid P. Moore
 

Ähnlich wie An insight to test driven development and unit testing (20)

Unit testing
Unit testingUnit testing
Unit testing
 
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)
 
Software Testing
Software TestingSoftware Testing
Software Testing
 
Writing Tests with the Unity Test Framework
Writing Tests with the Unity Test FrameworkWriting Tests with the Unity Test Framework
Writing Tests with the Unity Test Framework
 
TDD Workshop UTN 2012
TDD Workshop UTN 2012TDD Workshop UTN 2012
TDD Workshop UTN 2012
 
Making the Unstable Stable - An Intro To Testing
Making the Unstable Stable - An Intro To TestingMaking the Unstable Stable - An Intro To Testing
Making the Unstable Stable - An Intro To Testing
 
Unit testing, principles
Unit testing, principlesUnit testing, principles
Unit testing, principles
 
An Introduction to Unit Testing
An Introduction to Unit TestingAn Introduction to Unit Testing
An Introduction to Unit Testing
 
Unit Testing & TDD Training for Mobile Apps
Unit Testing & TDD Training for Mobile AppsUnit Testing & TDD Training for Mobile Apps
Unit Testing & TDD Training for Mobile Apps
 
TDD Best Practices
TDD Best PracticesTDD Best Practices
TDD Best Practices
 
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
 
Unit Testing in .NET Core 7.0 with XUnit.pptx
Unit Testing in .NET Core 7.0 with XUnit.pptxUnit Testing in .NET Core 7.0 with XUnit.pptx
Unit Testing in .NET Core 7.0 with XUnit.pptx
 
Test-Driven Development
Test-Driven DevelopmentTest-Driven Development
Test-Driven Development
 
JUnit- A Unit Testing Framework
JUnit- A Unit Testing FrameworkJUnit- A Unit Testing Framework
JUnit- A Unit Testing Framework
 
Test driven development in .Net - 2010 + Eclipse
Test driven development in .Net - 2010 + EclipseTest driven development in .Net - 2010 + Eclipse
Test driven development in .Net - 2010 + Eclipse
 
TDD and Unit Testing in Golang
TDD and Unit Testing in GolangTDD and Unit Testing in Golang
TDD and Unit Testing in Golang
 
Junit Interview Questions-ppt
Junit Interview Questions-pptJunit Interview Questions-ppt
Junit Interview Questions-ppt
 
Unit Testing in Angular
Unit Testing in AngularUnit Testing in Angular
Unit Testing in Angular
 
Test Driven Development with Sql Server
Test Driven Development with Sql ServerTest Driven Development with Sql Server
Test Driven Development with Sql Server
 

Kürzlich hochgeladen

%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park masabamasaba
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...masabamasaba
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park masabamasaba
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Bert Jan Schrijver
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfkalichargn70th171
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension AidPhilip Schwarz
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...masabamasaba
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...SelfMade bd
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrainmasabamasaba
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...masabamasaba
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationJuha-Pekka Tolvanen
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...Jittipong Loespradit
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxAnnaArtyushina1
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Hararemasabamasaba
 

Kürzlich hochgeladen (20)

%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptx
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare
 

An insight to test driven development and unit testing

  • 1. Test Driven Development & Unit Testing - Dharmendra Prasad
  • 2. ◦ Is testing only a QA job? ◦ Why should we bother? ◦ What if I don't have tests?
  • 3.  Testing is more of a Developer Job than QA  It’s my responsibility towards others  Another developer can quick start  Another developer will not spend time debugging your issues  Losing time on Debugging Production Issues  No tests during development, will lead to more debug time on a production bug day.  You will not be there all the time.
  • 4.  Test Driven Design  Enforces Design for Testability ◦ Programming to Interfaces ◦ Modularization ◦ Avoiding Statics when possible  Test First Approach ◦ May lead to minor changes in design  Finally TDD is a personal choice, no one can force you to use or not use it!! 
  • 5.  It puts you into the customer's shoe.  It gives you an insight on completeness.  Feature wise development.  Identification of newer scenarios.  It helps identifying & solidifying requirements.  Once well written, it serves you for the life time of an application.
  • 6.  People are not much aware of its benefits  Of course it is time consuming, both writing and reviewing tests.  Maintainability is an issue if not done properly
  • 7.  Unit Test - A small piece of code to check another small piece of code.  Few criteria ◦ Must be very Simple. ◦ Must test only one thing ◦ Must help in understanding the production code  Our Aim from Unit Testing ◦ Make it Fail ◦ Make it Work ◦ Make it Better (better here means Simple)
  • 8.  Write a Test  Run a Test  Review Results  REPEAT
  • 9.  Making Unit Tests Trustworthy ◦ Code coverage ◦ Must test valid logic ◦ Introduce bugs and re run ◦ Start with Failing Test ◦ Don’t use production code in test ◦ Do not change production code if no failing tests ◦ Never change or remove a test (Of course there are exceptions to this rule) ◦ Must Not contain logic ◦ Know the difference between UT and IT
  • 10.  Creating Maintainable Unit Test ◦ Test only public methods ◦ Enforce Test Isolation ◦ Avoid Multiple Asserts in one test  Creating Maintainable Unit Test ◦ Follow Naming Conventions Test_Scenario_ExpectedBehavior ◦ If testing with multiple parameters still use the same name with number suffix
  • 11.  Runs in memory  Uses no external systems  Should be fast  Easily Repeatable  Consistent irrespective of where they run.  Should not be dependent on the third party services or resources
  • 12.  Write a Test ◦ @Test methods, no arguments, no returns ◦ Biggest impact is Naming Conventions ◦ Unit Testing is about Arrange, Act and Assert  Arrange - create an instance of the class on which you want to act.  Act - invoking the method which is to be tested  Assert - most important part, validating the result ◦ Write tests for Valid data first ** If you do not assert it, then there is no point of this unit test. expected value to actual value. people tend to swap them which must be taken into account.
  • 13.  Run a Test (Using JUnit) ◦ Use Runners from the Frameworks  Runners are used for running test classes  @Runwith specifies the runner - For e.g. @RunWith(SpringJUnit4ClassRunner.class)  JUnit tests are started using the JUnitCore class (JUnitCore.runClasses(MyTestClass.class);) ◦ You can use Suite Runners  Suite are collection of tests  @Suite invokes the Suite Runner  @SuiteClasses (to specify all the classes this suite will run)  Review Results ◦ Verification and Improvements happen here. Frameworks like Mockito provide MockitoJUnitRunner for automated mock initialization.
  • 14.  What should be the first step after checking out the code?  How many Unit Test Cases one should have? ◦ Consider a simple method with two arguments and a Boolean return
  • 15.  Mock vs Stubs  Subject of Assertion is called a Mock Object – ◦ We can have multiple stubs for a test. But may want to have just one mock object.  Everything else is a Stub.  Why do we need Mocks? ◦ We cannot simulate all possible situations, downtimes, network error, Exceptions  Mock Frameworks are actually Isolation Frameworks. Mockito, JMock, EasyMock, MockServer (http://www.mock-server.com/)

Hinweis der Redaktion

  1. Is testing only a QA job? We are creative people and we have important code to write, why should we bother wasting our precious time in writing test code which doesn't even run in production ? What if I don't have tests?
  2. The development process is the one where you can make a significant impact. TDD is a fancy term and many of us interpret it in our own fancy ways. Many of us also consider writing the Unit Tests first is TDD. TDD means Test Driven Development Test Driven Design is one way of doing it. Few experts start with a test, they have a design somewhere in their mind and they start writing tests and then implement the design if the test fails. Test First Approach - Most of us follow this, there is a well drafted design, everyone starts writing test code and then test against the design, fix failures and re test and so on. This may force us to change our designs some time. We must always design for Testability (for e.g. programming to interfaces, using public method and not having too many static variables.) Far or less TDD is a personal choice, if you say that my team is not allowing me, or they do not prefer following TDD, it just means that you don't want to implement or follow TDD.
  3. How is TDD beneficial? It puts you into the customer's shoe. When you test your code, you re actually consuming your code. It gives you an insight on how much is complete and working fine. It helps you finish one feature first and then jump on another, following this you will always have something to show. If someone reviews your tests, they might help you with more scenarios It helps identifying hazy requirements because you are the customer of your API Once well written, it serves you for the life time of an application.
  4. Unit Tests have to be very very simple. A Unit Test must test one thing, hence there has to be just one assert per test method. Understandability of code should come inherently from a nicely written test, Naming your tests like loginSuccess1 , loginFailure3 etc wont help. You will end up debugging the tests which is an overhead.
  5. Making Unit Tests Trustworthy – Covers most of the code. Review Tests (they must test valid things). Introduce obvious bug and run tests again. Start with a Failing Test - The Test should eventually pass when you change the production code. Do not re create the bug by reusing the production code logic. You cannot change code if there is no test which fails. In no normal scenarios a Test should be changed or removed. Exceptions are: When Tests become invalid - adding a new test questions the existence of some old test. These two are meant to work in opposite directions. If you do not change the functionality of the tests, for e.g. renaming the tests, re factoring the tests etc. There should be no logic in Unit Tests. If there is some, you are testing more than the capacity of the test method. Separate the Unit Test from Integration Test . For e.g: Oh! the third test failed, but if the fifth one passed its ok we can live with third failing. The third Billing Test is supposed to fail because Webservice is down. I know its failing, don't test around this there is a maintenance going on to the Open ID server
  6. Creating Maintainable Unit Test Only public things must be tested - because that is the contract between the API developer and the world. I can always change the protected and private, if a test is written against the private or protected, it might start failing if I change the name, or re factored it. If you are forced to write a test for such a method, you may consider changing it to public. Use a Setup Method method for Arrange Step and Tear Down method for Clean Up. The block will be called once per test method. Helps in avoiding pollution of the instance state which is used by many other tests. Enforce Test Isolation Each Test should setup its own data and rollback Ensure that tests can be run in any order, any number of times and still give the same result Avoiding Multiple Asserts. Why ? If a previous assert failed, the others are not even evaluated and we do not get a fair picture. If you are testing multiple properties of the same object, you can use multiple assert, or may be you can create the mock for the same object and check equality. Making Unit Tests Readable By following a better naming conventions and following other best practices. The biggest Impact is brought due to - Naming Conventions like Test_Scenario_ExpectedBehavior - This will help us understand, exactly what happens in the test (no need to check the code of the test). You don't even need to debug code, test will automatically tell what scenario failed and you know where it broke. So, you can directly go and fix the code :) If you want to test with different parameters, you will use the same name with a number at the end.