1 of 40

The Beauty of TDD

How you can use Test Driven Development to make things better

2 of 40

TDD

  • Write the Test
  • Make it Pass
  • Refactor�

3 of 40

Red Green Refactor

4 of 40

Why TDD?

  • Testable from the start
  • Tight feedback loop
  • Specific errors
  • Drives smaller, less coupled classes
  • Defines and Documents behavior
  • Creates an ongoing safety net
  • Provides a defense against regression
  • Refactor with peace of mind
  • Grows naturally more modular, flexible code
  • Intuitive way to break up large problems�

5 of 40

Pencil Durability - A Kata

As a writer

I want to be able use a pencil to write text on a sheet of paper

so that I can better remember my thoughts

When the pencil is instructed to write a string of text on a sheet of paper, the paper should reflect the text that was written.

Text written by the pencil should always be appended to existing text on the paper. Thus, given a piece of paper with the text "She sells sea shells", when a pencil is instructed to write " down by the sea shore" on the paper, the paper will then contain the entire string (i.e. "She sells sea shells down by the sea shore").

6 of 40

Paper Returns Text

@Test

public void paperReturnsText() {

Pencil pencil = new Pencil();

String input = "She sells sea shells";

Paper paper = new Paper();

pencil.write(input, paper);

Assert.assertEquals(input, paper.getPage());

}

7 of 40

Paper Returns Text

public class Pencil {

public void write(String text, Paper paper) {

}

}

8 of 40

Paper Returns Text

public class Paper {

public String getPage() {

return "She sells sea shells";

}

}

9 of 40

The smallest step

10 of 40

The smallest step

11 of 40

The Smallest STep

  • Be Lazy!
  • Be Simple
  • Be Dumb (not clever)

12 of 40

What did we test?

  • Created Paper Class
  • Created Pencil Class
  • Created an interface to write with a pencil
  • Created an interface to get a page’s text

13 of 40

What do we test next?

  • Smallest possible step
  • Provide value
  • Drive towards our story
  • Let’s not hardcode that string

14 of 40

Paper Returns Text

@Test

public void paperReturnsText() {

Pencil pencil = new Pencil();

String input = "She sells sea shells";

Paper paper = new Paper();

pencil.write(input, paper);

Assert.assertEquals(input, paper.getPage());

}

15 of 40

Pencil Writes Text

@Test

public void pencilWritesTextToPaper() {

Pencil pencil = new Pencil();

String input = "Down by the sea shore";

Paper paper = new Paper();

pencil.write(input, paper);

Assert.assertEquals(input, paper.getPage());

}

16 of 40

Pencil Writes Text

public class Paper {

private String text = "";

public String getPage() {

return text;

}

public void setText(String text) {

this.text = text;

}

}

17 of 40

Pencil Writes Text

public class Pencil {

public void write(String text, Paper paper) {

paper.setText(text);

}

}

18 of 40

What did we test?

  • Create an additional test (prevent hardcoding)
  • Enforce how pencil interacts with paper
  • Store text on paper

19 of 40

Pencil Durability - A Kata

As a writer

I want to be able use a pencil to write text on a sheet of paper

so that I can better remember my thoughts

When the pencil is instructed to write a string of text on a sheet of paper, the paper should reflect the text that was written.

Text written by the pencil should always be appended to existing text on the paper. Thus, given a piece of paper with the text "She sells sea shells", when a pencil is instructed to write " down by the sea shore" on the paper, the paper will then contain the entire string (i.e. "She sells sea shells down by the sea shore").

20 of 40

Pencil Appends Text

@Test

public void pencilAddsTextToPaper() {

Pencil pencil = new Pencil();

String first = "first part";

String second = "second part";

Paper paper = new Paper();

pencil.write(first, paper);

pencil.write(second, paper);

Assert.assertEquals(first+second, paper.getPage());

}

21 of 40

Pencil Writes Text

public class Pencil {

public void write(String text, Paper paper) {

paper.setText(text);

}

}

22 of 40

Pencil Appends Text

public class Paper {

private String text = "";

public String getPage() {

return text;

}

public void setText(String text) {

this.text += text;

}

}

