Practical TestDriven Development in PHP - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

Practical TestDriven Development in PHP

Description:

extend the UnitTestCase class. verify expected behavior using the assert methods ... does not mean you do not need QA anymore. TDD is mainly about design, not ... – PowerPoint PPT presentation

Number of Views:28
Avg rating:3.0/5.0
Slides: 23
Provided by: andrec150
Category:

less

Transcript and Presenter's Notes

Title: Practical TestDriven Development in PHP


1
Practical Test-DrivenDevelopmentin 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
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...

5
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

6
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

7
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

8
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

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 behavior 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

13
Demo 1
  • Create an authentication system
  • User enters username and password
  • Three consecutive wrong passwords result in a
    locked account
  • Correct login resets the count
  • Any subsequent attempt to log in using a locked
    account should trigger an email to the system
    administrator

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

18
Web Page Testing
  • SimpleTest comes with an object that mimics a web
    browser
  • Verify that we actually have a user login page
    and that it is working
  • This is actually a form of integration testing
  • On to demo 3...

19
Demo 3
  • Let's use SimpleTest to verify the actual web
    pages, not just the underlying classes

20
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

21
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

22
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