SlideShare uma empresa Scribd logo
1 de 44
Baixar para ler offline
Test Driven Development (TDD) Workshop!
!
Anjana Somathilake!
Director, Engineering and Architecture at Leapset!

!
Proprietary & Confidential
Coverage!
Problem #1!

In the beginning, your source code
was perfect!!
Then it all changed…!
Problem #2!

“Fix one thing, break another!”!
Problem #3!

You know how to build it, !
but you don’t know how to prove
that it works!!
Problem #4!

What if I change this piece of code?!
Problem #5!

Change Happens !
Problem #6!

Developers get to know about the
issues only after the QA testing.!
Types of Testing

!

• 
• 
• 
• 
• 
• 
• 
• 
• 
• 
• 
• 
• 
• 

Unit testing!
Installation testing!
Compatibility testing!
Smoke and sanity testing!
Regression testing!
Acceptance testing!
Functional testing!
Destructive testing!
Performance testing!
Usability testing!
Accessibility testing!
Security testing!
Integration testing!
A/B testing!
Scope!

•  Introduction to TDD!
•  Unit Testing!

!

•  Refactoring!
Introduction to Test Driven Development

!
TDD as a Habit!

“TDD is NOT an expert level
practice, it is rather simple habit
of writing the test first!”!
Traditional vs. TDD !
What TDD is all about?!

Testing?!

Design!
TDD Lifecycle

!

Write a
failing test

Refactor

Write the
code to
make this
test pass
TDD Process

!
TDD Lifecycle - Detailed

!

Add a Test – Each new feature begins with writing a test. This test must
initially fail since it is written before the feature has been coded.!
!
Run All Tests – validate that the test-harnesses work and that the new
test does not mistakenly pass without requiring any new code.!
!
Write Development Code – That will cause the test to pass.!
!
Run Tests – If all test cases pass the code meets all the tested
requirements.!
!
Clean Up Code – Refactor code to make production ready removing
any dummy/stub code.!
!
Repeat Process – Starting with another new test, the cycle is then
repeated to push forward the functionality.!
TDD Results!

• 

Testable Code

• 

Working Software

• 

Enforce Thinking Ahead 

• 

Clean & Maintainable Code

• 

Increment Code Quality

• 

Faster Feedback

• 

Bring Back the Joy of Coding
TDD in a nutshell!

1.  Write a test!
2.  Watch the test fail!
3.  Write just enough code !
4.  Pass the test!
5.  Refactor, remove duplications!
6.  Pass the test again!
TDD in extremely simplified

!

•  Write a failing test!
•  Pass the test!
•  Repeat!
Unit Testing

!
Unit Testing – Software Equivalent of Exercising !
Unit Testing

!

A unit test is a piece of code that
invokes a unit of work in the system
and then checks a single
assumption about the behavior of
that unit of work.!
Unit of Work

!

•  A unit of work is a single logical functional
use case in the system that can be invoked
by some public interface (in most cases)!
•  A unit of work can span a single method, a
whole class or multiple classes working
together to achieve one single logical
purpose that can be verified!
Unit Test Frameworks!

SUnit!
Smalltalk!
!
JUnit!
Java!
!
NUnit!
.NET!
!
OCUnit!
Objective-c!
!
(etc.)!
JUnit Framework!
Assertions!

A confident and forceful statement
of fact or belief.!
!
“I assert that the earth is round”!
!
But if the assertion is wrong, then there is something fundamentally
flawed about the understanding.!
!

This is the fundamental principle used in unit
testing.!
Assertions in coding!

•  At this point in my code, I assert these two
strings are equal!
!
•  At this point in my code, I assert this object
is not null!
Assert in JUnit!
assertEquals("failure - strings not same", 5l, 5l);
assertNotNull("should not be null", new Object());

int objA[] = new int[10];
int objB[] = new int[10];
assertArrayEquals(objA, objB);

https://github.com/junit-team/junit/wiki/Assertions
Bank Account!

Bank Account
balance
getBalance()
deposit()
withdraw()
Bank Account!

Sprint 1!
!
• 
• 
• 
• 

