SlideShare una empresa de Scribd logo
1 de 32
Descargar para leer sin conexión
Basic TDD Moves
Heuristics beyond “Red/Green/Refactor”
About me…
Developer since 1994
(C++ used to be my friend…)
Agile Coach since 2009
fernando.a.cuenca@gmail.com
@fer_cuenca
Big Thanks to our Sponsors!
http://lighthouselabs.ca	
  	
   http://devhub.ca	
  	
  
Agenda
  Basic TDD Moves
  Practice Session 1 + Debrief
  BREAK
  Practice Session 2 + Debrief
Start
Here
TDD
Write a
failing
test
Make it
pass
Improve
the
Design
The Three Laws
of TDD
1.  You are not allowed to write any
production code unless it is to make a
failing unit test pass.
2. You are not allowed to write any more
of a unit test than is sufficient to fail;
and compilation failures are failures.
3. You are not allowed to write any more
production code than is sufficient to
pass the one failing unit test.
http://butunclebob.com/ArticleS.UncleBob.TheThreeRulesOfTdd
Robert “Uncle Bob” Martin
1, 2, , 4, , , 7, 8,
, , 11, , 13, 14,
, 16, 17, …
String result = "";
if((number % 3) == 0) result = "fizz";
if((number % 5) == 0) result += "buzz";
if(result.isEmpty())
result = number.toString();
return result;
public class FizzBuzzTests {
@Test public void test_fizzbuzz()
{
fail(“niy”);
}
}
NIY Test
Step on solid ground from the very beginning
public class FizzBuzzTests {
@Test public void test_fizzbuzz()
{
assertThat(FizzBuzz.of(1), is(1));
}
}
First Things First
Start by writing an assertion
public class FizzBuzzTests {
@Test public void test_fizzbuzz()
{
assertThat(FizzBuzz.of(1), is(1));
}
}
public class FizzBuzz
{
public static int of(int number)
{
return 1;
}
}
public class FizzBuzzTests {
@Test public void test_fizzbuzz()
{
assertThat(FizzBuzz.of(1), is(1));
}
@Test public void test_fizzbuzz_2()
{
assertThat(FizzBuzz.of(2), is(2));
}
}
public class FizzBuzz
{
public static int of(int number)
{
return number;
}
}
Uncle Bob’s 3 Laws
Start with Failing Test
Sufficient enough to fail
Uncle Bob’s 3 Laws
Sufficient enough code to pass the test
Triangulation
Approximate with examples
public class FizzBuzzTests {
@Test public void test_fizzbuzz()
{
assertThat(FizzBuzz.of(1), is(1));
}
@Test public void test_fizzbuzz_2()
{
assertThat(FizzBuzz.of(2), is(2));
}
@Test public void test_fizzbuzz_3()
{
assertThat(FizzBuzz.of(3), is(“fizz”));
}
}
public class FizzBuzz
{
public static int of(int number)
{
return number;
}
}
Type mismatch!
Design is wrong!
We need
to go
here…
TDD
Write a
failing
test
Make it
pass
Improve
the
Design
But we’re
here!
public class FizzBuzzTests {
@Test public void test_fizzbuzz()
{
assertThat(FizzBuzz.of(1), is(1));
}
@Test public void test_fizzbuzz_2()
{
assertThat(FizzBuzz.of(2), is(2));
}
//@Test public void test_fizzbuzz_3()
//{
// assertThat(FizzBuzz.of(3), is(“fizz”));
//}
}
public class FizzBuzz
{
public static int of(int number)
{
return number;
}
}
Better Green than Sorry
Refactor only when tests are passing;
public class FizzBuzzTests {
@Test public void test_fizzbuzz()
{
assertThat(FizzBuzz.of(1), is(1));
}
@Test public void test_fizzbuzz_2()
{
assertThat(FizzBuzz.of(2), is(2));
}
//@Test public void test_fizzbuzz_3()
//{
// assertThat(FizzBuzz.of(3), is(“fizz”));
//}
}
public class FizzBuzz
{
public static of( number)
{
return number;
}
}
Better Green than Sorry
Keep all test passing while refactoring
public class FizzBuzzTests {
@Test public void test_fizzbuzz()
{
assertThat(FizzBuzz.of(1), is(1));
}
@Test public void test_fizzbuzz_2()
{
assertThat(FizzBuzz.of(2), is(2));
}
//@Test public void test_fizzbuzz_3()
//{
// assertThat(FizzBuzz.of(3), is(“fizz”));
//}
}
public class FizzBuzz
{
public static of(Integer number)
{
return ();
}
}
Predictable Failure
Refactoring against the Red Bar
public class FizzBuzzTests {
@Test public void test_fizzbuzz()
{
assertThat(FizzBuzz.of(1), is( ));
}
@Test public void test_fizzbuzz_2()
{
assertThat(FizzBuzz.of(2), is( ));
}
//@Test public void test_fizzbuzz_3()
//{
// assertThat(FizzBuzz.of(3), is(“fizz”));
//}
}
public class FizzBuzz
{
public static String of(Integer number)
{
return number.toString();
}
}
Better Green than Sorry
Run all the tests, all the time
The House is in Order
All tests must pass before writing
the next test
public class FizzBuzzTests {
@Test public void test_fizzbuzz()
{
assertThat(FizzBuzz.of(1), is(“1”));
}
@Test public void test_fizzbuzz_2()
{
assertThat(FizzBuzz.of(2), is(“2”));
}
@Test public void test_fizzbuzz_3()
{
assertThat(FizzBuzz.of(3), is(“fizz”));
}
@Test public void test_fizzbuzz_6()
{
assertThat(FizzBuzz.of(6), is(“fizz”));
}
@Test public void test_fizzbuzz_5()
{
assertThat(FizzBuzz.of(5), is(“buzz”));
}
@Test public void test_fizzbuzz_10()
{
assertThat(FizzBuzz.of(10), is(“buzz”));
}
@Test public void test_fizzbuzz_15()
{
assertThat(FizzBuzz.of(15), is(“fizzbuzz”));
}
}
public class FizzBuzz
{
public static String of(Integer number)
{
String result = "";
if((number % 3) == 0) result = "fizz";
if((number % 5) == 0) result += "buzz";
if(result.isEmpty())
result = number.toString();
return result;
}
}
public class FizzBuzzTests {
@Test public void test_fizzbuzz()
{
assertThat(FizzBuzz.of(1), is(“1”));
assertThat(FizzBuzz.of(2), is(“2”));
assertThat(FizzBuzz.of(3), is(“fizz”));
assertThat(FizzBuzz.of(6), is(“fizz”));
assertThat(FizzBuzz.of(5), is(“buzz”));
assertThat(FizzBuzz.of(10), is(“buzz”));
assertThat(FizzBuzz.of(15), is(“fizzbuzz”));
}
}
public class FizzBuzzTests {
@Test
public void returns_fizz_for_multiples_of_3() {
assertThat(FizzBuzz.of(3), is("fizz"));
assertThat(FizzBuzz.of(6), is("fizz"));
}
@Test
public void returns_buzz_for_multiples_of_5() {
assertThat(FizzBuzz.of(5), is("buzz"));
assertThat(FizzBuzz.of(10), is("buzz"));
}
@Test
public void returns_fizzbuzz_for_multiples_of_both_3_and_5()
{
assertThat(FizzBuzz.of(15), is("fizzbuzz"));
assertThat(FizzBuzz.of(30), is("fizzbuzz"));
}
@Test
public void returns_number_as_is_for_other_numbers()
{
assertThat(FizzBuzz.of(1), is("1"));
assertThat(FizzBuzz.of(2), is(“2"));
int bigNumber= 2 * 4 * 7 * 11 * 23;
assertThat(FizzBuzz.of(bigNumber),
is(Integer.toString(bigNumber)));
}
}
No Guessing Games
Test Names Reflect Intent
Assertive Minimalist
Test only “one thing”
public class FizzBuzzTests {
public void returns_fizz_for_multiples_of_3() {…}
public void returns_buzz_for_multiples_of_5() {…}
public void returns_fizzbuzz_for_multiples_of_both_3_and_5() {…}
public void returns_number_as_is_for_other_numbers() {…}
}
Kevlin Henney
“Programming with GUTS”
“Test Smells & Fragrances”
public class FizzBuzzTests {
@Test
public void returns_fizz_for_multiples_of_3() {
assertThat(FizzBuzz.of(3), is("fizz"));
assertThat(FizzBuzz.of(6), is("fizz"));
}
@Test
public void returns_buzz_for_multiples_of_5() {
assertThat(FizzBuzz.of(5), is("buzz"));
assertThat(FizzBuzz.of(10), is("buzz"));
}
@Test
public void returns_fizzbuzz_for_multiples_of_both_3_and_5()
{
assertThat(FizzBuzz.of(15), is("fizzbuzz"));
assertThat(FizzBuzz.of(30), is("fizzbuzz"));
}
@Test
public void returns_number_as_is_for_other_numbers()
{
assertThat(FizzBuzz.of(1), is("1"));
assertThat(FizzBuzz.of(2), is(“2"));
int bigNumber= 2 * 4 * 7 * 11 * 23;
assertThat(FizzBuzz.of(bigNumber),
is(Integer.toString(bigNumber)));
}
}
public class FizzBuzzTests {
@Test
public void returns_fizz_for_multiples_of_3() {
assertThatFizzbuzzForNumbersIs(“fizz”, 3, 6);
}
@Test
public void returns_buzz_for_multiples_of_5() {
assertThatFizzbuzzForNumbersIs(“buzz”, 5, 10);
}
@Test
public void returns_fizzbuzz_for_multiples_of_both_3_and_5()
{
assertThatFizzbuzzForNumbersIs(“fizzbuzz”, 15, 35);
}
@Test
public void returns_number_as_is_for_other_numbers()
{
int bigNumber= 2 * 4 * 7 * 11 * 23;
assertThatNumbersRemainAsIs(1, 2, bigNumber);
}
}
/* Custom Assertions */
private void assertThatFizzbuzForNumbersIs(
String expectedResult, int... numbers)
{
for(int n: numbers)
{
assertThat(
FizzBuzz.of(n), is(expectedResult));
}
}
private void assertThatNumbersRemainAsIs(
int... numbers)
{
for(Integer n: numbers)
{
assertThat(
FizzBuzz.of(n), is(n.toString()));
}
}
Duplication Fighter
No duplication in production or test code
Your turn now!
2 Practice Sessions on the
“String Calculator Kata”
  We need to choose a common Programming Language
  Find a partner (we’ll switch for Session 2)
  Log in to Cyber-Dojo: http://cyber-­‐dojo.org	
  	
  
Practice Session 1:
Basic TDD “Moves”
Uncle Bob’s Three Laws
Start with a failing test
Write enough of a test to fail
Write only the code that is sufficient to make the failing test pass
NIY Test Start on solid ground from the very beginning
First Things First Start the test by writing an assertion
Triangulation Approximate with examples
Keep the House in Order All tests must pass before moving on to the next test
Duplication Fighter
Remove duplication after a test passes
Keep the tests clean and free of duplication
Better Green than Sorry
Refactor only when all tests are passing
Re-run all tests after every refactoring
No Guessing Games Test names reflect intent
Assertive Minimalist Minimize the number of assertions (test "one thing”)
Predictable Failure Refactoring against the Red Bar
Debrief: What was it like?
  What are most important insights you gained from the
session?
  What was helpful / not-helpful?
  What will you do differently in the next session?
Practice Session 2:
Extreme Baby Steps
1.  Setup source control repository.
2.  Setup a timer for 2 minutes interval when you start.
3.  Write exactly one test
1. If the timer rings and the test is red then revert and start over.
2. If the test is green before timer rings then commit.
4. Restart timer (no discussions in between timers)
5. Refactor
1. If the timer rings and the refactoring is not complete then revert and start over.
2. If the refactoring is complete before the timer rings then commit.
6. Restart the timer (no discussions in between timers)
7. Go to 3.
http://blog.adrianbolboaca.ro/2013/03/taking-­‐baby-­‐steps	
  
Debrief: What was it like?
  What are most important insights you gained from this
session?
  What was the overall feeling?
  How does the result compares to what you initially
thought the outcome might be?
  How/Where could this approach be useful?
  What will you do differently tomorrow, back in the
office?
Learning more
Kevlin Henney Presentations
Programming with GUTS
https://www.infoq.com/presentations/testing-communication
Test Smells & Fragrances
https://www.youtube.com/watch?v=wCx_6kOo99M
Thank you!
And please, complete the feedback forms!!
Image Credits
  “Chess”, by John Croudy / used under CC
  Scales image from Wikipedia
  “Feedback”, buy Jurgen Appelo / used under CC

Más contenido relacionado

La actualidad más candente

Test Driven Development: Why I hate it; but secretly love it.
Test Driven Development: Why I hate it; but secretly love it. Test Driven Development: Why I hate it; but secretly love it.
Test Driven Development: Why I hate it; but secretly love it.
Tom Crinson
 
Unit Testing using PHPUnit
Unit Testing using  PHPUnitUnit Testing using  PHPUnit
Unit Testing using PHPUnit
varuntaliyan
 
New Features PHPUnit 3.3 - Sebastian Bergmann
New Features PHPUnit 3.3 - Sebastian BergmannNew Features PHPUnit 3.3 - Sebastian Bergmann
New Features PHPUnit 3.3 - Sebastian Bergmann
dpc
 
20111018 boost and gtest
20111018 boost and gtest20111018 boost and gtest
20111018 boost and gtest
Will Shen
 
Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummies
Harry Potter
 
Advanced junit and mockito
Advanced junit and mockitoAdvanced junit and mockito
Advanced junit and mockito
Mathieu Carbou
 

La actualidad más candente (20)

Test Driven Development: Why I hate it; but secretly love it.
Test Driven Development: Why I hate it; but secretly love it. Test Driven Development: Why I hate it; but secretly love it.
Test Driven Development: Why I hate it; but secretly love it.
 
SmokeTests - What, Why & How - ConFoo 2019
SmokeTests - What, Why & How - ConFoo 2019SmokeTests - What, Why & How - ConFoo 2019
SmokeTests - What, Why & How - ConFoo 2019
 
PHPUnit best practices presentation
PHPUnit best practices presentationPHPUnit best practices presentation
PHPUnit best practices presentation
 
Advanced PHPUnit Testing
Advanced PHPUnit TestingAdvanced PHPUnit Testing
Advanced PHPUnit Testing
 
Confitura 2012 Bad Tests, Good Tests
Confitura 2012 Bad Tests, Good TestsConfitura 2012 Bad Tests, Good Tests
Confitura 2012 Bad Tests, Good Tests
 
GeeCON 2012 Bad Tests, Good Tests
GeeCON 2012 Bad Tests, Good TestsGeeCON 2012 Bad Tests, Good Tests
GeeCON 2012 Bad Tests, Good Tests
 
Executable documentation
Executable documentationExecutable documentation
Executable documentation
 
Unit testing with PHPUnit - there's life outside of TDD
Unit testing with PHPUnit - there's life outside of TDDUnit testing with PHPUnit - there's life outside of TDD
Unit testing with PHPUnit - there's life outside of TDD
 
Introduction to Unit Testing with PHPUnit
Introduction to Unit Testing with PHPUnitIntroduction to Unit Testing with PHPUnit
Introduction to Unit Testing with PHPUnit
 
PHPUnit: from zero to hero
PHPUnit: from zero to heroPHPUnit: from zero to hero
PHPUnit: from zero to hero
 
PhpUnit Best Practices
PhpUnit Best PracticesPhpUnit Best Practices
PhpUnit Best Practices
 
Test Driven Development with PHPUnit
Test Driven Development with PHPUnitTest Driven Development with PHPUnit
Test Driven Development with PHPUnit
 
Unit Testing using PHPUnit
Unit Testing using  PHPUnitUnit Testing using  PHPUnit
Unit Testing using PHPUnit
 
New Features PHPUnit 3.3 - Sebastian Bergmann
New Features PHPUnit 3.3 - Sebastian BergmannNew Features PHPUnit 3.3 - Sebastian Bergmann
New Features PHPUnit 3.3 - Sebastian Bergmann
 
Testing With Test::Class
Testing With Test::ClassTesting With Test::Class
Testing With Test::Class
 
TDD Hands-on
TDD Hands-onTDD Hands-on
TDD Hands-on
 
20111018 boost and gtest
20111018 boost and gtest20111018 boost and gtest
20111018 boost and gtest
 
Realizando Pruebas con Spock
Realizando Pruebas con SpockRealizando Pruebas con Spock
Realizando Pruebas con Spock
 
Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummies
 
Advanced junit and mockito
Advanced junit and mockitoAdvanced junit and mockito
Advanced junit and mockito
 

Similar a Basic TDD moves

Bdd: Tdd and beyond the infinite
Bdd: Tdd and beyond the infiniteBdd: Tdd and beyond the infinite
Bdd: Tdd and beyond the infinite
Giordano Scalzo
 
FizzBuzz Guided Kata
FizzBuzz Guided KataFizzBuzz Guided Kata
FizzBuzz Guided Kata
Mike Clement
 
When Tdd Goes Awry (IAD 2013)
When Tdd Goes Awry (IAD 2013)When Tdd Goes Awry (IAD 2013)
When Tdd Goes Awry (IAD 2013)
Uberto Barbini
 
Refactoring In Tdd The Missing Part
Refactoring In Tdd The Missing PartRefactoring In Tdd The Missing Part
Refactoring In Tdd The Missing Part
Gabriele Lana
 
Pitfalls Of Tdd Adoption by Bartosz Bankowski
Pitfalls Of Tdd Adoption by Bartosz BankowskiPitfalls Of Tdd Adoption by Bartosz Bankowski
Pitfalls Of Tdd Adoption by Bartosz Bankowski
Agileee
 

Similar a Basic TDD moves (20)

Bdd: Tdd and beyond the infinite
Bdd: Tdd and beyond the infiniteBdd: Tdd and beyond the infinite
Bdd: Tdd and beyond the infinite
 
FizzBuzz Guided Kata
FizzBuzz Guided KataFizzBuzz Guided Kata
FizzBuzz Guided Kata
 
Introducción práctica a Test-Driven Development (TDD)
Introducción práctica a Test-Driven Development (TDD)Introducción práctica a Test-Driven Development (TDD)
Introducción práctica a Test-Driven Development (TDD)
 
Introducción práctica a TDD
Introducción práctica a TDDIntroducción práctica a TDD
Introducción práctica a TDD
 
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
 
TDD reloaded - JUGTAA 24 Ottobre 2012
TDD reloaded - JUGTAA 24 Ottobre 2012TDD reloaded - JUGTAA 24 Ottobre 2012
TDD reloaded - JUGTAA 24 Ottobre 2012
 
Type-Directed TDD in Rust: a case study using FizzBuzz
Type-Directed TDD in Rust: a case study using FizzBuzzType-Directed TDD in Rust: a case study using FizzBuzz
Type-Directed TDD in Rust: a case study using FizzBuzz
 
Mutation Testing: Start Hunting The Bugs
Mutation Testing: Start Hunting The BugsMutation Testing: Start Hunting The Bugs
Mutation Testing: Start Hunting The Bugs
 
Tdd is not about testing (OOP)
Tdd is not about testing (OOP)Tdd is not about testing (OOP)
Tdd is not about testing (OOP)
 
When Tdd Goes Awry (IAD 2013)
When Tdd Goes Awry (IAD 2013)When Tdd Goes Awry (IAD 2013)
When Tdd Goes Awry (IAD 2013)
 
TDD Flow: The Mantra in Action
TDD Flow: The Mantra in ActionTDD Flow: The Mantra in Action
TDD Flow: The Mantra in Action
 
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
 
Tdd is not about testing (C++ version)
Tdd is not about testing (C++ version)Tdd is not about testing (C++ version)
Tdd is not about testing (C++ version)
 
Mateusz Bryła - Mutation testing
Mateusz Bryła - Mutation testingMateusz Bryła - Mutation testing
Mateusz Bryła - Mutation testing
 
TDD - Unit testing done right and programmer happiness
TDD - Unit testing done right and programmer happinessTDD - Unit testing done right and programmer happiness
TDD - Unit testing done right and programmer happiness
 
Refactoring In Tdd The Missing Part
Refactoring In Tdd The Missing PartRefactoring In Tdd The Missing Part
Refactoring In Tdd The Missing Part
 
Developer Tests - Things to Know (Vilnius JUG)
Developer Tests - Things to Know (Vilnius JUG)Developer Tests - Things to Know (Vilnius JUG)
Developer Tests - Things to Know (Vilnius JUG)
 
Pitfalls Of Tdd Adoption by Bartosz Bankowski
Pitfalls Of Tdd Adoption by Bartosz BankowskiPitfalls Of Tdd Adoption by Bartosz Bankowski
Pitfalls Of Tdd Adoption by Bartosz Bankowski
 
Assorted TDD tips
Assorted TDD tipsAssorted TDD tips
Assorted TDD tips
 
Hack@macs 2014 test driven development & pair programing
Hack@macs 2014 test driven development & pair programingHack@macs 2014 test driven development & pair programing
Hack@macs 2014 test driven development & pair programing
 

Más de Fernando Cuenca

Más de Fernando Cuenca (15)

Kanban: More than boards and WIP limits
Kanban: More than boards and WIP limitsKanban: More than boards and WIP limits
Kanban: More than boards and WIP limits
 
El Pivot Pragmatico
El Pivot PragmaticoEl Pivot Pragmatico
El Pivot Pragmatico
 
Kanban: mucho Mas que tableros y Limites de WIP
Kanban: mucho Mas que tableros y Limites de WIPKanban: mucho Mas que tableros y Limites de WIP
Kanban: mucho Mas que tableros y Limites de WIP
 
Finding your SDM
Finding your SDMFinding your SDM
Finding your SDM
 
Finding your Service Delivery Manager
Finding your Service Delivery ManagerFinding your Service Delivery Manager
Finding your Service Delivery Manager
 
Agile Dependencies: When "going cross-functional" is not an option
Agile Dependencies: When "going cross-functional" is not an optionAgile Dependencies: When "going cross-functional" is not an option
Agile Dependencies: When "going cross-functional" is not an option
 
Kanban in The Land of Scrum: Choose your Own Scrumban Adventure
Kanban in The Land of Scrum: Choose your Own Scrumban AdventureKanban in The Land of Scrum: Choose your Own Scrumban Adventure
Kanban in The Land of Scrum: Choose your Own Scrumban Adventure
 
From Team Flow to System Flow to Customer Flow: Practical Tools to Keep Valua...
From Team Flow to System Flow to Customer Flow: Practical Tools to Keep Valua...From Team Flow to System Flow to Customer Flow: Practical Tools to Keep Valua...
From Team Flow to System Flow to Customer Flow: Practical Tools to Keep Valua...
 
Que tan agiles somos?
Que tan agiles somos?Que tan agiles somos?
Que tan agiles somos?
 
Your board is trying to tell you something
Your board is trying to tell you somethingYour board is trying to tell you something
Your board is trying to tell you something
 
AgileLunch Meetup - Listen to your Board
AgileLunch Meetup - Listen to your BoardAgileLunch Meetup - Listen to your Board
AgileLunch Meetup - Listen to your Board
 
Visualizing Work: If you can't see it, you can't manage it
Visualizing Work: If you can't see it, you can't manage itVisualizing Work: If you can't see it, you can't manage it
Visualizing Work: If you can't see it, you can't manage it
 
Kanban to #003 - Metrics
Kanban to #003 - MetricsKanban to #003 - Metrics
Kanban to #003 - Metrics
 
Test Driving Legacy Code Mini Workshop
Test Driving Legacy Code Mini WorkshopTest Driving Legacy Code Mini Workshop
Test Driving Legacy Code Mini Workshop
 
Amp up your Agile Implementation with Systems Thinking
Amp up your Agile Implementation with Systems ThinkingAmp up your Agile Implementation with Systems Thinking
Amp up your Agile Implementation with Systems Thinking
 

Último

Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
Epec Engineered Technologies
 
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
jaanualu31
 
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
HenryBriggs2
 

Último (20)

data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086
 
Bridge Jacking Design Sample Calculation.pptx
Bridge Jacking Design Sample Calculation.pptxBridge Jacking Design Sample Calculation.pptx
Bridge Jacking Design Sample Calculation.pptx
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdf
 
DC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationDC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equation
 
kiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal loadkiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal load
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.ppt
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
Engineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planesEngineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planes
 
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced LoadsFEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
 
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
 
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech students
 
Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdf
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxS1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 

Basic TDD moves

  • 1. Basic TDD Moves Heuristics beyond “Red/Green/Refactor”
  • 2. About me… Developer since 1994 (C++ used to be my friend…) Agile Coach since 2009 fernando.a.cuenca@gmail.com @fer_cuenca
  • 3. Big Thanks to our Sponsors! http://lighthouselabs.ca     http://devhub.ca    
  • 4. Agenda   Basic TDD Moves   Practice Session 1 + Debrief   BREAK   Practice Session 2 + Debrief
  • 6. The Three Laws of TDD 1.  You are not allowed to write any production code unless it is to make a failing unit test pass. 2. You are not allowed to write any more of a unit test than is sufficient to fail; and compilation failures are failures. 3. You are not allowed to write any more production code than is sufficient to pass the one failing unit test. http://butunclebob.com/ArticleS.UncleBob.TheThreeRulesOfTdd Robert “Uncle Bob” Martin
  • 7. 1, 2, , 4, , , 7, 8, , , 11, , 13, 14, , 16, 17, …
  • 8. String result = ""; if((number % 3) == 0) result = "fizz"; if((number % 5) == 0) result += "buzz"; if(result.isEmpty()) result = number.toString(); return result;
  • 9. public class FizzBuzzTests { @Test public void test_fizzbuzz() { fail(“niy”); } } NIY Test Step on solid ground from the very beginning
  • 10. public class FizzBuzzTests { @Test public void test_fizzbuzz() { assertThat(FizzBuzz.of(1), is(1)); } } First Things First Start by writing an assertion
  • 11. public class FizzBuzzTests { @Test public void test_fizzbuzz() { assertThat(FizzBuzz.of(1), is(1)); } } public class FizzBuzz { public static int of(int number) { return 1; } }
  • 12. public class FizzBuzzTests { @Test public void test_fizzbuzz() { assertThat(FizzBuzz.of(1), is(1)); } @Test public void test_fizzbuzz_2() { assertThat(FizzBuzz.of(2), is(2)); } } public class FizzBuzz { public static int of(int number) { return number; } } Uncle Bob’s 3 Laws Start with Failing Test Sufficient enough to fail Uncle Bob’s 3 Laws Sufficient enough code to pass the test Triangulation Approximate with examples
  • 13. public class FizzBuzzTests { @Test public void test_fizzbuzz() { assertThat(FizzBuzz.of(1), is(1)); } @Test public void test_fizzbuzz_2() { assertThat(FizzBuzz.of(2), is(2)); } @Test public void test_fizzbuzz_3() { assertThat(FizzBuzz.of(3), is(“fizz”)); } } public class FizzBuzz { public static int of(int number) { return number; } } Type mismatch! Design is wrong! We need to go here… TDD Write a failing test Make it pass Improve the Design But we’re here!
  • 14. public class FizzBuzzTests { @Test public void test_fizzbuzz() { assertThat(FizzBuzz.of(1), is(1)); } @Test public void test_fizzbuzz_2() { assertThat(FizzBuzz.of(2), is(2)); } //@Test public void test_fizzbuzz_3() //{ // assertThat(FizzBuzz.of(3), is(“fizz”)); //} } public class FizzBuzz { public static int of(int number) { return number; } } Better Green than Sorry Refactor only when tests are passing;
  • 15. public class FizzBuzzTests { @Test public void test_fizzbuzz() { assertThat(FizzBuzz.of(1), is(1)); } @Test public void test_fizzbuzz_2() { assertThat(FizzBuzz.of(2), is(2)); } //@Test public void test_fizzbuzz_3() //{ // assertThat(FizzBuzz.of(3), is(“fizz”)); //} } public class FizzBuzz { public static of( number) { return number; } } Better Green than Sorry Keep all test passing while refactoring
  • 16. public class FizzBuzzTests { @Test public void test_fizzbuzz() { assertThat(FizzBuzz.of(1), is(1)); } @Test public void test_fizzbuzz_2() { assertThat(FizzBuzz.of(2), is(2)); } //@Test public void test_fizzbuzz_3() //{ // assertThat(FizzBuzz.of(3), is(“fizz”)); //} } public class FizzBuzz { public static of(Integer number) { return (); } } Predictable Failure Refactoring against the Red Bar
  • 17. public class FizzBuzzTests { @Test public void test_fizzbuzz() { assertThat(FizzBuzz.of(1), is( )); } @Test public void test_fizzbuzz_2() { assertThat(FizzBuzz.of(2), is( )); } //@Test public void test_fizzbuzz_3() //{ // assertThat(FizzBuzz.of(3), is(“fizz”)); //} } public class FizzBuzz { public static String of(Integer number) { return number.toString(); } } Better Green than Sorry Run all the tests, all the time The House is in Order All tests must pass before writing the next test
  • 18. public class FizzBuzzTests { @Test public void test_fizzbuzz() { assertThat(FizzBuzz.of(1), is(“1”)); } @Test public void test_fizzbuzz_2() { assertThat(FizzBuzz.of(2), is(“2”)); } @Test public void test_fizzbuzz_3() { assertThat(FizzBuzz.of(3), is(“fizz”)); } @Test public void test_fizzbuzz_6() { assertThat(FizzBuzz.of(6), is(“fizz”)); } @Test public void test_fizzbuzz_5() { assertThat(FizzBuzz.of(5), is(“buzz”)); } @Test public void test_fizzbuzz_10() { assertThat(FizzBuzz.of(10), is(“buzz”)); } @Test public void test_fizzbuzz_15() { assertThat(FizzBuzz.of(15), is(“fizzbuzz”)); } } public class FizzBuzz { public static String of(Integer number) { String result = ""; if((number % 3) == 0) result = "fizz"; if((number % 5) == 0) result += "buzz"; if(result.isEmpty()) result = number.toString(); return result; } } public class FizzBuzzTests { @Test public void test_fizzbuzz() { assertThat(FizzBuzz.of(1), is(“1”)); assertThat(FizzBuzz.of(2), is(“2”)); assertThat(FizzBuzz.of(3), is(“fizz”)); assertThat(FizzBuzz.of(6), is(“fizz”)); assertThat(FizzBuzz.of(5), is(“buzz”)); assertThat(FizzBuzz.of(10), is(“buzz”)); assertThat(FizzBuzz.of(15), is(“fizzbuzz”)); } }
  • 19. public class FizzBuzzTests { @Test public void returns_fizz_for_multiples_of_3() { assertThat(FizzBuzz.of(3), is("fizz")); assertThat(FizzBuzz.of(6), is("fizz")); } @Test public void returns_buzz_for_multiples_of_5() { assertThat(FizzBuzz.of(5), is("buzz")); assertThat(FizzBuzz.of(10), is("buzz")); } @Test public void returns_fizzbuzz_for_multiples_of_both_3_and_5() { assertThat(FizzBuzz.of(15), is("fizzbuzz")); assertThat(FizzBuzz.of(30), is("fizzbuzz")); } @Test public void returns_number_as_is_for_other_numbers() { assertThat(FizzBuzz.of(1), is("1")); assertThat(FizzBuzz.of(2), is(“2")); int bigNumber= 2 * 4 * 7 * 11 * 23; assertThat(FizzBuzz.of(bigNumber), is(Integer.toString(bigNumber))); } } No Guessing Games Test Names Reflect Intent Assertive Minimalist Test only “one thing”
  • 20. public class FizzBuzzTests { public void returns_fizz_for_multiples_of_3() {…} public void returns_buzz_for_multiples_of_5() {…} public void returns_fizzbuzz_for_multiples_of_both_3_and_5() {…} public void returns_number_as_is_for_other_numbers() {…} } Kevlin Henney “Programming with GUTS” “Test Smells & Fragrances”
  • 21. public class FizzBuzzTests { @Test public void returns_fizz_for_multiples_of_3() { assertThat(FizzBuzz.of(3), is("fizz")); assertThat(FizzBuzz.of(6), is("fizz")); } @Test public void returns_buzz_for_multiples_of_5() { assertThat(FizzBuzz.of(5), is("buzz")); assertThat(FizzBuzz.of(10), is("buzz")); } @Test public void returns_fizzbuzz_for_multiples_of_both_3_and_5() { assertThat(FizzBuzz.of(15), is("fizzbuzz")); assertThat(FizzBuzz.of(30), is("fizzbuzz")); } @Test public void returns_number_as_is_for_other_numbers() { assertThat(FizzBuzz.of(1), is("1")); assertThat(FizzBuzz.of(2), is(“2")); int bigNumber= 2 * 4 * 7 * 11 * 23; assertThat(FizzBuzz.of(bigNumber), is(Integer.toString(bigNumber))); } }
  • 22. public class FizzBuzzTests { @Test public void returns_fizz_for_multiples_of_3() { assertThatFizzbuzzForNumbersIs(“fizz”, 3, 6); } @Test public void returns_buzz_for_multiples_of_5() { assertThatFizzbuzzForNumbersIs(“buzz”, 5, 10); } @Test public void returns_fizzbuzz_for_multiples_of_both_3_and_5() { assertThatFizzbuzzForNumbersIs(“fizzbuzz”, 15, 35); } @Test public void returns_number_as_is_for_other_numbers() { int bigNumber= 2 * 4 * 7 * 11 * 23; assertThatNumbersRemainAsIs(1, 2, bigNumber); } } /* Custom Assertions */ private void assertThatFizzbuzForNumbersIs( String expectedResult, int... numbers) { for(int n: numbers) { assertThat( FizzBuzz.of(n), is(expectedResult)); } } private void assertThatNumbersRemainAsIs( int... numbers) { for(Integer n: numbers) { assertThat( FizzBuzz.of(n), is(n.toString())); } } Duplication Fighter No duplication in production or test code
  • 24. 2 Practice Sessions on the “String Calculator Kata”   We need to choose a common Programming Language   Find a partner (we’ll switch for Session 2)   Log in to Cyber-Dojo: http://cyber-­‐dojo.org    
  • 25.
  • 26. Practice Session 1: Basic TDD “Moves” Uncle Bob’s Three Laws Start with a failing test Write enough of a test to fail Write only the code that is sufficient to make the failing test pass NIY Test Start on solid ground from the very beginning First Things First Start the test by writing an assertion Triangulation Approximate with examples Keep the House in Order All tests must pass before moving on to the next test Duplication Fighter Remove duplication after a test passes Keep the tests clean and free of duplication Better Green than Sorry Refactor only when all tests are passing Re-run all tests after every refactoring No Guessing Games Test names reflect intent Assertive Minimalist Minimize the number of assertions (test "one thing”) Predictable Failure Refactoring against the Red Bar
  • 27. Debrief: What was it like?   What are most important insights you gained from the session?   What was helpful / not-helpful?   What will you do differently in the next session?
  • 28. Practice Session 2: Extreme Baby Steps 1.  Setup source control repository. 2.  Setup a timer for 2 minutes interval when you start. 3.  Write exactly one test 1. If the timer rings and the test is red then revert and start over. 2. If the test is green before timer rings then commit. 4. Restart timer (no discussions in between timers) 5. Refactor 1. If the timer rings and the refactoring is not complete then revert and start over. 2. If the refactoring is complete before the timer rings then commit. 6. Restart the timer (no discussions in between timers) 7. Go to 3. http://blog.adrianbolboaca.ro/2013/03/taking-­‐baby-­‐steps  
  • 29. Debrief: What was it like?   What are most important insights you gained from this session?   What was the overall feeling?   How does the result compares to what you initially thought the outcome might be?   How/Where could this approach be useful?   What will you do differently tomorrow, back in the office?
  • 30. Learning more Kevlin Henney Presentations Programming with GUTS https://www.infoq.com/presentations/testing-communication Test Smells & Fragrances https://www.youtube.com/watch?v=wCx_6kOo99M
  • 31. Thank you! And please, complete the feedback forms!!
  • 32. Image Credits   “Chess”, by John Croudy / used under CC   Scales image from Wikipedia   “Feedback”, buy Jurgen Appelo / used under CC

Notas del editor

  1. This is the code you know you need to write
  2. We know that the final implementation can’t return hard-coded values. What test can I write that pushes the design in that direction?