Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Test Drive Development with WordPress

Michael Cheng
September 05, 2012

Test Drive Development with WordPress

How to go about doing TDD with WordPress

Michael Cheng

September 05, 2012
Tweet

More Decks by Michael Cheng

Other Decks in Programming

Transcript

  1. The secret to coding with confidence
    Test Driven Development
    with WordPress
    1

    View Slide

  2. 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 files. No more print_r &
    var_dump in your code!
    • The tests contain assertions that are either true or
    false. Passing the tests confirms correct behavior
    of the code.
    2

    View Slide

  3. 3

    View Slide

  4. Why TDD?
    • TDD gives you higher confidence that your
    code is doing what it's suppose to.
    • Small wins makes the coding process more
    enjoyable (Gamification).
    • Working code every step of the way!
    4

    View Slide

  5. TDD Process
    1 Feature
    specifications
    2 Write test cases
    3 Tests will fail
    4 Write code to pass
    the tests (minimal)
    5 Pass all tests
    6 Refactor
    "Make it green, then make it clean"
    5

    View Slide

  6. • 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
    6

    View Slide

  7. WordPress Unit Tests
    • Core libraries unit tests
    • http://unit-tests.trac.wordpress.org/wiki
    • Plugins Development
    • Nikolay Bachiyski (https://github.com/nb/wordpress-
    tests)
    • Themes Development
    • http://codex.wordpress.org/Theme_Unit_Test
    7

    View Slide

  8. WordPress Unit Tests
    • Nikolay Bachiyski
    • https://github.com/nb/
    wordpress-tests
    • http://wordpress.tv/
    2011/08/20/nikolay-
    bachiyski-unit-testing-will-
    change-your-life/
    8

    View Slide

  9. Demo
    9

    View Slide

  10. Demo Script
    • Installing PHPUnit.
    • Checkout unit tests
    • Sample Unit Test for a WP Plugin.
    10

    View Slide

  11. Installing PHPUnit
    # pear config-set auto_discover 1
    # pear install pear.phpunit.de/PHPUnit
    11

    View Slide

  12. Sample Code
    https://github.com/miccheng/WordPress-TDD
    12

    View Slide

  13. End of File
    13

    View Slide