Create a bank account!
Deposit 100!
Withdraw 50!
Check the balance!

!
!
Sprint 2
!
•  Max withdrawal is 5000!
•  Add penalty of 5, if withdraw more than the balance!
!
!
Sprint 3
•  Throw an exception if negative value is passed to the deposit method
Unit Test Structure!

Arrange

Act

Assert

@Test
public void deposit500(){
BankAccount bankAcct = new BankAccount();
bankAcct.deposit(500);
assertEquals(500, bankAcct.getBalance());
}
Unit Test Code Coverage!

How much unit testing is enough?!
!
How to measure the coverage?!
!
Is there a tool for this?!
Unit Test Code Coverage!
Benefits of Unit Tests



!

•  Fast!
•  Easy to write and maintain!
•  Better code coverage!
•  When fail, provide insight into the
problem!
Refactoring!
Refactoring!

Refactoring is the process of clarifying and
simplifying the design of existing code,
without changing its behavior.!
!
!
At the smallest scale, a “refactoring” is a
minimal, safe transformation of your code that
keeps its behavior unchanged.!
Most well-known Refactoring Patterns.!
Inline Temporary Variable

!
Replace all uses of a local variable by inserting copies of its assigned value.!

public double applyDiscount() {
double basePrice = _cart.totalPrice();
return basePrice * discount();
}

public double applyDiscount() {
return _cart.totalPrice() * discount();
}
Extract method!
Create a new method by extracting a code fragment from an existing!
method.!
public void report(Writer out, List<Machine> machines) throws IOException {
for (Machine machine : machines) {
out.write(“Machine “ + machine.name());
if (machine.bin() != null)
out.write(“ bin=” + machine.bin());
out.write(“n”);
}
}

public void report(Writer out, List<Machine> machines) throws IOException {
for (Machine machine : machines) {
reportMachine(out, machine);
}
}
private void reportMachine(Writer out, Machine machine) throws IOException {
out.write(“Machine “ + machine.name());
if (machine.bin() != null){
out.write(“ bin=” + machine.bin());
}
out.write(“n”);
}
String Calculator Exercise!

1.  Create a simple String calculator with a method int Add(string
numbers)!
1.  The method can take 0, 1 or 2 numbers, and will return their sum
(for an empty string it will return 0) for example “” or “1” or “1,2”!
2.  Start with the simplest test case of an empty string and move to 1
and two numbers!
3.  Remember to solve things as simply as possible so that you force
yourself to write tests you did not think about!
4.  Remember to refactor after each passing test!
2.  Allow the Add method to handle an unknown amount of numbers!
!
3.  Calling Add with a negative number will throw an exception “negatives
not allowed”!
Thank You!!

Mais conteúdo relacionado

Mais procurados

Calabash my understanding
Calabash my understandingCalabash my understanding
Calabash my understandingFARHAN SUMBUL
 
SauceCon 2017: Building a Continuous Delivery Pipeline with Testing in Mind
SauceCon 2017: Building a Continuous Delivery Pipeline with Testing in MindSauceCon 2017: Building a Continuous Delivery Pipeline with Testing in Mind
SauceCon 2017: Building a Continuous Delivery Pipeline with Testing in MindSauce Labs
 
Using Selenium To Test Mobile? Meet Appium!
Using Selenium To Test Mobile? Meet Appium!Using Selenium To Test Mobile? Meet Appium!
Using Selenium To Test Mobile? Meet Appium!Sauce Labs
 
Automated Acceptance Tests & Tool choice
Automated Acceptance Tests & Tool choiceAutomated Acceptance Tests & Tool choice
Automated Acceptance Tests & Tool choicetoddbr
 
Advanced Mocking for Swagger APIs
Advanced Mocking for Swagger APIsAdvanced Mocking for Swagger APIs
Advanced Mocking for Swagger APIsSmartBear
 
Mca 02 year_exp_unit_automation_testing_ldra_rtrt_c -
Mca 02 year_exp_unit_automation_testing_ldra_rtrt_c -Mca 02 year_exp_unit_automation_testing_ldra_rtrt_c -
Mca 02 year_exp_unit_automation_testing_ldra_rtrt_c -sandeep kumar gupta
 
