Test Driven Development with PHP

4,002 views

Published on

Presentation with an introduction to TDD with PHP at DRI Code Sessions.

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
4,002
On SlideShare
0
From Embeds
0
Number of Embeds
1,286
Actions
Shares
0
Downloads
29
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Test Driven Development with PHP

  1. 1. DRI Code Sessions ● TDD - Test Driven Development Introduction to TDD, Unit Testing and some short examples in PHP with PHPUnit. Content by Rogério Vicente under the Creative Commons License
  2. 2. TDD – What is it? ● Software Development technique ● Short development cycle ● Write tests before any production code ● Write only the minimum amount of code required to pass the tests ● It's great for code documentation
  3. 3. TDD Cycle Image from Wikipedia
  4. 4. Common excuses ● “We have no time to write tests” ● “Testing is boring” ● “My code is good enough” ● “That's not my job, tests are for QA Department” ● “This is just something simple. It's not worth it”
  5. 5. TDD - Pros ● Some studies conclude that TDD raises productivity – less time spent debugging ● Makes programmers focus on interface before implementation ● Provides greater level of confidence in the code ● Can lead to more modularized, flexible and extensible code ● Tests are a source of documentation
  6. 6. TDD - Cons ● Difficult to use with GUI's and code that works with databases ● Badly written tests become a burden to maintain ● Specs misinterpretation leads to wrong tests and wrong code ● Management support is crucial
  7. 7. Unit Testing ● Not TDD ● Part of TDD ● Unit = The smallest testable part of an application
  8. 8. Unit Testing “Whenever you are tempted to type something into a print statement or a debugger expression, write it as a test instead.” Martin Fowler
  9. 9. Unit Testing Frameworks in PHP ● PHPUnit ● SimpleTest
  10. 10. PHPUnit ● http://www.phpunit.de ● PEAR Package ● Created by Sebastian Bergmann ● Port of Junit (Java) ● Regression test framework to implement unit tests in PHP
  11. 11. SimpleTest ● http://www.lastcraft.com/simple_test.php ● Created by Marcus Baker ● Can test web interfaces ● Cute web interface for displaying test results
  12. 12. Code Examples Let's see some code, but first...
  13. 13. Some Cool Game ● Contract: – Player life points ● Is initially 100 ● Cannot become negative ● Cannot be more than 150 ● Interface: – getLife() – setLife($value) – gainLife($value) – loseLife($value)
  14. 14. Code Samples Download available at: http://whit.me/dricodesessionstdd Or fork and improve it: git clone http://github.com/rogeriopvl/dri_codesessions_tdd
  15. 15. References ● http://en.wikipedia.org/wiki/Test_driven_development ● http://en.wikipedia.org/wiki/Unit_testing ● http://www.viddler.com/explore/RoyOsherove/videos/19/ ● http://www.slideshare.net/Skud/test-driven-development-tutoria ● http://artofunittesting.com/ ● http://xunitpatterns.com
  16. 16. The End Questions?

×