23 of 40

Pencil Appends Text

public class Paper {

private String text = "";

public String getPage() {

return text;

}

public void setText(String text) {

this.text += text;

}

}

24 of 40

What did we test?

  • Appending text
  • setText or addText?

25 of 40

Refactoring with Tests

public class Paper {

private String text = "";

public String getPage() {

return text;

}

public void addText(String text) {

this.text += text;

}

}

26 of 40

Refactoring with Tests

public class Pencil {

public void write(String text, Paper paper) {

paper.addText(text);

}

}

27 of 40

Pencil Durability - A Kata

As a writer

I want to be able use a pencil to write text on a sheet of paper

so that I can better remember my thoughts

When the pencil is instructed to write a string of text on a sheet of paper, the paper should reflect the text that was written.

Text written by the pencil should always be appended to existing text on the paper. Thus, given a piece of paper with the text "She sells sea shells", when a pencil is instructed to write " down by the sea shore" on the paper, the paper will then contain the entire string (i.e. "She sells sea shells down by the sea shore").

28 of 40

REfactoring the Tests Themselves

  • When is it appropriate to refactor tests?
  • Extract out common code
  • Make sure there is not shared state

29 of 40

Refactoring the Tests

@Test

public void paperReturnsText() {

Pencil pencil = new Pencil();

String input = "She sell sea shells";

Paper paper = new Paper();

pencil.write(input, paper);

Assert.assertEquals(input, paper.getPage());

}

@Test

public void pencilWritesTextToPaper() {

Pencil pencil = new Pencil();

String input = "Down by the sea shore";

Paper paper = new Paper();

pencil.write(input, paper);

Assert.assertEquals(input, paper.getPage());

}

@Test

public void pencilAddsTextToPaper() {

Pencil pencil = new Pencil();

String first = "first part";

String second = "second part";

Paper paper = new Paper();

pencil.write(first, paper);

pencil.write(second, paper);

Assert.assertEquals(first+second, paper.getPage());

}

30 of 40

Refactoring the Tests

public class WriteOnPaperTest {

private Pencil pencil;

private Paper paper;

@Before

public void setup(){

pencil = new Pencil();

paper = new Paper();

}

31 of 40

Refactoring the Tests

@Test

public void paperReturnsText() {

Pencil pencil = new Pencil();

String input = "She sell sea shells";

Paper paper = new Paper();

pencil.write(input, paper);

Assert.assertEquals(input, paper.getPage());

}

32 of 40

Refactoring the Tests

@Test

public void paperReturnsText() {

Pencil pencil = new Pencil();

String input = "She sell sea shells";

Paper paper = new Paper();

pencil.write(input, paper);

Assert.assertEquals(input, paper.getPage());

}

33 of 40

Refactoring the Tests

@Test

public void paperReturnsText() {

String input = "She sell sea shells";

pencil.write(input, paper);

Assert.assertEquals(input, paper.getPage());

}

34 of 40

The Focus of TDD

  • Not worried about code coverage
  • Not testing functions, testing features
  • Break large problems into small steps
  • Can we make a smaller slice? A smaller test?
  • How can we make a problem testable from the start?

35 of 40

Real Life

  • In a Kata, over-exaggerate
  • Production is not always ideal
  • It’s very easy to not TDD one thing, and suddenly not be TDDing anything

36 of 40

Cheating

  • Worst case scenario
  • Code or pseudocode a solution
  • Comment it out
  • Do it again, but this time with TDD
  • Make sure you understand the risks / dangers of this
  • Make sure the second time you’re really driving the code with tests

37 of 40

Level of detail

  • Refactoring without breaking all the tests
  • Testing implementation vs interface
  • Unit vs Integration

38 of 40

Review

  • TDD helps us build more stable code
  • Red Green Refactor
  • Katas help us practice
  • Be dumb, not clever
  • Writing text to a paper, small steps
  • Iterate everything (code and tests)
  • Focus on the right things
  • Strive for the right level of detail
  • Play!�

39 of 40

Try the Kata yourself!

40 of 40

Feedback

  • Please take a minute to fill out some feedback!