TDD With PHP - The Secret of Coding With Confidence
TDD With PHP - The Secret of Coding With Confidence
What is TDD?
Getting into the habit of writing tests before writing code. TDD are continuous short cycle of test, code, refactor. Test cases are in separate les. No more print_r & var_dump in your code! The tests contain assertions that are either true or false. Passing the tests conrms correct behavior of the code.
Tuesday, May 22, 12
What is TDD?
Old way: Write code rst, test if you have time. Often times, you'll be too exhausted (mentally) at the end of coding process to write tests.
What is TDD?
What is TDD?
What is TDD?
What is TDD?
Why TDD?
TDD gives you higher condence that your code is doing what it's suppose to. Small wins makes the coding process more enjoyable (Gamication). Working code every step of the way!
TDD Process
1 Feature specications 2 Write test cases 3 Tests will fail 4 Write code to pass the tests (minimal) 5 Pass all tests 6 Refactor
TDD Process
By focusing on writing only the code necessary to pass tests, designs can be cleaner and clearer than is often achieved by other methods. Resist the urge to pre-optimize the code. Just make it pass rst. Too many asserts might be a sign that your function is doing too many things - break it into smaller chunks. No code is perfect.
Tuesday, May 22, 12
Benets
Better Code Design Modular - If it breaks, it's isolated and easily traceable. Testable units speed up development Update the test case, make the code change, run the tests. Condence in making larger change sets.
Benets
Detect if other parts of your app is affected by the latest code change. Coverage reports help you discover potential weak spots / blind spots in your code. Tests with examples will help in simulating actual use cases in an automated manner. Tests becomes the documentation.
Adoption Strategies
Legacy Codebase Identify critical path, generate skeleton test cases & write test cases. Write tests for new features &/or code that are accessed by the new feature.
Adoption Strategies
New Projects Establish a workow and make the automated test part of your release process.
Adoption Strategies
Developers who write their own tests might have a tendency to take shortcuts by writing conservative test cases. One way to prevent this is to pair-program with another developer or a product owner when preparing the test cases. Spec documents that includes examples can be useful to validate code behavior.
PHPUnit is the de-facto standard for unit testing in PHP projects. It provides both a framework that makes the writing of tests easy as well as the functionality to easily run the tests and analyse their results. http://phpunit.de Created by Sebastian Bergmann
Tuesday, May 22, 12
Demo
Demo Script
Installing PHPUnit. Use existing class to generate tests. Use test cases to generate class le. Coverage Report. Behavior Driven Development.
Installing PHPUnit
# pear config-set auto_discover 1 # pear install pear.phpunit.de/PHPUnit
Sample Code
https://github.com/miccheng/TDD-PHP-Sample
Caveat
It's gonna take some time to get productive Read: Downtime of at least 1-2 days Nothing is full-proof! It's easy to get lured into a false sense of security. Even if u have 100% coverage, it still doesn't prevent unintended usage from breaking the code. Bugs are test cases you have not written yet.
End of File