tdd (with flow3)

31
Inspiring people to share TDD (with FLOW3) Karsten Dambekalns <[email protected]> Robert Lemke <[email protected]>

Upload: karsten-dambekalns

Post on 14-May-2015

1.288 views

Category:

Technology


0 download

DESCRIPTION

An introduction to Test-Driven Development using FLOW given at the TYPO3 University in Annecy, France in May 2008.

TRANSCRIPT

Page 1: TDD (with FLOW3)

Inspiring people toshare

TDD (with FLOW3)Karsten Dambekalns <[email protected]>

Robert Lemke <[email protected]>

Page 2: TDD (with FLOW3)

Inspiring people toshare

What?Why?How?

Integrate!

Page 3: TDD (with FLOW3)

Test first,code later

Page 4: TDD (with FLOW3)

Inspiring people toshare

Test first, code laterWrite tests for all your code

• New features come with a test

• Bugfixes come with a test

Write tests before your code

• Adding a feature means adding a test first

• Fixing a bug means writing a test first

Page 5: TDD (with FLOW3)

Inspiring people toshare

Unit testsUnit tests are small programs that test your code

They check

• the result of methods against expected values

• whether methods are (not) called as expected

A test should

• be well-named

• check only one assertion

Page 6: TDD (with FLOW3)

Inspiring people toshare

Good testsShould be...

• automated

• self-contained

• repeatable

• thorough

• small

• talk about the domain

Page 7: TDD (with FLOW3)

Inspiring people toshare

Unit testsUnit tests are usually run with a test runner

The xUnit family is most widespread

PHPUnit does the job for PHP

Page 8: TDD (with FLOW3)

Tests aren’tthe goal

Page 9: TDD (with FLOW3)

But you said...

Page 10: TDD (with FLOW3)
Page 11: TDD (with FLOW3)

They make you feel good

Page 12: TDD (with FLOW3)

Inspiring people toshare

They make you feel goodThe tests make you

• focus on your task

• code exactly what you need

• think from the outside

• a better programmer*

So what the tests do for you is the key

* decoupling helps with testing, decoupled code is easier to maintain, easier is better - q.e.d.

Page 13: TDD (with FLOW3)

Inspiring people toshare

Reasons for TDDFeel more comfortable

• Build confidence in your code

• Reduce fear of change

(Good) tests are (good) documentation

Acts as safety net

• Regression testing built in

• Helps with refactoring

Page 14: TDD (with FLOW3)

Inspiring people toshare

D for DesignWriting tests first is likely to improve your code

Constant refactoring keeps code clean

Unit testing ensure decoupling

You only code what is really needed

Page 15: TDD (with FLOW3)

Inspiring people toshare

TDD in Practice

Page 16: TDD (with FLOW3)

Inspiring people toshare

The Mantra

Page 17: TDD (with FLOW3)

Inspiring people toshare

The Mantra

Implement

Test Test

Design

Page 18: TDD (with FLOW3)

Inspiring people toshare

More testingSometimes you need to test interaction with external systems, like database access

A test should be small, encapsulated, stand-alone

So how do you test database access?

Page 19: TDD (with FLOW3)

Inspiring people toshare

Mocks & stubsMock objects allow you to use fake objects instead of real implementations

Stubs can be used to return hard-coded results

Using them is actually (somewhat) easy with PHPUnit

Page 20: TDD (with FLOW3)

Inspiring people toshare

Saving in TYPO3CRAssumptions / Background

• The Session object has a save() method

• It uses a Storage_Backend implementation to do the work

• That implementation has methods like addNode(), updateNode() and deleteNode()

You need to make sure those are used when calling save()

Page 21: TDD (with FLOW3)

No FLOW3?No TDD!

Page 22: TDD (with FLOW3)

Inspiring people toshare

No TDD without FLOW3Without Dependency Injection you cannot really do unit testing

Page 23: TDD (with FLOW3)

Inspiring people toshare

DependenciesProblem: Classes explicitly refer to other classes:

Page 24: TDD (with FLOW3)

Inspiring people toshare

DependenciesTry to test this class:

Page 25: TDD (with FLOW3)

Inspiring people toshare

DependenciesUnit Testing: You want to test a small unit

You don't want to test

• The Simple File Logger

• The Card Repository

Page 26: TDD (with FLOW3)

Inspiring people toshare

DependenciesUnit Testing: You want to test a small unit

You want to test

• if the action returns a string representation of the random card it gets from the repository

Page 27: TDD (with FLOW3)

Inspiring people toshare

Dependency InjectionA class doesn't ask for the instance of another class but gets it injected

This methodology is referred to as the "Hollywood Principle":"Don't call us, we'll call you"

Enforces loose coupling and high cohesion

Allows you to mock collaborators

Makes you a better programmer

Page 28: TDD (with FLOW3)

Questions!

Page 29: TDD (with FLOW3)

Inspiring people toshare

Test-Driven Development By ExampleKent Beck, Addison-Wesley

Links & Literature

Continuous Integration – Improving Software Quality and Reducing RiskPaul M. Duvall, Addison-Wesley

xUnit Test Patterns – Refactoring Test CodeGerard Meszaros, Addison-Wesley

Page 30: TDD (with FLOW3)

Inspiring people toshare

Links & LiteratureFLOW3http://flow3.typo3.org/

PHPUnithttp://www.phpunit.de/

Hudson CI-Serverhttps://hudson.dev.java.net/

TYPO3 Forgehttp://forge.typo3.org/

Page 31: TDD (with FLOW3)