API Test Automation Tips and Tricks
API Test Automation Tips and TricksAPI Test Automation Tips and Tricks
API Test Automation Tips and Trickstesthive
 
Automate REST API Testing
Automate REST API TestingAutomate REST API Testing
Automate REST API TestingTechWell
 
Shift left-csun-sagar-barbhaya
Shift left-csun-sagar-barbhayaShift left-csun-sagar-barbhaya
Shift left-csun-sagar-barbhayaSAGAR BARBHAYA
 
An Introduction To Automated API Testing
An Introduction To Automated API TestingAn Introduction To Automated API Testing
An Introduction To Automated API TestingSauce Labs
 
Ios driver presentation copy
Ios driver presentation copyIos driver presentation copy
Ios driver presentation copyDavid O'Dowd
 
10x Test Coverage, Less Drama: Shift Left Functional & Performance Testing
10x Test Coverage, Less Drama: Shift Left Functional & Performance Testing10x Test Coverage, Less Drama: Shift Left Functional & Performance Testing
10x Test Coverage, Less Drama: Shift Left Functional & Performance TestingSauce Labs
 
SauceCon 2017: test.allTheThings(): Digital Edition
SauceCon 2017: test.allTheThings(): Digital EditionSauceCon 2017: test.allTheThings(): Digital Edition
SauceCon 2017: test.allTheThings(): Digital EditionSauce Labs
 
Do's and Don'ts of APIs
Do's and Don'ts of APIsDo's and Don'ts of APIs
Do's and Don'ts of APIsJason Harmon
 
Wheat - Mobile functional test automation
Wheat - Mobile functional test automationWheat - Mobile functional test automation
Wheat - Mobile functional test automationSunny Tambi
 
YAGNI, YMMV and APIs: building a hybrid strategy for your API platform.
YAGNI, YMMV and APIs: building a hybrid strategy for your API platform.YAGNI, YMMV and APIs: building a hybrid strategy for your API platform.
YAGNI, YMMV and APIs: building a hybrid strategy for your API platform.Diogo Lucas
 
Selenium Camp 2016
Selenium Camp 2016Selenium Camp 2016
Selenium Camp 2016Dan Cuellar
 

Mais procurados (20)

Calabash my understanding
Calabash my understandingCalabash my understanding
Calabash my understanding
 
SauceCon 2017: Building a Continuous Delivery Pipeline with Testing in Mind
SauceCon 2017: Building a Continuous Delivery Pipeline with Testing in MindSauceCon 2017: Building a Continuous Delivery Pipeline with Testing in Mind
SauceCon 2017: Building a Continuous Delivery Pipeline with Testing in Mind
 
Using Selenium To Test Mobile? Meet Appium!
Using Selenium To Test Mobile? Meet Appium!Using Selenium To Test Mobile? Meet Appium!
Using Selenium To Test Mobile? Meet Appium!
 
Automated Acceptance Tests & Tool choice
Automated Acceptance Tests & Tool choiceAutomated Acceptance Tests & Tool choice
Automated Acceptance Tests & Tool choice
 
Advanced Mocking for Swagger APIs
Advanced Mocking for Swagger APIsAdvanced Mocking for Swagger APIs
Advanced Mocking for Swagger APIs
 
Mca 02 year_exp_unit_automation_testing_ldra_rtrt_c -
Mca 02 year_exp_unit_automation_testing_ldra_rtrt_c -Mca 02 year_exp_unit_automation_testing_ldra_rtrt_c -
Mca 02 year_exp_unit_automation_testing_ldra_rtrt_c -
 
How to define an api
How to define an apiHow to define an api
How to define an api
 
React a11y-csun
React a11y-csunReact a11y-csun
React a11y-csun
 
API Test Automation Tips and Tricks
API Test Automation Tips and TricksAPI Test Automation Tips and Tricks
API Test Automation Tips and Tricks
 
Automate REST API Testing
Automate REST API TestingAutomate REST API Testing
Automate REST API Testing
 
Shift left-csun-sagar-barbhaya
Shift left-csun-sagar-barbhayaShift left-csun-sagar-barbhaya
Shift left-csun-sagar-barbhaya
 
