SlideShare uma empresa Scribd logo
1 de 30
Baixar para ler offline
Introduction to !
Test Driven Development
Prasanna N Venkatesan
Unit Testing is a practice where the smallest
testable parts of an application (units) are
individually and independently tested.
Unit Tests are developer tests.
It enables developers to do “Refactoring” and
helps in “Code Maintenance”
Test Driven Development (TDD) is a
Software development technique which
advocates to test the code that's to be written
before it is written.
Test Driven Development (TDD) is mostly
driven through Unit Tests
Not a Silver Bullet
“TDD doesn't drive good design. TDD
gives you immediate feedback about
what is likely to be a bad design.”
!
- Kent Beck
Design /
Think
Failing Test
Make the
test pass
Refactor
TDD
Design
/ Think
Failing
Test
Make
the test
pass
Refactor TDD
Design / Think!
!
Figure out what test will best move
your code towards completion.
(Take as much time as you need.
This is the hardest step for
beginners.)
Design
/ Think
Failing
Test
Make
the test
pass
Refactor TDD
Write a Failing Test!
!
Write a very small amount of test
code. Only a few lines. Usually no
more than five lines. Run the tests
and watch the new test fail. The
test bar should turn red.
Design
/ Think
Failing
Test
Make
the test
pass
Refactor TDD!
Make The Test Pass!
!
Write a very small amount of
production code. Again, usually no
more than five lines of code. Don't
worry about design purity or
conceptual elegance. Sometimes
you can just hardcode the answer.
Run the tests and watch them pass
the test bar will turn green.
Design
/ Think
Failing
Test
Make
the test
pass
Refactor TDD!
Refactor!
!
Now that your tests are passing, you
can make changes without worrying
about breaking anything. Look at the
code you've written, and ask yourself
if you can improve it. After each little
refactoring, run the tests and make
sure they still pass.
3 Rules of TDD
Rule 1
You can’t write production code unless it makes a
failing unit test pass.
Rule 2
You can’t write any more of unit test that is
sufficient to fail, and not compiling is failing
Rule 3
You can’t write any more production code than
is sufficient to pass one failing unit test.
“But one should not first make the program
and then prove its correctness, because
then the requirement of providing the proof
would only increase the poor programmer's
burden. On the contrary: the programmer
should let correctness proof and program
grow hand in hand.”
!
The Humble Programmer, Edsger W. Dijkstra 1972
Test First vs Testing
✦ Think small, iteratively develop.
✦ More focus on the problem to be
solved.
✦ Immediate feedback of the code.
✦ No excuses for not testing.
@Test
public void shouldReturnTrueForSaturday(){
DeliveryDate deliveryDate = new DeliveryDate();
boolean result = deliveryDate.isWeekendOrHoliday();
assertTrue("Should be true for Saturday",result);
}
First Test - Should Return true for Saturday
public class DeliveryDate {
!
public boolean isWeekendOrHoliday() {
return false;
}
}
@Test
public void shouldReturnTrueForSaturday(){
DeliveryDate deliveryDate = new DeliveryDate();
boolean result = deliveryDate.isWeekendOrHoliday();
assertTrue("Should be true for Saturday",result);
}
First Test - Should Return true for Saturday
public class DeliveryDate {
!
public boolean isWeekendOrHoliday() {
return true;
}
}
@Test
public void shouldReturnTrueForSaturday(){
Calendar calendarInstance = getCalendarForDay(Calendar.SATURDAY);
DeliveryDate deliveryDate = new DeliveryDate(calendarInstance);
boolean result = deliveryDate.isWeekendOrHoliday();
assertTrue("Should be true for Saturday",result);
}
First Test - Should Return true for Saturday
public class DeliveryDate {
private Calendar calendar;
public DeliveryDate(Calendar calendar){
this.calendar = calendar;
}
!
public boolean isWeekendOrHoliday() {
if(calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY){
return true;
}
return false;
}
}
@Test
public void shouldReturnTrueForSunday(){
Calendar calendarInstance = getCalendarForDay(Calendar.SUNDAY);
DeliveryDate deliveryDate = new DeliveryDate(calendarInstance);
boolean result = deliveryDate.isWeekendOrHoliday();
assertTrue("Should be true for Sunday",result);
}
Second Test - Should Return true for Sunday
public class DeliveryDate {
private Calendar calendar;
public DeliveryDate(Calendar calendar){
this.calendar = calendar;
}
!
public boolean isWeekendOrHoliday() {
if(calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY ||
calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY){
return true;
}
return false;
}
}
@Test
public void shouldReturnTrueIfPresentInHolidayList(){
Calendar calendarInstance = getCalendarForDay(Calendar.FRIDAY);
DeliveryDate deliveryDate = new DeliveryDate(calendarInstance);
boolean result = deliveryDate.isWeekendOrHoliday(asList(calendarInstance.getTime()));
assertTrue("Should be true for Holiday",result);
}
Third Test - Should Return true for Holidays
public class DeliveryDate {
private Calendar calendar;
public DeliveryDate(Calendar calendar){
this.calendar = calendar;
}
!
public boolean isWeekendOrHoliday(List<Date> holidayList) {
if(calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY ||
calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY ||
holidayList.contains(calendar.getTime())){
return true;
}
return false;
}
}
@Test
public void shouldReturnFalseIfNotPresentInHolidayListAndNotAWeekend(){
Calendar calendarInstance = getCalendarForDay(Calendar.FRIDAY);
DeliveryDate deliveryDate = new DeliveryDate(calendarInstance);
boolean result = deliveryDate.isWeekendOrHoliday(new ArrayList<Date>());
assertFalse("Should be false for Friday",result);
}
One More to confirm that it’s working.
public class DeliveryDate {
private Calendar calendar;
public DeliveryDate(Calendar calendar){
this.calendar = calendar;
}
!
public boolean isWeekendOrHoliday(List<Date> holidayList) {
if(calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY ||
calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY ||
holidayList.contains(calendar.getTime())){
return true;
}
return false;
}
}
@Test
public void shouldReturnFalseIfNotPresentInHolidayListAndNotAWeekend(){
Calendar calendarInstance = getCalendarForDay(Calendar.FRIDAY);
DeliveryDate deliveryDate = new DeliveryDate(calendarInstance);
boolean result = deliveryDate.isWeekendOrHoliday(null);
// No idea ??
}
What if ??
Unit Test should be
Fast
Independent
Repeatable
Self-Validating
Timely
Test Doubles
Mocks are pre-programmed with
expectations which form a specification
of the calls they are expected to receive.
Test Doubles
Stubs provide canned answers to calls
made during the test, usually not
responding at all to anything outside
what’s programmed in for the test.
Test Doubles
Spies are stubs that also record some
information based on how they were
called.
Test Doubles
✤ Dummy objects that are passed
around but never actually used.
!
✤ Fake objects actually have working
implementation (simpler)
class Washer
def wash(clothes,detergent)
if(isInWorkingCondition? and power.isOn?){
while(!waterIsFull){
inlet.fillWater();
}
100.times do base.spin();
outlet.dispense();
set clothes.washed = true
return clothes unless hasException?
}
throw DirtyClothesException!
end
def isInWorkingCondition?
// Does too many checks to make sure that
end
end
class WasherTest
def wash_returnTrueOnSuccessfulWash
//Mocking External systems
when(MockPower.isOn?).thenReturn(true)
//Stubbing - Overriding Behaviours
def fillWater(){
set waterIsFull = true if 100.times.called
}
//Spying - Internal behaviours
when(isInWorkingCondition?()).then(true)
washedClothes = Washer.wash(dirtyClothes,some_detergent)
assert washedClothes.getStatus == CLEAN
end
end
A quick walkthrough of Test Doubles
** END **

