Why are people resistant to Test Driven Development? I would suggest that this is partly due to a misunderstanding. TDD appears (on the face of it) to require one to test something before it is created, which sounds logically backwards to most people. And that's because it is.
But TDD isn't about testing.
In order to clear up this misunderstanding, there's a linguistic trick we can use. One which completely sidesteps the logical fallacy and completely reframes the activity. Replace the word 'test' with 'specification' and suddenly - it all makes sense. Writing a specification for your class before writing the code is far less jarring, cognitively speaking.
4. [test] noun
A procedure intended to establish the
quality, performance, or reliability of
something, especially before it is taken
into widespread use.
5. [test] verb
To do something in order to discover if
something is safe, works correctly, etc.,
or if something is present
13. 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.
23. 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
24. [specification] noun
a detailed description or assessment
of requirements, dimensions,
materials, etc., as of a proposed
building, machine, bridge, etc.
28. Did you write it
before the
code?
It’s a Test
It’s a
Specification
YesNo
29. Changing the way of thinking about the same
problem by changing the language used to
describe it
30. 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
31. RSpec – 2005 –
Making TDD Productive and
Started as an experiment by Steven Baker
Fun
32. Don’t rush ahead with
more code.
Instead, add another
example and let it guide you
to what you have to do next.
33. … 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
34. 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
37. • 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
38. • 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
63. 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)
64. 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?
65. Focus on one Problem
- Ignore the infrastructure
- Ignore the framework
- What’s left?
78. 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
79. 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
80. 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
81. 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?
Old school - write code first then test it works correctly , maybe with browser
Idea of writing test first goes back a long way, but beck *formamalized* it R G R
– First textbook on computer programming
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
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
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.
Specification By Example only works in the context of a working relationship where both sides are collaborating and not fighting.
Rspec 2005 changed the language and consequently the frame of reference.