Download - Test-Driven Development with FLOW3
![Page 2: Test-Driven Development with FLOW3](https://reader033.vdocuments.us/reader033/viewer/2022052410/555491c7b4c905186d8b4822/html5/thumbnails/2.jpg)
Inspiring people toshare
What?
Why?
How?
![Page 3: Test-Driven Development with FLOW3](https://reader033.vdocuments.us/reader033/viewer/2022052410/555491c7b4c905186d8b4822/html5/thumbnails/3.jpg)
Test first,code later
![Page 4: Test-Driven Development with FLOW3](https://reader033.vdocuments.us/reader033/viewer/2022052410/555491c7b4c905186d8b4822/html5/thumbnails/4.jpg)
Inspiring people toshare
TDD is about TestsWrite tests for all your code
• New features come with a test
• Bugfixes come with a test
![Page 5: Test-Driven Development with FLOW3](https://reader033.vdocuments.us/reader033/viewer/2022052410/555491c7b4c905186d8b4822/html5/thumbnails/5.jpg)
Inspiring people toshare
TDD is about TestsWrite 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 6: Test-Driven Development with FLOW3](https://reader033.vdocuments.us/reader033/viewer/2022052410/555491c7b4c905186d8b4822/html5/thumbnails/6.jpg)
Inspiring people toshare
The TDD Mantra
Design
![Page 7: Test-Driven Development with FLOW3](https://reader033.vdocuments.us/reader033/viewer/2022052410/555491c7b4c905186d8b4822/html5/thumbnails/7.jpg)
Inspiring people toshare
The TDD Mantra
Test fails
Design
![Page 8: Test-Driven Development with FLOW3](https://reader033.vdocuments.us/reader033/viewer/2022052410/555491c7b4c905186d8b4822/html5/thumbnails/8.jpg)
Inspiring people toshare
The TDD Mantra
Implement
Test fails
Design
![Page 9: Test-Driven Development with FLOW3](https://reader033.vdocuments.us/reader033/viewer/2022052410/555491c7b4c905186d8b4822/html5/thumbnails/9.jpg)
Inspiring people toshare
The TDD Mantra
Implement
Test passes Test fails
Design
![Page 10: Test-Driven Development with FLOW3](https://reader033.vdocuments.us/reader033/viewer/2022052410/555491c7b4c905186d8b4822/html5/thumbnails/10.jpg)
Inspiring people toshare
The TDD Mantra
Implement
Test passes Test fails
Design
![Page 11: Test-Driven Development with FLOW3](https://reader033.vdocuments.us/reader033/viewer/2022052410/555491c7b4c905186d8b4822/html5/thumbnails/11.jpg)
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 12: Test-Driven Development with FLOW3](https://reader033.vdocuments.us/reader033/viewer/2022052410/555491c7b4c905186d8b4822/html5/thumbnails/12.jpg)
Inspiring people toshare
Good testsShould be...
• automated
• self-contained
• repeatable
• thorough
• small
• talk about the domain
![Page 13: Test-Driven Development with FLOW3](https://reader033.vdocuments.us/reader033/viewer/2022052410/555491c7b4c905186d8b4822/html5/thumbnails/13.jpg)
Inspiring people toshare
Running unit testsUnit tests are usually run with a test runner
The xUnit family is most widespread
PHPUnit does the job for PHP
FLOW3 has a Testing package
• acts as test runner
• provides helpful environment
• web-based and CLI tool
![Page 14: Test-Driven Development with FLOW3](https://reader033.vdocuments.us/reader033/viewer/2022052410/555491c7b4c905186d8b4822/html5/thumbnails/14.jpg)
Inspiring people toshare
Running unit testsUnit tests are usually run with a test runner
The xUnit family is most widespread
PHPUnit does the job for PHP
FLOW3 has a Testing package
• acts as test runner
• provides helpful environment
• web-based and CLI tool
![Page 15: Test-Driven Development with FLOW3](https://reader033.vdocuments.us/reader033/viewer/2022052410/555491c7b4c905186d8b4822/html5/thumbnails/15.jpg)
Inspiring people toshare
Running unit testsUnit tests are usually run with a test runner
The xUnit family is most widespread
PHPUnit does the job for PHP
FLOW3 has a Testing package
• acts as test runner
• provides helpful environment
• web-based and CLI tool
![Page 16: Test-Driven Development with FLOW3](https://reader033.vdocuments.us/reader033/viewer/2022052410/555491c7b4c905186d8b4822/html5/thumbnails/16.jpg)
Inspiring people toshare
Running unit testsUnit tests are usually run with a test runner
The xUnit family is most widespread
PHPUnit does the job for PHP
FLOW3 has a Testing package
• acts as test runner
• provides helpful environment
• web-based and CLI tool
![Page 17: Test-Driven Development with FLOW3](https://reader033.vdocuments.us/reader033/viewer/2022052410/555491c7b4c905186d8b4822/html5/thumbnails/17.jpg)
Tests aren’tthe goal
![Page 18: Test-Driven Development with FLOW3](https://reader033.vdocuments.us/reader033/viewer/2022052410/555491c7b4c905186d8b4822/html5/thumbnails/18.jpg)
![Page 19: Test-Driven Development with FLOW3](https://reader033.vdocuments.us/reader033/viewer/2022052410/555491c7b4c905186d8b4822/html5/thumbnails/19.jpg)
They make you feel good
![Page 20: Test-Driven Development with FLOW3](https://reader033.vdocuments.us/reader033/viewer/2022052410/555491c7b4c905186d8b4822/html5/thumbnails/20.jpg)
Inspiring people toshare
They make you feel goodWhat the tests do for you is the key
The tests make you
• focus on your task
• code exactly what you need
• think from the outside
• a better programmer*
![Page 21: Test-Driven Development with FLOW3](https://reader033.vdocuments.us/reader033/viewer/2022052410/555491c7b4c905186d8b4822/html5/thumbnails/21.jpg)
Inspiring people toshare
* decoupling helps with testing, decoupled code is easier to maintain, easier is better - q.e.d.
They make you feel goodWhat the tests do for you is the key
The tests make you
• focus on your task
• code exactly what you need
• think from the outside
• a better programmer*
![Page 22: Test-Driven Development with FLOW3](https://reader033.vdocuments.us/reader033/viewer/2022052410/555491c7b4c905186d8b4822/html5/thumbnails/22.jpg)
Inspiring people toshare
Use TDD to...Feel more comfortable
• Build confidence in your code
• Reduce fear of change
Have a safety net
• Regression testing built in
• Helps with refactoring
Provide (good) documentation through (good) tests
![Page 23: Test-Driven Development with FLOW3](https://reader033.vdocuments.us/reader033/viewer/2022052410/555491c7b4c905186d8b4822/html5/thumbnails/23.jpg)
Inspiring people toshare
D for Development
![Page 24: Test-Driven Development with FLOW3](https://reader033.vdocuments.us/reader033/viewer/2022052410/555491c7b4c905186d8b4822/html5/thumbnails/24.jpg)
Inspiring people toshare
D for Design
![Page 25: Test-Driven Development with FLOW3](https://reader033.vdocuments.us/reader033/viewer/2022052410/555491c7b4c905186d8b4822/html5/thumbnails/25.jpg)
Inspiring people toshare
D for DesignWriting tests first is likely to improve your code
• You use the API before you code
• You make the API fit your needs
Unit testing ensures decoupling
You only code what is really needed
Constant refactoring keeps code clean
![Page 26: Test-Driven Development with FLOW3](https://reader033.vdocuments.us/reader033/viewer/2022052410/555491c7b4c905186d8b4822/html5/thumbnails/26.jpg)
Inspiring people toshare
TDD in PracticeWrite down your next goal
Now write a test to check this
The test will fail, it might even break with a fatal error
Now write the code you need to write
If you test again, the test should now pass
Iterate – until it passes or all features are in
![Page 27: Test-Driven Development with FLOW3](https://reader033.vdocuments.us/reader033/viewer/2022052410/555491c7b4c905186d8b4822/html5/thumbnails/27.jpg)
Inspiring people toshare
No FLOW3?No TDD!
![Page 28: Test-Driven Development with FLOW3](https://reader033.vdocuments.us/reader033/viewer/2022052410/555491c7b4c905186d8b4822/html5/thumbnails/28.jpg)
Inspiring people toshare
DependenciesClasses explicitly refer to other classes
But you want to test a small unit
You don't want to test
• The Steamer
• The Grinder
You want to test
• if the Barista uses the right amount of coffee and the requested milk when asked for coffee
![Page 29: Test-Driven Development with FLOW3](https://reader033.vdocuments.us/reader033/viewer/2022052410/555491c7b4c905186d8b4822/html5/thumbnails/29.jpg)
Inspiring people toshare
class Barista {
/** * Makes a drink * * @param string $drink */ public function make($drink) { if ($drink === 'Tea') { throw new F3::Demo::Exception::NotAvailable(
'We don\'t serve no tea, Sir', 1223385110); }
$coffeeGrinder = new Grinder(); $steamer = new Steamer();
$coffee = $coffeeGrinder->grind(9, Grinder::FINE); $milk = $steamer->getSteamedMilk(Steamer::FAT_LOW); }}
Dependencies – bad
![Page 30: Test-Driven Development with FLOW3](https://reader033.vdocuments.us/reader033/viewer/2022052410/555491c7b4c905186d8b4822/html5/thumbnails/30.jpg)
Inspiring people toshare
Dependency InjectionThis methodology is referred to as the "Hollywood Principle":"Don't call us, we'll call you"
A class doesn't ask for the instance of another class but gets it injected
Enforces loose coupling and high cohesion
Allows you to mock collaborators
Makes you a better programmer
![Page 31: Test-Driven Development with FLOW3](https://reader033.vdocuments.us/reader033/viewer/2022052410/555491c7b4c905186d8b4822/html5/thumbnails/31.jpg)
Inspiring people toshare
class Barista {
/** * @var F3::Demo::Grinder */ protected $grinder;
/** * @var F3::Demo::Steamer */ protected $steamer;
/** * * @param Grinder $grinder * @param Steamer $steamer */ public function __construct(F3::Demo::Grinder $grinder, F3::Demo::Steamer $steamer) { $this->grinder = $grinder; $this->steamer = $steamer; }
Dependencies – good
![Page 32: Test-Driven Development with FLOW3](https://reader033.vdocuments.us/reader033/viewer/2022052410/555491c7b4c905186d8b4822/html5/thumbnails/32.jpg)
Inspiring people toshare
/** * Makes a drink * * @param string $drink */ public function make($drink) { if ($drink === 'Tea') { throw new F3::Demo::Exception::NotAvailable(
'We don\'t serve no tea, Sir', 1223385110); }
$coffee = $this->grinder->grind(9, Grinder::FINE); $milk = $this->steamer->getSteamedMilk(Steamer::FAT_LOW); }
Dependencies – good
![Page 33: Test-Driven Development with FLOW3](https://reader033.vdocuments.us/reader033/viewer/2022052410/555491c7b4c905186d8b4822/html5/thumbnails/33.jpg)
Inspiring people toshare
Without Dependency Injection you cannot
do unit testing
![Page 34: Test-Driven Development with FLOW3](https://reader033.vdocuments.us/reader033/viewer/2022052410/555491c7b4c905186d8b4822/html5/thumbnails/34.jpg)
Inspiring people toshare
So, what toinject?
![Page 35: Test-Driven Development with FLOW3](https://reader033.vdocuments.us/reader033/viewer/2022052410/555491c7b4c905186d8b4822/html5/thumbnails/35.jpg)
Inspiring people toshare
Mocks & StubsSometimes you need to test interaction with external systems, like milk steamers
A test should be small, encapsulated, stand-alone
Mock objects allow you to use fake objects
Stubs can be used to return hard-coded results
Using them is actually (somewhat) easy with PHPUnit
![Page 36: Test-Driven Development with FLOW3](https://reader033.vdocuments.us/reader033/viewer/2022052410/555491c7b4c905186d8b4822/html5/thumbnails/36.jpg)
Inspiring people toshare
FLOW3 + TDD = FUNFLOW3 makes Dependency Injection easy
With Dependency Injection you can do proper unit testing
Proper unit tests
• help produce reliable code
• make refactoring possible
• make you feel better
All this means more fun while coding
![Page 37: Test-Driven Development with FLOW3](https://reader033.vdocuments.us/reader033/viewer/2022052410/555491c7b4c905186d8b4822/html5/thumbnails/37.jpg)
Inspiring people toshare
Questions!
![Page 38: Test-Driven Development with FLOW3](https://reader033.vdocuments.us/reader033/viewer/2022052410/555491c7b4c905186d8b4822/html5/thumbnails/38.jpg)
Inspiring people toshare
Test-Driven Development By ExampleKent Beck, Addison-Wesley
Literature
Continuous Integration – Improving Software Quality and Reducing RiskPaul M. Duvall, Addison-Wesley
xUnit Test Patterns – Refactoring Test CodeGerard Meszaros, Addison-Wesley
![Page 39: Test-Driven Development with FLOW3](https://reader033.vdocuments.us/reader033/viewer/2022052410/555491c7b4c905186d8b4822/html5/thumbnails/39.jpg)
Inspiring people toshare
LinksFLOW3http://flow3.typo3.org/
PHPUnithttp://www.phpunit.de/
TDD in Wikipediahttp://en.wikipedia.org/wiki/Test-driven_development
![Page 40: Test-Driven Development with FLOW3](https://reader033.vdocuments.us/reader033/viewer/2022052410/555491c7b4c905186d8b4822/html5/thumbnails/40.jpg)