Calabash for iPhone apps
Calabash for iPhone appsCalabash for iPhone apps
Calabash for iPhone apps
 
An Introduction To Automated API Testing
An Introduction To Automated API TestingAn Introduction To Automated API Testing
An Introduction To Automated API Testing
 
Ios driver presentation copy
Ios driver presentation copyIos driver presentation copy
Ios driver presentation copy
 
10x Test Coverage, Less Drama: Shift Left Functional & Performance Testing
10x Test Coverage, Less Drama: Shift Left Functional & Performance Testing10x Test Coverage, Less Drama: Shift Left Functional & Performance Testing
10x Test Coverage, Less Drama: Shift Left Functional & Performance Testing
 
SauceCon 2017: test.allTheThings(): Digital Edition
SauceCon 2017: test.allTheThings(): Digital EditionSauceCon 2017: test.allTheThings(): Digital Edition
SauceCon 2017: test.allTheThings(): Digital Edition
 
Do's and Don'ts of APIs
Do's and Don'ts of APIsDo's and Don'ts of APIs
Do's and Don'ts of APIs
 
Wheat - Mobile functional test automation
Wheat - Mobile functional test automationWheat - Mobile functional test automation
Wheat - Mobile functional test automation
 
YAGNI, YMMV and APIs: building a hybrid strategy for your API platform.
YAGNI, YMMV and APIs: building a hybrid strategy for your API platform.YAGNI, YMMV and APIs: building a hybrid strategy for your API platform.
YAGNI, YMMV and APIs: building a hybrid strategy for your API platform.
 
Selenium Camp 2016
Selenium Camp 2016Selenium Camp 2016
Selenium Camp 2016
 

Destaque

BDD testing with cucumber
BDD testing with cucumberBDD testing with cucumber
BDD testing with cucumberDaniel Kummer
 
BDD presentation
BDD presentationBDD presentation
BDD presentationtemebele
 
Behavior Driven Development (BDD) and Agile Testing
Behavior Driven Development (BDD) and Agile TestingBehavior Driven Development (BDD) and Agile Testing
Behavior Driven Development (BDD) and Agile Testingdversaci
 
Test and Behaviour Driven Development (TDD/BDD)
Test and Behaviour Driven Development (TDD/BDD)Test and Behaviour Driven Development (TDD/BDD)
Test and Behaviour Driven Development (TDD/BDD)Lars Thorup
 
Behavior Driven Development and Automation Testing Using Cucumber
Behavior Driven Development and Automation Testing Using CucumberBehavior Driven Development and Automation Testing Using Cucumber
Behavior Driven Development and Automation Testing Using CucumberKMS Technology
 
Behavior Driven Development with Cucumber
Behavior Driven Development with CucumberBehavior Driven Development with Cucumber
Behavior Driven Development with CucumberBrandon Keepers
 

Destaque (7)

BDD testing with cucumber
BDD testing with cucumberBDD testing with cucumber
BDD testing with cucumber
 
BDD presentation
BDD presentationBDD presentation
BDD presentation
 
Behavior Driven Development (BDD) and Agile Testing
Behavior Driven Development (BDD) and Agile TestingBehavior Driven Development (BDD) and Agile Testing
Behavior Driven Development (BDD) and Agile Testing
 
Test and Behaviour Driven Development (TDD/BDD)
Test and Behaviour Driven Development (TDD/BDD)Test and Behaviour Driven Development (TDD/BDD)
Test and Behaviour Driven Development (TDD/BDD)
 
Behavior Driven Development and Automation Testing Using Cucumber
Behavior Driven Development and Automation Testing Using CucumberBehavior Driven Development and Automation Testing Using Cucumber
Behavior Driven Development and Automation Testing Using Cucumber
 
Behavior Driven Development with Cucumber
Behavior Driven Development with CucumberBehavior Driven Development with Cucumber
Behavior Driven Development with Cucumber
 
BDD in Action - building software that matters
BDD in Action - building software that mattersBDD in Action - building software that matters
BDD in Action - building software that matters
 