Mais conteúdo relacionado

Mais procurados

Test-Driven Development in Vue with Cypress
Test-Driven Development in Vue with CypressTest-Driven Development in Vue with Cypress
Test-Driven Development in Vue with CypressJosh Justice
 
TDD And Refactoring
TDD And RefactoringTDD And Refactoring
TDD And RefactoringNaresh Jain
 
Google test training
Google test trainingGoogle test training
Google test trainingThierry Gayet
 
Testing, Learning and Professionalism — 20171214
Testing, Learning and Professionalism — 20171214Testing, Learning and Professionalism — 20171214
Testing, Learning and Professionalism — 20171214David Rodenas
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Developmentguestc8093a6
 
2016 10-04: tdd++: tdd made easier
2016 10-04: tdd++: tdd made easier2016 10-04: tdd++: tdd made easier
2016 10-04: tdd++: tdd made easierChristian Hujer
 
Test driven-development
Test driven-developmentTest driven-development
Test driven-developmentDavid Paluy
 
TDD (Test Driven Design)
TDD (Test Driven Design)TDD (Test Driven Design)
TDD (Test Driven Design)nedirtv
 
TDD and Simple Design Workshop - Session 1 - March 2019
TDD and Simple Design Workshop - Session 1 - March 2019TDD and Simple Design Workshop - Session 1 - March 2019
TDD and Simple Design Workshop - Session 1 - March 2019Paulo Clavijo
 
