TDD FlowTDD Flow
@dionatanmoura
The Mantra in ActionThe Mantra in Action
TDD Flow
The Mantra in Action
● eXtreme Programming
● Test-Driven Development (TDD)
● Mantra
● TDD Patterns
– Red patterns
– Green patterns
– Refactoring patterns
● Hands-on
https://5com1001-0912.wikispaces.com/
http://commons.wikimedia.org/wiki/File:Creador_de_la_metodologia_xp.jpg
“Test-Driven Development (TDD) is a style of
development that drives development with tests”
Kent Beck
TDD is the shortest cycleTDD is the shortest cycle
in agile developmentin agile development
TDD Rules
1. Write new code only if you first
have a failing automated test
2. Eliminate duplication
Write a small automated
test that fails
Red
Code just enough to
make the test pass
GreenRemove duplication and
refactor to clean code
Refactor
The Mantra in Action!
TDD = TFD + Refactoring;
TFD
Test
First
Development
TDD = TFD + Refactoring;
Refactoring
To restructure
software by applying a
series of refactorings
without changing its
observable behavior.
Martin Fowler
http://martinfowler.com
TDD = TFD + Refactoring;
Refactoring for TDD
Observable Behavior
==
Tests
“The goal of TDD is clean code that works.”
Ron Jeffries
https://twitter.com/RonJeffries
Clean Code
http://programmer.97things.oreilly.com/wiki/index.php/Uncle_Bob
SmallSmall
StepsSteps
https://thisbucketlist.wordpress.com/2012/06/01/small-steps-will-take-you-far-eventually/
Remember...
TDD Patterns
TDD Patterns
Test n.
Test for TDD is a noum (n.) which means a
procedure to acceptance (or not)
that runs automatically
TDD Patterns
Test List
What should I test?
Write a list of all known tests
that will be needed to be implemented.
TDD Patterns
Test First
When should I write a test?
Before I code.
TDD Patterns
Assert First
When should I write the assert?
First.
TDD Patterns
Test Data
How do I represent the intent of
the data?
With a meaningful relationship between the expect and the
actual result.
Red Bar Patterns
Red Bar Patterns
One Step Test
Which test should I pick from
the list?
Learning X Confidence
Red Bar Patterns
Starter Test
Which test should I start?
By a variation of the operation that doesn’t do
anything, returning a constant.
Outside-in (skeleton)
Red Bar Patterns
Explanation Test
How to spread test automation?
Ask for and give explanations in terms of tests.
Red Bar Patterns
Learning Test
How to learn from externally
produced software?
Write a test before use that software.
Red Bar Patterns
Another Test
How to discuss something off
topic?
Add a test to the list,
and go back to the current test.
Red Bar Patterns
Regression Test
What to do with a reported bug?
Write a small test that fails that bug.
Red Bar Patterns
Break
What to do when I feel tired or
stuck?
Take a break.
Red Bar Patterns
Do over
What to do when I feel lost?
Throw away the code and start over.
Red Bar Patterns
Cheap desk, Nice chair
What is the physical setup for TDD?
A really nice chair mostly.
Testing Patterns
Testing Patterns
Child Test
How to get a too big test case running?
Write the smallest test case that represents the
broken part of the bigger test case, and make it run.
Go back to the big test case.
Testing Patterns
Mock Object
How to test something that relies on an
expensive or complicated resource?
Create a fake version of the resource.
Testing Patterns
Crash Test Dummy
How to test error codes difficult to be
invoked?
Create a fake object for that.
Testing Patterns
Broken Test
How to leave a programming session (alone)?
Leave the last test broken.
Testing Patterns
Clean Check-in
How to leave a programming session (in pair
or in a team)?
Leave all the tests running.
Green Bar Patterns
Green Bar Patterns
Fake It (‘til you make it)
What is the first implementation to make a
test pass?
Return a constant.
Green Bar Patterns
Triangulate
How to drive abstraction?
Abstract when you have two or more examples.
Green Bar Patterns
Obvious Implementation
How should I implement simple
operations?
Just implement them.
Green Bar Patterns
One to Many
How to implement with collections?
Implement first without collections. Then make it work with
collections.
Extract
Inline
Introduce
Move
Pull up
Push Down
Remove
Replace
http://refactoring.com/catalog/
Class
Field
Interface
Method
Object
Parameter
Subclass
Superclass
Refactoring Patterns
Design Patterns
xUnit Patterns
Assertion
Boolean expressions with assert();
Fixture
Common objects for the tests with setUp();
External Fixture
Release external resources with tearDown();
Test Method
Represents a test case
Exception Test
Fail(); if the exception is not thrown
AllTests
Suite of all suites
However don’t forget...
JUnit Test Infected:
Programmers Love Writing Tests
http://junit.sourceforge.net/doc/testinfected/testing.htm
Is TDD only about
Unit Testing?
Is TDD a
testing technique?
“The focus of TDD is design.
Test is a (nice) side effect.”
Marco Trincardi
Is TDD Dead?
http://martinfowler.com/articles/is-tdd-dead/
RIP TDD (ironic)
https://www.facebook.com/notes/kent-beck/rip-tdd/750840194948847/
“To me the essence of the
question I think about that,
thinking about software design, I
don't care about TDD or not TDD.”
Kent Beck, Is TDD dead? Part II.
Does TDD lead you to a
good design?
"TDD isn't taking you anywhere.
You're making the decisions."
Kent Beck, Is TDD dead? Part II.
How to learn (more) TDD?
CraftsmanshipCraftsmanship
http://www.houshamadyan.org
PracticingPracticing
http://en.wikipedia.org/wiki/Dojo
Kata: TroLolo
(FizzBuzz)
Kata: DSL, Java
and ANTLR
Kata: Testing JavaKata: Testing Java
Web ApplicationsWeb Applications
outside the containeroutside the container
Kata: In-ContainerKata: In-Container
Testing Java withTesting Java with
ArquillianArquillian
Kata: RomanKata: Roman
NumbersNumbers
“Worried that TDD will slow down
your programmers? Don't. They
probably need slowing down.”
J. B. Rainsberger
Remember again...
http://blog.gravypower.net/
TDD FlowTDD Flow
@dionatanmoura
The Mantra in ActionThe Mantra in Action

TDD Flow: The Mantra in Action