Semelhante a Test Driven Development - Workshop

iOS Test-Driven Development
iOS Test-Driven DevelopmentiOS Test-Driven Development
iOS Test-Driven DevelopmentPablo Villar
 
Bootstrapping Quality
Bootstrapping QualityBootstrapping Quality
Bootstrapping QualityMichael Roufa
 
DevOps - Boldly Go for Distro
DevOps - Boldly Go for DistroDevOps - Boldly Go for Distro
DevOps - Boldly Go for DistroPaul Boos
 
Developer Job in Practice
Developer Job in PracticeDeveloper Job in Practice
Developer Job in Practiceintive
 
An Introduction to unit testing
An Introduction to unit testingAn Introduction to unit testing
An Introduction to unit testingSteven Casey
 
Test Essentials @mdevcon 2012
Test Essentials @mdevcon 2012Test Essentials @mdevcon 2012
Test Essentials @mdevcon 2012Maxim Zaks
 
Beyond TDD: Enabling Your Team to Continuously Deliver Software
Beyond TDD: Enabling Your Team to Continuously Deliver SoftwareBeyond TDD: Enabling Your Team to Continuously Deliver Software
Beyond TDD: Enabling Your Team to Continuously Deliver SoftwareChris Weldon
 
assertYourself - Breaking the Theories and Assumptions of Unit Testing in Flex
assertYourself - Breaking the Theories and Assumptions of Unit Testing in FlexassertYourself - Breaking the Theories and Assumptions of Unit Testing in Flex
assertYourself - Breaking the Theories and Assumptions of Unit Testing in Flexmichael.labriola
 
How ANDROID TESTING changed how we think about Death - Second Edition
How ANDROID TESTING changed how we think about Death - Second EditionHow ANDROID TESTING changed how we think about Death - Second Edition
How ANDROID TESTING changed how we think about Death - Second EditionFernando Cejas
 
How ANDROID TESTING changed how we think about Death - Second Edition
How ANDROID TESTING changed how we think about Death - Second EditionHow ANDROID TESTING changed how we think about Death - Second Edition
How ANDROID TESTING changed how we think about Death - Second Editionpenanochizzo
 
Unit tests & TDD
Unit tests & TDDUnit tests & TDD
Unit tests & TDDDror Helper
 
Dependency Injection in .NET applications
Dependency Injection in .NET applicationsDependency Injection in .NET applications
Dependency Injection in .NET applicationsBabak Naffas
 
Good Unit Tests Ask For Quality Code
Good Unit Tests Ask For Quality CodeGood Unit Tests Ask For Quality Code
Good Unit Tests Ask For Quality CodeFlorin Coros
 
Testing sync engine
Testing sync engineTesting sync engine
Testing sync engineIlya Puchka
 
What is Unit Testing
What is Unit TestingWhat is Unit Testing
What is Unit TestingSadaaki Emura
 

Semelhante a Test Driven Development - Workshop (20)

iOS Test-Driven Development
iOS Test-Driven DevelopmentiOS Test-Driven Development
iOS Test-Driven Development
 
Grails Spock Testing
Grails Spock TestingGrails Spock Testing
Grails Spock Testing
 
Bootstrapping Quality
Bootstrapping QualityBootstrapping Quality
Bootstrapping Quality
 
DevOps - Boldly Go for Distro
DevOps - Boldly Go for DistroDevOps - Boldly Go for Distro
DevOps - Boldly Go for Distro
 
Developer Job in Practice
Developer Job in PracticeDeveloper Job in Practice
Developer Job in Practice
 
An Introduction to unit testing
An Introduction to unit testingAn Introduction to unit testing
An Introduction to unit testing
 
Test Essentials @mdevcon 2012
Test Essentials @mdevcon 2012Test Essentials @mdevcon 2012
Test Essentials @mdevcon 2012
 
Beyond TDD: Enabling Your Team to Continuously Deliver Software
Beyond TDD: Enabling Your Team to Continuously Deliver SoftwareBeyond TDD: Enabling Your Team to Continuously Deliver Software
Beyond TDD: Enabling Your Team to Continuously Deliver Software
 
