95
TDD is Dead, Long Live TDD

Tdd is Dead, Long Live TDD

Embed Size (px)

Citation preview

Page 1: Tdd is Dead, Long Live TDD

TDD is Dead, Long Live TDD

Page 2: Tdd is Dead, Long Live TDD

Works for

Trainer

Software Engineer

@__jacker__

Page 3: Tdd is Dead, Long Live TDD

[test]

Page 4: Tdd is Dead, Long Live TDD

[test] noun

A procedure intended to establish the quality, performance, or reliability of something, especially before it is taken into widespread use.

Page 5: Tdd is Dead, Long Live TDD

[test] verb

To do something in order to discover if something is safe, works correctly, etc., or if something is present

Page 6: Tdd is Dead, Long Live TDD

[test] verb synonyms

Prove, Assess, Examine

Page 7: Tdd is Dead, Long Live TDD

Write code – test it works

Page 8: Tdd is Dead, Long Live TDD

class Calculator{ public function sum($a, $b) { return $a + $b; }}

Page 9: Tdd is Dead, Long Live TDD

$result = $calculator->sum(2, 3);

$this->assertEquals($result, 5)

Page 10: Tdd is Dead, Long Live TDD

Test Driven DevelopmentWrite the test

first ??

That doesn’t

make any sense!

Page 11: Tdd is Dead, Long Live TDD

Kent Beck

“Rediscovered” TDD in 1994

“TDD By Example” – Published 2002

Page 12: Tdd is Dead, Long Live TDD

Digital Computer Programming - Daniel Delbert McCracken 1957

Page 13: Tdd is Dead, Long Live TDD

The hand calculations can be done at any point during programming. […] In these cases it is highly desirable that the "customer" prepare the check case, largely because logical errors and misunderstandings between the programmer and customer may be pointed out by such procedure.

Page 14: Tdd is Dead, Long Live TDD

PASS

REFACTOR

FAIL

RED / GREEN / REFACTOR

Page 15: Tdd is Dead, Long Live TDD

Kent Beck - 2002

Test-Driven Development is a way of managing fear during programming

Page 16: Tdd is Dead, Long Live TDD

TestDriven Development

Page 17: Tdd is Dead, Long Live TDD

Driven ??

Page 18: Tdd is Dead, Long Live TDD

What is software driven by?

Fun

Profit

Business Requirements

Page 19: Tdd is Dead, Long Live TDD

What is software driven by?

Requirements Examples

Page 20: Tdd is Dead, Long Live TDD

TestDriven DevelopmentBy

Examples

Page 21: Tdd is Dead, Long Live TDD

Specification By Example

Page 22: Tdd is Dead, Long Live TDD

Specification by Example – Martin Fowler 2004

Page 23: Tdd is Dead, Long Live TDD

Tests based on shared examples fit best in the category of tests designed to support a team while delivering software from a business perspective - ensuring that the right product is built

Page 24: Tdd is Dead, Long Live TDD

[specification] noun

a detailed description or assessment of requirements, dimensions, materials, etc., as of a proposed building, machine, bridge, etc.

Page 25: Tdd is Dead, Long Live TDD

[specification] in Technology

(spec) A document describing how some system should work.

Page 26: Tdd is Dead, Long Live TDD

$result = $calculator->sum(2, 3);

$this->assertEquals($result, 5)

Page 27: Tdd is Dead, Long Live TDD

Test

?Spec?

Page 28: Tdd is Dead, Long Live TDD

Did you write it before the code?

It’s a Test It’s a Specification

YesNo

Page 29: Tdd is Dead, Long Live TDD

Changing the way of thinking about the same problem by changing the language used to describe it

Page 30: Tdd is Dead, Long Live TDD

To reframe, then, means to change the conceptual and emotional setting or viewpoint in relation to which a situation is experienced and to place it in another frame which fits the 'facts' of the same concrete situation equally well or even better, and thereby changing its entire meaning.

1974, Watzlawick, Weakland and Fisch

Page 31: Tdd is Dead, Long Live TDD

RSpec – 2005 – Making TDD Productive and

Started as an experiment by Steven Baker