Test-Driven Development In Action
Test-Driven Development In ActionTest-Driven Development In Action
Test-Driven Development In ActionJon Kruger
 
TDD That Was Easy!
TDD   That Was Easy!TDD   That Was Easy!
TDD That Was Easy!Kaizenko
 
Understanding Layers of Testing
Understanding Layers of TestingUnderstanding Layers of Testing
Understanding Layers of TestingChristopher Rex
 
Getting started with Test Driven Development
Getting started with Test Driven DevelopmentGetting started with Test Driven Development
Getting started with Test Driven DevelopmentFerdous Mahmud Shaon
 
Unit Testing - The Whys, Whens and Hows
Unit Testing - The Whys, Whens and HowsUnit Testing - The Whys, Whens and Hows
Unit Testing - The Whys, Whens and Howsatesgoral
 
Introduction to TDD (Test Driven development) - Ahmed Shreef
Introduction to TDD (Test Driven development) - Ahmed ShreefIntroduction to TDD (Test Driven development) - Ahmed Shreef
Introduction to TDD (Test Driven development) - Ahmed ShreefAhmed Shreef
 
TDD - Test Driven Development
TDD - Test Driven DevelopmentTDD - Test Driven Development
TDD - Test Driven DevelopmentTung Nguyen Thanh
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven DevelopmentConsulthinkspa
 
Mutation Testing DevoxxUK 2021
Mutation Testing DevoxxUK 2021Mutation Testing DevoxxUK 2021
Mutation Testing DevoxxUK 2021Paco van Beckhoven
 

Mais procurados (20)

Test-Driven Development in Vue with Cypress
Test-Driven Development in Vue with CypressTest-Driven Development in Vue with Cypress
Test-Driven Development in Vue with Cypress
 
TDD And Refactoring
TDD And RefactoringTDD And Refactoring
TDD And Refactoring
 
Google test training
Google test trainingGoogle test training
Google test training
 
Testing, Learning and Professionalism — 20171214
Testing, Learning and Professionalism — 20171214Testing, Learning and Professionalism — 20171214
Testing, Learning and Professionalism — 20171214
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
2016 10-04: tdd++: tdd made easier
2016 10-04: tdd++: tdd made easier2016 10-04: tdd++: tdd made easier
2016 10-04: tdd++: tdd made easier
 
Test driven-development
Test driven-developmentTest driven-development
Test driven-development
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
TDD (Test Driven Design)
TDD (Test Driven Design)TDD (Test Driven Design)
TDD (Test Driven Design)
 
TDD and Simple Design Workshop - Session 1 - March 2019
TDD and Simple Design Workshop - Session 1 - March 2019TDD and Simple Design Workshop - Session 1 - March 2019
TDD and Simple Design Workshop - Session 1 - March 2019
 
Test-Driven Development In Action
Test-Driven Development In ActionTest-Driven Development In Action
Test-Driven Development In Action
 
TDD That Was Easy!
TDD   That Was Easy!TDD   That Was Easy!
TDD That Was Easy!
 