assertYourself - Breaking the Theories and Assumptions of Unit Testing in Flex
assertYourself - Breaking the Theories and Assumptions of Unit Testing in FlexassertYourself - Breaking the Theories and Assumptions of Unit Testing in Flex
assertYourself - Breaking the Theories and Assumptions of Unit Testing in Flex
 
eXtreme Programming
eXtreme ProgrammingeXtreme Programming
eXtreme Programming
 
How ANDROID TESTING changed how we think about Death - Second Edition
How ANDROID TESTING changed how we think about Death - Second EditionHow ANDROID TESTING changed how we think about Death - Second Edition
How ANDROID TESTING changed how we think about Death - Second Edition
 
How ANDROID TESTING changed how we think about Death - Second Edition
How ANDROID TESTING changed how we think about Death - Second EditionHow ANDROID TESTING changed how we think about Death - Second Edition
How ANDROID TESTING changed how we think about Death - Second Edition
 
Unit tests & TDD
Unit tests & TDDUnit tests & TDD
Unit tests & TDD
 
Dependency Injection in .NET applications
Dependency Injection in .NET applicationsDependency Injection in .NET applications
Dependency Injection in .NET applications
 
Testing Angular
Testing AngularTesting Angular
Testing Angular
 
Good Unit Tests Ask For Quality Code
Good Unit Tests Ask For Quality CodeGood Unit Tests Ask For Quality Code
Good Unit Tests Ask For Quality Code
 
Codemotion 2015 spock_workshop
Codemotion 2015 spock_workshopCodemotion 2015 spock_workshop
Codemotion 2015 spock_workshop
 
Testing sync engine
Testing sync engineTesting sync engine
Testing sync engine
 
What is Unit Testing
What is Unit TestingWhat is Unit Testing
What is Unit Testing
 
AspectMock
AspectMockAspectMock
AspectMock
 

Último

TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rick Flair
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsNathaniel Shimoni
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...AliaaTarek5
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 

Último (20)

TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directions
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 

