TestDriven Development TDD in PHP - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

TestDriven Development TDD in PHP

Description:

Helps you design well-factored classes. Single responsibility principle ... Need to reproduce exactly the same environment ... Floating-point round off errors ... – PowerPoint PPT presentation

Number of Views:104
Avg rating:3.0/5.0
Slides: 22
Provided by: andrec150
Category:

less

Transcript and Presenter's Notes

Title: TestDriven Development TDD in PHP


1
Test-DrivenDevelopment (TDD)in PHP
  • Andre John Cruz
  • http//devpinoy.org/blogs/cruizer

2
Test-Driven Development
  • Write tests before code
  • Design methodology, not a test methodology
  • Unit tests
  • Integration tests

3
Why TDD?
  • Repeatable, easy-to-run tests
  • The concept of context
  • Helps you design well-factored classes
  • Single responsibility principle
  • Predictable code
  • Test harness
  • Easy refactoring of code
  • Automated tests

4
The problem with context
  • Need to reproduce exactly the same environment
  • Need to reproduce exactly the same
    sequence/situation
  • Dependencies!
  • browser
  • database
  • sessions and cookies
  • and many more...argh! P

5
TDD and PHP
  • PHP typelessness is great but...
  • TDD helps ensure predictability
  • Would you want to discover that error during
    development, or during production? )?
  • Reduces risks
  • Here's an example...

6
Run me and find out...
  • lt?php
  • a 0.7
  • b 0.1
  • c a b
  • if (c 0.8)
  • echo "It's correct!\n"
  • else
  • echo "It's incorrect. c is not equal to 0.8\n"
  • ?gt

7
Why?!!!
  • Floating-point round off errors
  • These are errors that are so easy to overlook,
    yet can wreak havoc on your production system
  • TDD helps catch these errors by letting you run
    parts of your system in isolation

8
Sample in TDD
  • lt?php
  • require_once '../simpletest/unit_tester.php'
  • require_once '../simpletest/reporter.php'
  • class SimpleExample extends UnitTestCase
  • function testAddition()
  • a 0.7
  • b 0.1
  • c a b
  • this-gtassertEqual(c, 0.8)
  • test new SimpleExample()
  • test-gtrun(new HtmlReporter())
  • ?gt

9
TDD Frameworks for PHP
  • PHPUnit
  • SimpleTest
  • supports both PHP4 and PHP5
  • I like running my tests in the web browser
    because it's colored P

10
Skeleton for SimpleTest
  • extend the UnitTestCase class
  • verify expected values and results using the
    assert methods
  • this is called state testing

11
Red-Green-Refactor
  • TDD prescribes the following steps
  • Write a new test FIRST, resulting in a failing
    program (red)?
  • Write code to pass the test (green)?
  • Remove redundancies and refactor the code as
    necessary
  • repeat )?

12
Test before code?!
  • Intentional programming
  • Ensures that your code is designed to be testable
    in chunks
  • Ensures that your code doesn't have non-tested
    parts
  • Ensures that you feel first-hand how it is to use
    your code )?

13
Demo 1
  • Password complexity checker
  • Takes in a password string as input
  • Optional input minimum required characters
  • Output whether password is acceptable or not

14
Ways to test
  • We've looked at state testing already
  • Let's look at interaction testing

15
Interaction Testing
  • Verifies that the class under test is interacting
    with other classes in expected ways
  • Helpful in testing functionality with external
    dependencies
  • network connections/services
  • database
  • browser
  • files

16
Mock Objects
  • Substitute objects that appear the same but
    actually just observe things
  • Similar to stand-ins used in the movie industry
    )?
  • Allow you to remove external dependencies while
    testing units
  • Can help you verify that certain methods are
    actually called with correct parameters

17
Demo 2
  • Let's use a mock object to test email sending
    functionality
  • SimpleTest has mocking facilities that we can use
    for this
  • Scenario we need to email users whose disk quota
    are 90 utilized.

18
Observations
  • Mindset pretend that your objects are actors and
    that you tell them what to do (pass a message)?
  • State testing is easier than interaction testing
  • Test small before testing big
  • It seems to take longer to do things the TDD way
    than to go straight to the code
  • Did we use a debugger? )?

19
TDD Gotchas
  • TDD does not lead to bug-free code
  • TDD does not mean you do not need QA anymore
  • TDD is mainly about design, not testing
  • some say it's more appropriate to call it
    Behavior Driven Design
  • Use mock objects for quick testing but use the
    real objects for integration testing

20
TDD Pointers
  • Write the test before writing the code
  • Test small
  • Stub out external dependencies into mock objects
  • How quickly do your tests run?
  • Separate unit tests from integration tests

21
Thank You!
  • Links
  • http//devpinoy.org/blogs/cruizer
  • http//www.agilealliance.org
  • http//www.lastcraft.com
  • http//www.phpunit.de
  • http//www.phpugph.com
Write a Comment
User Comments (0)
About PowerShow.com