Understanding Layers of Testing
Understanding Layers of TestingUnderstanding Layers of Testing
Understanding Layers of 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 - The Whys, Whens and Hows
Unit Testing - The Whys, Whens and HowsUnit Testing - The Whys, Whens and Hows
Unit Testing - The Whys, Whens and Hows
 
Introduction to TDD (Test Driven development) - Ahmed Shreef
Introduction to TDD (Test Driven development) - Ahmed ShreefIntroduction to TDD (Test Driven development) - Ahmed Shreef
Introduction to TDD (Test Driven development) - Ahmed Shreef
 
TDD - Test Driven Development
TDD - Test Driven DevelopmentTDD - Test Driven Development
TDD - Test Driven Development
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
TDD, BDD and mocks
TDD, BDD and mocksTDD, BDD and mocks
TDD, BDD and mocks
 
Mutation Testing DevoxxUK 2021
Mutation Testing DevoxxUK 2021Mutation Testing DevoxxUK 2021
Mutation Testing DevoxxUK 2021
 

Destaque

การคัดลอกลายเส้น
การคัดลอกลายเส้นการคัดลอกลายเส้น
การคัดลอกลายเส้นsontayajomjam
 
การคัดลอกลายเส้นอันสมบรูณ์
การคัดลอกลายเส้นอันสมบรูณ์การคัดลอกลายเส้นอันสมบรูณ์
การคัดลอกลายเส้นอันสมบรูณ์sontayajomjam
 
การคัดลอกลายเส้นอันสมบรูณ์
การคัดลอกลายเส้นอันสมบรูณ์การคัดลอกลายเส้นอันสมบรูณ์
การคัดลอกลายเส้นอันสมบรูณ์sontayajomjam
 
การคัดลอกลายเส้นอันสมบรูณ์
การคัดลอกลายเส้นอันสมบรูณ์การคัดลอกลายเส้นอันสมบรูณ์
การคัดลอกลายเส้นอันสมบรูณ์sontayajomjam
 
визитная карточка проекта
визитная карточка проектавизитная карточка проекта
визитная карточка проектаalenavoronova1
 
การคัดลอกลายเส้น
การคัดลอกลายเส้นการคัดลอกลายเส้น
การคัดลอกลายเส้นsontayajomjam
 
การคัดลอกลายเส้น
การคัดลอกลายเส้นการคัดลอกลายเส้น
การคัดลอกลายเส้นsontayajomjam
 
ข้อสอบ
ข้อสอบข้อสอบ
ข้อสอบsutawee
 
การคัดลอกลายเส้น
การคัดลอกลายเส้นการคัดลอกลายเส้น
การคัดลอกลายเส้นsontayajomjam
 
стартовая презентация секреты истории
стартовая презентация секреты историистартовая презентация секреты истории
стартовая презентация секреты историиalenavoronova1
 
визитная карточка проекта
визитная карточка проектавизитная карточка проекта
визитная карточка проектаalenavoronova1
 

Destaque (17)

การ
การการ
การ
 
การคัดลอกลายเส้น
การคัดลอกลายเส้นการคัดลอกลายเส้น
การคัดลอกลายเส้น
 
การคัดลอกลายเส้นอันสมบรูณ์
การคัดลอกลายเส้นอันสมบรูณ์การคัดลอกลายเส้นอันสมบรูณ์
การคัดลอกลายเส้นอันสมบรูณ์
 
การคัดลอกลายเส้นอันสมบรูณ์
การคัดลอกลายเส้นอันสมบรูณ์การคัดลอกลายเส้นอันสมบรูณ์
การคัดลอกลายเส้นอันสมบรูณ์
 
gat มีนา
gat มีนาgat มีนา
gat มีนา
 
การคัดลอกลายเส้นอันสมบรูณ์
การคัดลอกลายเส้นอันสมบรูณ์การคัดลอกลายเส้นอันสมบรูณ์
การคัดลอกลายเส้นอันสมบรูณ์
 
การ
การการ
การ
 
визитная карточка проекта
визитная карточка проектавизитная карточка проекта
визитная карточка проекта
 