Test Driven Development - Workshop

  • 1. Test Driven Development (TDD) Workshop! ! Anjana Somathilake! Director, Engineering and Architecture at Leapset! ! Proprietary & Confidential
  • 3.
  • 4. Problem #1! In the beginning, your source code was perfect!! Then it all changed…!
  • 5. Problem #2! “Fix one thing, break another!”!
  • 6. Problem #3! You know how to build it, ! but you don’t know how to prove that it works!!
  • 7. Problem #4! What if I change this piece of code?!
  • 9. Problem #6! Developers get to know about the issues only after the QA testing.!
  • 10. Types of Testing
 ! •  •  •  •  •  •  •  •  •  •  •  •  •  •  Unit testing! Installation testing! Compatibility testing! Smoke and sanity testing! Regression testing! Acceptance testing! Functional testing! Destructive testing! Performance testing! Usability testing! Accessibility testing! Security testing! Integration testing! A/B testing!
  • 11. Scope! •  Introduction to TDD! •  Unit Testing! ! •  Refactoring!
  • 12. Introduction to Test Driven Development
 !
  • 13. TDD as a Habit! “TDD is NOT an expert level practice, it is rather simple habit of writing the test first!”!
  • 15. What TDD is all about?! Testing?! Design!
  • 16.
  • 17. TDD Lifecycle
 ! Write a failing test Refactor Write the code to make this test pass
  • 19. TDD Lifecycle - Detailed
 ! Add a Test – Each new feature begins with writing a test. This test must initially fail since it is written before the feature has been coded.! ! Run All Tests – validate that the test-harnesses work and that the new test does not mistakenly pass without requiring any new code.! ! Write Development Code – That will cause the test to pass.! ! Run Tests – If all test cases pass the code meets all the tested requirements.! ! Clean Up Code – Refactor code to make production ready removing any dummy/stub code.! ! Repeat Process – Starting with another new test, the cycle is then repeated to push forward the functionality.!
  • 20. TDD Results! •  Testable Code •  Working Software •  Enforce Thinking Ahead •  Clean & Maintainable Code •  Increment Code Quality •  Faster Feedback •  Bring Back the Joy of Coding
  • 21. TDD in a nutshell! 1.  Write a test! 2.  Watch the test fail! 3.  Write just enough code ! 4.  Pass the test! 5.  Refactor, remove duplications! 6.  Pass the test again!
  • 22. TDD in extremely simplified
 ! •  Write a failing test! •  Pass the test! •  Repeat!
  • 24. Unit Testing – Software Equivalent of Exercising !
  • 25. Unit Testing
 ! A unit test is a piece of code that invokes a unit of work in the system and then checks a single assumption about the behavior of that unit of work.!
  • 26. Unit of Work
 ! •  A unit of work is a single logical functional use case in the system that can be invoked by some public interface (in most cases)! •  A unit of work can span a single method, a whole class or multiple classes working together to achieve one single logical purpose that can be verified!
  • 29. Assertions! A confident and forceful statement of fact or belief.! ! “I assert that the earth is round”! ! But if the assertion is wrong, then there is something fundamentally flawed about the understanding.! ! This is the fundamental principle used in unit testing.!
  • 30. Assertions in coding! •  At this point in my code, I assert these two strings are equal! ! •  At this point in my code, I assert this object is not null!
  • 31. Assert in JUnit! assertEquals("failure - strings not same", 5l, 5l); assertNotNull("should not be null", new Object()); int objA[] = new int[10]; int objB[] = new int[10]; assertArrayEquals(objA, objB); https://github.com/junit-team/junit/wiki/Assertions
  • 33. Bank Account! Sprint 1! ! •  •  •  •  Create a bank account! Deposit 100! Withdraw 50! Check the balance! ! ! Sprint 2 ! •  Max withdrawal is 5000! •  Add penalty of 5, if withdraw more than the balance! ! ! Sprint 3 •  Throw an exception if negative value is passed to the deposit method
  • 34. Unit Test Structure! Arrange Act Assert @Test public void deposit500(){ BankAccount bankAcct = new BankAccount(); bankAcct.deposit(500); assertEquals(500, bankAcct.getBalance()); }
  • 35. Unit Test Code Coverage! How much unit testing is enough?! ! How to measure the coverage?! ! Is there a tool for this?!
  • 36. Unit Test Code Coverage!
  • 37. Benefits of Unit Tests
 
 ! •  Fast! •  Easy to write and maintain! •  Better code coverage! •  When fail, provide insight into the problem!
  • 39. Refactoring! Refactoring is the process of clarifying and simplifying the design of existing code, without changing its behavior.! ! ! At the smallest scale, a “refactoring” is a minimal, safe transformation of your code that keeps its behavior unchanged.!
  • 41. Inline Temporary Variable
 ! Replace all uses of a local variable by inserting copies of its assigned value.! public double applyDiscount() { double basePrice = _cart.totalPrice(); return basePrice * discount(); } public double applyDiscount() { return _cart.totalPrice() * discount(); }
  • 42. Extract method! Create a new method by extracting a code fragment from an existing! method.! public void report(Writer out, List<Machine> machines) throws IOException { for (Machine machine : machines) { out.write(“Machine “ + machine.name()); if (machine.bin() != null) out.write(“ bin=” + machine.bin()); out.write(“n”); } } public void report(Writer out, List<Machine> machines) throws IOException { for (Machine machine : machines) { reportMachine(out, machine); } } private void reportMachine(Writer out, Machine machine) throws IOException { out.write(“Machine “ + machine.name()); if (machine.bin() != null){ out.write(“ bin=” + machine.bin()); } out.write(“n”); }
  • 43. String Calculator Exercise! 1.  Create a simple String calculator with a method int Add(string numbers)! 1.  The method can take 0, 1 or 2 numbers, and will return their sum (for an empty string it will return 0) for example “” or “1” or “1,2”! 2.  Start with the simplest test case of an empty string and move to 1 and two numbers! 3.  Remember to solve things as simply as possible so that you force yourself to write tests you did not think about! 4.  Remember to refactor after each passing test! 2.  Allow the Add method to handle an unknown amount of numbers! ! 3.  Calling Add with a negative number will throw an exception “negatives not allowed”!