Tdd is Dead, Long Live TDD

Preview:

Citation preview

TDD is Dead, Long Live TDD

Works for

Trainer

Software Engineer

@__jacker__

[test]

[test] noun

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

[test] verb

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

[test] verb synonyms

Prove, Assess, Examine

Write code – test it works

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

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

$this->assertEquals($result, 5)

Test Driven DevelopmentWrite the test

first ??

That doesn’t

make any sense!

Kent Beck

“Rediscovered” TDD in 1994

“TDD By Example” – Published 2002

Digital Computer Programming - Daniel Delbert McCracken 1957

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.

PASS

REFACTOR

FAIL

RED / GREEN / REFACTOR

Kent Beck - 2002

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

TestDriven Development

Driven ??

What is software driven by?

Fun

Profit

Business Requirements

What is software driven by?

Requirements Examples

TestDriven DevelopmentBy

Examples

Specification By Example

Specification by Example – Martin Fowler 2004

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

[specification] noun

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

[specification] in Technology

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

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

$this->assertEquals($result, 5)

Test

?Spec?

Did you write it before the code?

It’s a Test It’s a Specification

YesNo

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

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

RSpec – 2005 – Making TDD Productive and

Started as an experiment by Steven Baker

Fun

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

… 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

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

• 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

• 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

Classic TDD Kata (exercise)

The String Calculator

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

StringCalculator

A “Real Life” Example

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)

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?

Focus on one Problem

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

Infrastructure Boundary

Your Domain / Business Logic

Stor

age /

Fra

mew

ork

(db /

file /

se

ssion

)

Framework /

Controllers / UI

Remote API

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..*

CollaboratorsClient

<<interface>>

+ searchFor(term)

SearchHistory<<interface>

>

+ track(term)+ findAll() : array

HighlightingSearch

+ search(term)

SearchResult+ highlight()+ matches(history)

Doubles / MocksClient

<<interface>>

+ searchFor(term)

SearchHistory<<interface>

>

+ track(term)+ findAll() : array

HighlightingSearch

+ search(term)

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

0..*

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

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

double double

class HighlightingSearch{ private $searchHistory;

private $client;

public function __construct(SearchHistory $searchHistory, Client $client

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

class HighlightingSearch{ private $searchHistory;

private $client;

public function __construct(SearchHistory $searchHistory, Client $client

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

dependency dependency

namespace Acme\Wiki;

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

public function findAll() : array;}

namespace Acme\Wiki;

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

namespace Acme\Wiki;

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

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

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

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

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?

Lets build HighlightingSearch

Thank You!