การคัดลอกลายเส้น
การคัดลอกลายเส้นการคัดลอกลายเส้น
การคัดลอกลายเส้น
 
521
521521
521
 
การคัดลอกลายเส้น
การคัดลอกลายเส้นการคัดลอกลายเส้น
การคัดลอกลายเส้น
 
ข้อสอบ
ข้อสอบข้อสอบ
ข้อสอบ
 
การคัดลอกลายเส้น
การคัดลอกลายเส้นการคัดลอกลายเส้น
การคัดลอกลายเส้น
 
стартовая презентация секреты истории
стартовая презентация секреты историистартовая презентация секреты истории
стартовая презентация секреты истории
 
визитная карточка проекта
визитная карточка проектавизитная карточка проекта
визитная карточка проекта
 
DevSkillBoard
DevSkillBoardDevSkillBoard
DevSkillBoard
 
Microservices
MicroservicesMicroservices
Microservices
 

Semelhante a Tdd

Art of unit testing: how to do it right
Art of unit testing: how to do it rightArt of unit testing: how to do it right
Art of unit testing: how to do it rightDmytro Patserkovskyi
 
Test-Driven development; why you should never code without it
Test-Driven development; why you should never code without itTest-Driven development; why you should never code without it
Test-Driven development; why you should never code without itJad Salhani
 
SE2018_Lec 20_ Test-Driven Development (TDD)
SE2018_Lec 20_ Test-Driven Development (TDD)SE2018_Lec 20_ Test-Driven Development (TDD)
SE2018_Lec 20_ Test-Driven Development (TDD)Amr E. Mohamed
 
SE2_Lec 21_ TDD and Junit
SE2_Lec 21_ TDD and JunitSE2_Lec 21_ TDD and Junit
SE2_Lec 21_ TDD and JunitAmr E. Mohamed
 
The real beginner's guide to android testing
The real beginner's guide to android testingThe real beginner's guide to android testing
The real beginner's guide to android testingEric (Trung Dung) Nguyen
 
TDD reloaded - JUGTAA 24 Ottobre 2012
TDD reloaded - JUGTAA 24 Ottobre 2012TDD reloaded - JUGTAA 24 Ottobre 2012
TDD reloaded - JUGTAA 24 Ottobre 2012Pietro Di Bello
 
Tdd is not about testing (OOP)
Tdd is not about testing (OOP)Tdd is not about testing (OOP)
Tdd is not about testing (OOP)Gianluca Padovani
 
North Virginia Coldfusion User Group Meetup - Testbox - July 19th 2017
North Virginia Coldfusion User Group Meetup - Testbox - July 19th 2017North Virginia Coldfusion User Group Meetup - Testbox - July 19th 2017
North Virginia Coldfusion User Group Meetup - Testbox - July 19th 2017Ortus Solutions, Corp
 
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
 
7 stages of unit testing
7 stages of unit testing7 stages of unit testing
7 stages of unit testingJorge Ortiz
 
VT.NET 20160411: An Intro to Test Driven Development (TDD)
VT.NET 20160411: An Intro to Test Driven Development (TDD)VT.NET 20160411: An Intro to Test Driven Development (TDD)
VT.NET 20160411: An Intro to Test Driven Development (TDD)Rob Hale
 
Reduce Development Cost with Test Driven Development
Reduce Development Cost with Test Driven DevelopmentReduce Development Cost with Test Driven Development
Reduce Development Cost with Test Driven Developmentsthicks14
 
An introduction to unit testing
An introduction to unit testingAn introduction to unit testing
An introduction to unit testingAdam Stephensen
 
Building unit tests correctly with visual studio 2013
Building unit tests correctly with visual studio 2013Building unit tests correctly with visual studio 2013
Building unit tests correctly with visual studio 2013Dror Helper
 

Semelhante a Tdd (20)

Design for Testability
Design for TestabilityDesign for Testability
Design for Testability
 
Art of unit testing: how to do it right
Art of unit testing: how to do it rightArt of unit testing: how to do it right
Art of unit testing: how to do it right
 
