SlideShare a Scribd company logo
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 Development
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
Test Driven Development
Driven ??
What is software driven by?
Fun
Profit
Business Requirements
What is software driven by?
Requirements Examples
Test Driven Development
By
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
Tdd is Dead, Long Live TDD
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
• 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
Tdd is Dead, Long Live TDD
Tdd is Dead, Long Live TDD
Tdd is Dead, Long Live TDD
Tdd is Dead, Long Live TDD
Tdd is Dead, Long Live TDD
Tdd is Dead, Long Live TDD
Tdd is Dead, Long Live TDD
Tdd is Dead, Long Live TDD
Tdd is Dead, Long Live TDD
Tdd is Dead, Long Live TDD
Tdd is Dead, Long Live TDD
Tdd is Dead, Long Live TDD
Tdd is Dead, Long Live TDD
Tdd is Dead, Long Live TDD
Tdd is Dead, Long Live TDD
Tdd is Dead, Long Live TDD
Tdd is Dead, Long Live TDD
Tdd is Dead, Long Live TDD
Tdd is Dead, Long Live TDD
Tdd is Dead, Long Live TDD
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
Remote
API
Your Domain /
Business Logic
Wikipedia
Search
Client
+ searchFor(term)
SearchHistory
+ track(term)
+ findAll() : array
SearchController
+ lookup(term)
HighlightingSearch
+ search(term)
SearchResult
+ highlight()
+ matches(history)
0..*
Collaborators
Client
+ searchFor(term)
SearchHistory
+ track(term)
+ findAll() : array
HighlightingSearch
+ search(term)
SearchResult
+ highlight()
+ matches(history)
Doubles / Mocks
Client
+ searchFor(term)
SearchHistory
+ track(term)
+ findAll() : array
HighlightingSearch
+ search(term)
SearchResult
+ 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 AcmeWiki;
interface SearchHistory
{
public function add(string $term);
public function findAll() : array;
}
namespace AcmeWiki;
interface Client
{
public function searchFor(string $term) : array;
}
namespace AcmeWiki;
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?
Tdd is Dead, Long Live TDD
Tdd is Dead, Long Live TDD
Tdd is Dead, Long Live TDD
Lets build HighlightingSearch
Tdd is Dead, Long Live TDD
Tdd is Dead, Long Live TDD
Tdd is Dead, Long Live TDD
Tdd is Dead, Long Live TDD
Tdd is Dead, Long Live TDD
Tdd is Dead, Long Live TDD
Tdd is Dead, Long Live TDD
Tdd is Dead, Long Live TDD
Tdd is Dead, Long Live TDD
Thank You!

More Related Content

Tdd is Dead, Long Live TDD

Editor's Notes

  1. Old school - write code first then test it works correctly , maybe with browser
  2. Idea of writing test first goes back a long way, but beck *formamalized* it R G R
  3. – First textbook on computer programming
  4. Many themes developed in this book including the ideas developed later by Steve Freeman and Nat Pryce such as “writing tests before code to grow software organically” Growing object oriented software guided by tests
  5. Kent beck – tests in programming are like the ratchet which help you slow down and prevent you from sliding backwards whilst pulling up a heavy load which is likely to slip
  6. The examples trigger abstractions in the design team while keeping the abstractions grounded. You do need more - things like regular conversation, techniques like Domain Driven Design, indeed even doses of Design by Contract.
  7. Specification By Example only works in the context of a working relationship where both sides are collaborating and not fighting.
  8. Rspec 2005 changed the language and consequently the frame of reference.
  9. Rspec book came out in 2010