Fun

Page 32: Tdd is Dead, Long Live TDD

Don’t rush ahead with more code. Instead, add another example and let it guide you to what you have to do next.

Page 33: Tdd is Dead, Long Live TDD

… using tests to drive the features and the object-oriented structure of the code, and using Mock Objects to discover and then describe relationships between objects

Page 34: Tdd is Dead, Long Live TDD

2007 - PHPSpec 1.0 - Padraic Brady & Travis Swicegood

A Port of RSpec 4

SpecBDD

2010 - PHPSpec 2.0 – Complete rewrite by –Marcello Duarte & Konstantin Kudryashov

Page 35: Tdd is Dead, Long Live TDD
Page 36: Tdd is Dead, Long Live TDD
Page 37: Tdd is Dead, Long Live TDD

• public function testListContainsKeyword(){ $basket = new Basket(); $basket->add('apple'); $this->assertTrue($basket->has('apple'));}

What we’re going to test

Make an assertion about what it should return

Page 38: Tdd is Dead, Long Live TDD

• public function it_returns_true_if_it_has_an_apple(){ $this->add('apple'); $this->has('apple’)->shouldReturn(true);}

Specifying desired behavior

Set up an expectation of the behavior

Page 39: Tdd is Dead, Long Live TDD

Classic TDD Kata (exercise)

Page 40: Tdd is Dead, Long Live TDD

The String Calculator

Page 41: Tdd is Dead, Long Live TDD

Returns 0 for an empty string Returns the “bare” number given one number Returns the sum of numbers separated by space

StringCalculator

Page 42: Tdd is Dead, Long Live TDD
Page 43: Tdd is Dead, Long Live TDD
Page 44: Tdd is Dead, Long Live TDD
Page 45: Tdd is Dead, Long Live TDD
Page 46: Tdd is Dead, Long Live TDD
Page 47: Tdd is Dead, Long Live TDD
Page 48: Tdd is Dead, Long Live TDD
Page 49: Tdd is Dead, Long Live TDD
Page 50: Tdd is Dead, Long Live TDD
Page 51: Tdd is Dead, Long Live TDD
Page 52: Tdd is Dead, Long Live TDD
Page 53: Tdd is Dead, Long Live TDD
Page 54: Tdd is Dead, Long Live TDD
Page 55: Tdd is Dead, Long Live TDD
Page 56: Tdd is Dead, Long Live TDD
Page 57: Tdd is Dead, Long Live TDD
Page 58: Tdd is Dead, Long Live TDD
Page 59: Tdd is Dead, Long Live TDD
Page 60: Tdd is Dead, Long Live TDD
Page 61: Tdd is Dead, Long Live TDD
Page 62: Tdd is Dead, Long Live TDD

A “Real Life” Example

Page 63: Tdd is Dead, Long Live TDD

Acceptance Criteria??

I want something that will - allow searching Wikipedia for a

keyword, - keep track of past searches - highlights results that contain

previous search terms.- (but not my current one)

Page 64: Tdd is Dead, Long Live TDD

Hmm… guess I’ll need to

- Store searched in database? session?

- Make remote calls to wikipedia

- What’s wikipedia’s API like?- What color will the highlight

be?

Page 65: Tdd is Dead, Long Live TDD

Focus on one Problem

- Ignore the infrastructure- Ignore the framework- What’s left?

Page 66: Tdd is Dead, Long Live TDD

Infrastructure Boundary

Page 67: Tdd is Dead, Long Live TDD

Your Domain / Business Logic

Stor

age /

Fra

mew

ork

(db /

file /

se

ssion

)

Framework /

Controllers / UI

Remote API

Page 68: Tdd is Dead, Long Live TDD

Your Domain / Business Logic

Lara

vel M

odel

/

Quer

y Buil

der

Laravel Controller +

Blade

Wikipedia Search

Client<<interface>

>

+ searchFor(term)SearchHistory<<interface>

>

+ track(term)+ findAll() : array

SearchController

+ lookup(term)

HighlightingSearch

+ search(term)

SearchResult+ highlight()+ matches(history)

0..*

Page 69: Tdd is Dead, Long Live TDD

CollaboratorsClient

<<interface>>

+ searchFor(term)

SearchHistory<<interface>

>

+ track(term)+ findAll() : array

HighlightingSearch

+ search(term)

SearchResult+ highlight()+ matches(history)

Page 70: Tdd is Dead, Long Live TDD

Doubles / MocksClient

<<interface>>

+ searchFor(term)

SearchHistory<<interface>

>

+ track(term)+ findAll() : array

HighlightingSearch

+ search(term)

SearchResult<<interface>>+ highlight()+ matches(history)

0..*

Page 71: Tdd is Dead, Long Live TDD

class HighlightingSearchSpec extends ObjectBehavior{ function let(SearchHistory $searchHistory, Client $client) { $this->beConstructedWith($searchHistory, $client); }}

Page 72: Tdd is Dead, Long Live TDD

class HighlightingSearchSpec extends ObjectBehavior{ function let(SearchHistory $searchHistory, Client $client) { $this->beConstructedWith($searchHistory, $client); }}

double double

Page 73: Tdd is Dead, Long Live TDD

class HighlightingSearch{ private $searchHistory;

private $client;

public function __construct(SearchHistory $searchHistory, Client $client

) { $this->searchHistory = $searchHistory; $this->client = $client; }}

Page 74: Tdd is Dead, Long Live TDD

class HighlightingSearch{ private $searchHistory;

private $client;

public function __construct(SearchHistory $searchHistory, Client $client

) { $this->searchHistory = $searchHistory; $this->client = $client; }}

dependency dependency

Page 75: Tdd is Dead, Long Live TDD

namespace Acme\Wiki;

interface SearchHistory{ public function add(string $term);

public function findAll() : array;}

Page 76: Tdd is Dead, Long Live TDD

namespace Acme\Wiki;

interface Client{ public function searchFor(string $term) : array;}

Page 77: Tdd is Dead, Long Live TDD

namespace Acme\Wiki;

interface SearchResult{ public function highlight(); public function matches(array $historicTerms) : bool;}

Page 78: Tdd is Dead, Long Live TDD

Keeps track of search history Returns a collection of search results Highlights results whose titles do match historic terms Does not highlight any results if there is no search history Does not highlight results whose titles don’t match historic

terms Ignores the current search term Only matches whole words

HighlightingSearch

Page 79: Tdd is Dead, Long Live TDD

Keeps track of search history Returns a collection of search results Highlights results whose titles do match historic terms Does not highlight any results if there is no search history Does not highlight results whose titles don’t match historic

terms Ignores the current search term Only matches whole words

HighlightingSearch

delegated to SearchResult

Page 80: Tdd is Dead, Long Live TDD

Keeps track of search history Returns a collection of search results Highlights results whose titles do match historic terms Does not highlight any results if there is no search history Does not highlight results whose title don’t match historic

terms Ignores the current search term Only matches whole words

HighlightingSearch

delegated to SearchResult

Page 81: Tdd is Dead, Long Live TDD

Keeps track of search history Returns a collection of search results Highlights results whose titles do match historic terms Does not highlight any results if there is no search history Does not highlight results whose title don’t match historic

terms Ignores the current search term Only matches whole words

HighlightingSearch

delegated to SearchResult

Is this a requirement?

Page 82: Tdd is Dead, Long Live TDD
Page 83: Tdd is Dead, Long Live TDD
Page 84: Tdd is Dead, Long Live TDD
Page 85: Tdd is Dead, Long Live TDD

Lets build HighlightingSearch

Page 86: Tdd is Dead, Long Live TDD
Page 87: Tdd is Dead, Long Live TDD
Page 88: Tdd is Dead, Long Live TDD
Page 89: Tdd is Dead, Long Live TDD
Page 90: Tdd is Dead, Long Live TDD
Page 91: Tdd is Dead, Long Live TDD
Page 92: Tdd is Dead, Long Live TDD
Page 93: Tdd is Dead, Long Live TDD
Page 94: Tdd is Dead, Long Live TDD
Page 95: Tdd is Dead, Long Live TDD

Thank You!