Test-Driven development; why you should never code without it
Test-Driven development; why you should never code without itTest-Driven development; why you should never code without it
Test-Driven development; why you should never code without it
 
TDD Agile Tour Beirut
TDD  Agile Tour BeirutTDD  Agile Tour Beirut
TDD Agile Tour Beirut
 
SE2018_Lec 20_ Test-Driven Development (TDD)
SE2018_Lec 20_ Test-Driven Development (TDD)SE2018_Lec 20_ Test-Driven Development (TDD)
SE2018_Lec 20_ Test-Driven Development (TDD)
 
Test Driven
Test DrivenTest Driven
Test Driven
 
SE2_Lec 21_ TDD and Junit
SE2_Lec 21_ TDD and JunitSE2_Lec 21_ TDD and Junit
SE2_Lec 21_ TDD and Junit
 
The real beginner's guide to android testing
The real beginner's guide to android testingThe real beginner's guide to android testing
The real beginner's guide to android testing
 
TDD reloaded - JUGTAA 24 Ottobre 2012
TDD reloaded - JUGTAA 24 Ottobre 2012TDD reloaded - JUGTAA 24 Ottobre 2012
TDD reloaded - JUGTAA 24 Ottobre 2012
 
Tdd is not about testing (OOP)
Tdd is not about testing (OOP)Tdd is not about testing (OOP)
Tdd is not about testing (OOP)
 
North Virginia Coldfusion User Group Meetup - Testbox - July 19th 2017
North Virginia Coldfusion User Group Meetup - Testbox - July 19th 2017North Virginia Coldfusion User Group Meetup - Testbox - July 19th 2017
North Virginia Coldfusion User Group Meetup - Testbox - July 19th 2017
 
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
 
7 stages of unit testing
7 stages of unit testing7 stages of unit testing
7 stages of unit testing
 
Test driven development
Test driven developmentTest driven development
Test driven development
 
VT.NET 20160411: An Intro to Test Driven Development (TDD)
VT.NET 20160411: An Intro to Test Driven Development (TDD)VT.NET 20160411: An Intro to Test Driven Development (TDD)
VT.NET 20160411: An Intro to Test Driven Development (TDD)
 
Reduce Development Cost with Test Driven Development
Reduce Development Cost with Test Driven DevelopmentReduce Development Cost with Test Driven Development
Reduce Development Cost with Test Driven Development
 
Ian Cooper webinar for DDD Iran: Kent beck style tdd seven years after
Ian Cooper webinar for DDD Iran: Kent beck style tdd   seven years afterIan Cooper webinar for DDD Iran: Kent beck style tdd   seven years after
Ian Cooper webinar for DDD Iran: Kent beck style tdd seven years after
 
An introduction to unit testing
An introduction to unit testingAn introduction to unit testing
An introduction to unit testing
 
Fundamentals of unit testing
Fundamentals of unit testingFundamentals of unit testing
Fundamentals of unit testing
 
Building unit tests correctly with visual studio 2013
Building unit tests correctly with visual studio 2013Building unit tests correctly with visual studio 2013
Building unit tests correctly with visual studio 2013
 

Mais de Prasanna Venkatesan

Building applications in a Micro-frontends way
Building applications in a Micro-frontends wayBuilding applications in a Micro-frontends way
Building applications in a Micro-frontends wayPrasanna Venkatesan
 
SOAP calls in Clojure application
SOAP calls in Clojure applicationSOAP calls in Clojure application
SOAP calls in Clojure applicationPrasanna Venkatesan
 
Micro frontends with react and redux dev day
Micro frontends with react and redux   dev dayMicro frontends with react and redux   dev day
Micro frontends with react and redux dev dayPrasanna Venkatesan
 
My perspective on Tech radar Nov 2016
My perspective on Tech radar Nov 2016My perspective on Tech radar Nov 2016
My perspective on Tech radar Nov 2016Prasanna Venkatesan
 

Mais de Prasanna Venkatesan (6)

Platform engineering
Platform engineeringPlatform engineering
Platform engineering
 
Building applications in a Micro-frontends way
Building applications in a Micro-frontends wayBuilding applications in a Micro-frontends way
Building applications in a Micro-frontends way
 
SOAP calls in Clojure application
SOAP calls in Clojure applicationSOAP calls in Clojure application
SOAP calls in Clojure application
 
Micro frontends with react and redux dev day
Micro frontends with react and redux   dev dayMicro frontends with react and redux   dev day
Micro frontends with react and redux dev day
 
My perspective on Tech radar Nov 2016
My perspective on Tech radar Nov 2016My perspective on Tech radar Nov 2016
My perspective on Tech radar Nov 2016
 
Being a consultant developer
Being a consultant developerBeing a consultant developer
Being a consultant developer
 

Último

08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 

Último (20)

08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 

Tdd

  • 1. Introduction to ! Test Driven Development Prasanna N Venkatesan
  • 2. Unit Testing is a practice where the smallest testable parts of an application (units) are individually and independently tested. Unit Tests are developer tests. It enables developers to do “Refactoring” and helps in “Code Maintenance”
  • 3. Test Driven Development (TDD) is a Software development technique which advocates to test the code that's to be written before it is written. Test Driven Development (TDD) is mostly driven through Unit Tests
  • 4. Not a Silver Bullet
  • 5. “TDD doesn't drive good design. TDD gives you immediate feedback about what is likely to be a bad design.” ! - Kent Beck
  • 6. Design / Think Failing Test Make the test pass Refactor TDD
  • 7. Design / Think Failing Test Make the test pass Refactor TDD Design / Think! ! Figure out what test will best move your code towards completion. (Take as much time as you need. This is the hardest step for beginners.)
  • 8. Design / Think Failing Test Make the test pass Refactor TDD Write a Failing Test! ! Write a very small amount of test code. Only a few lines. Usually no more than five lines. Run the tests and watch the new test fail. The test bar should turn red.
  • 9. Design / Think Failing Test Make the test pass Refactor TDD! Make The Test Pass! ! Write a very small amount of production code. Again, usually no more than five lines of code. Don't worry about design purity or conceptual elegance. Sometimes you can just hardcode the answer. Run the tests and watch them pass the test bar will turn green.
  • 10. Design / Think Failing Test Make the test pass Refactor TDD! Refactor! ! Now that your tests are passing, you can make changes without worrying about breaking anything. Look at the code you've written, and ask yourself if you can improve it. After each little refactoring, run the tests and make sure they still pass.
  • 11. 3 Rules of TDD
  • 12. Rule 1 You can’t write production code unless it makes a failing unit test pass.
  • 13. Rule 2 You can’t write any more of unit test that is sufficient to fail, and not compiling is failing
  • 14. Rule 3 You can’t write any more production code than is sufficient to pass one failing unit test.
  • 15. “But one should not first make the program and then prove its correctness, because then the requirement of providing the proof would only increase the poor programmer's burden. On the contrary: the programmer should let correctness proof and program grow hand in hand.” ! The Humble Programmer, Edsger W. Dijkstra 1972
  • 16. Test First vs Testing ✦ Think small, iteratively develop. ✦ More focus on the problem to be solved. ✦ Immediate feedback of the code. ✦ No excuses for not testing.
  • 17. @Test public void shouldReturnTrueForSaturday(){ DeliveryDate deliveryDate = new DeliveryDate(); boolean result = deliveryDate.isWeekendOrHoliday(); assertTrue("Should be true for Saturday",result); } First Test - Should Return true for Saturday public class DeliveryDate { ! public boolean isWeekendOrHoliday() { return false; } }
  • 18. @Test public void shouldReturnTrueForSaturday(){ DeliveryDate deliveryDate = new DeliveryDate(); boolean result = deliveryDate.isWeekendOrHoliday(); assertTrue("Should be true for Saturday",result); } First Test - Should Return true for Saturday public class DeliveryDate { ! public boolean isWeekendOrHoliday() { return true; } }
  • 19. @Test public void shouldReturnTrueForSaturday(){ Calendar calendarInstance = getCalendarForDay(Calendar.SATURDAY); DeliveryDate deliveryDate = new DeliveryDate(calendarInstance); boolean result = deliveryDate.isWeekendOrHoliday(); assertTrue("Should be true for Saturday",result); } First Test - Should Return true for Saturday public class DeliveryDate { private Calendar calendar; public DeliveryDate(Calendar calendar){ this.calendar = calendar; } ! public boolean isWeekendOrHoliday() { if(calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY){ return true; } return false; } }
  • 20. @Test public void shouldReturnTrueForSunday(){ Calendar calendarInstance = getCalendarForDay(Calendar.SUNDAY); DeliveryDate deliveryDate = new DeliveryDate(calendarInstance); boolean result = deliveryDate.isWeekendOrHoliday(); assertTrue("Should be true for Sunday",result); } Second Test - Should Return true for Sunday public class DeliveryDate { private Calendar calendar; public DeliveryDate(Calendar calendar){ this.calendar = calendar; } ! public boolean isWeekendOrHoliday() { if(calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY){ return true; } return false; } }
  • 21. @Test public void shouldReturnTrueIfPresentInHolidayList(){ Calendar calendarInstance = getCalendarForDay(Calendar.FRIDAY); DeliveryDate deliveryDate = new DeliveryDate(calendarInstance); boolean result = deliveryDate.isWeekendOrHoliday(asList(calendarInstance.getTime())); assertTrue("Should be true for Holiday",result); } Third Test - Should Return true for Holidays public class DeliveryDate { private Calendar calendar; public DeliveryDate(Calendar calendar){ this.calendar = calendar; } ! public boolean isWeekendOrHoliday(List<Date> holidayList) { if(calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY || holidayList.contains(calendar.getTime())){ return true; } return false; } }
  • 22. @Test public void shouldReturnFalseIfNotPresentInHolidayListAndNotAWeekend(){ Calendar calendarInstance = getCalendarForDay(Calendar.FRIDAY); DeliveryDate deliveryDate = new DeliveryDate(calendarInstance); boolean result = deliveryDate.isWeekendOrHoliday(new ArrayList<Date>()); assertFalse("Should be false for Friday",result); } One More to confirm that it’s working. public class DeliveryDate { private Calendar calendar; public DeliveryDate(Calendar calendar){ this.calendar = calendar; } ! public boolean isWeekendOrHoliday(List<Date> holidayList) { if(calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY || holidayList.contains(calendar.getTime())){ return true; } return false; } }
  • 23. @Test public void shouldReturnFalseIfNotPresentInHolidayListAndNotAWeekend(){ Calendar calendarInstance = getCalendarForDay(Calendar.FRIDAY); DeliveryDate deliveryDate = new DeliveryDate(calendarInstance); boolean result = deliveryDate.isWeekendOrHoliday(null); // No idea ?? } What if ??
  • 24. Unit Test should be Fast Independent Repeatable Self-Validating Timely
  • 25. Test Doubles Mocks are pre-programmed with expectations which form a specification of the calls they are expected to receive.
  • 26. Test Doubles Stubs provide canned answers to calls made during the test, usually not responding at all to anything outside what’s programmed in for the test.
  • 27. Test Doubles Spies are stubs that also record some information based on how they were called.
  • 28. Test Doubles ✤ Dummy objects that are passed around but never actually used. ! ✤ Fake objects actually have working implementation (simpler)
  • 29. class Washer def wash(clothes,detergent) if(isInWorkingCondition? and power.isOn?){ while(!waterIsFull){ inlet.fillWater(); } 100.times do base.spin(); outlet.dispense(); set clothes.washed = true return clothes unless hasException? } throw DirtyClothesException! end def isInWorkingCondition? // Does too many checks to make sure that end end class WasherTest def wash_returnTrueOnSuccessfulWash //Mocking External systems when(MockPower.isOn?).thenReturn(true) //Stubbing - Overriding Behaviours def fillWater(){ set waterIsFull = true if 100.times.called } //Spying - Internal behaviours when(isInWorkingCondition?()).then(true) washedClothes = Washer.wash(dirtyClothes,some_detergent) assert washedClothes.getStatus == CLEAN end end A quick walkthrough of Test Doubles