TDD with PHP - the secret of coding with confidence

Embed Size (px)

Citation preview

  • 7/31/2019 TDD with PHP - the secret of coding with confidence

    1/27

    The secret to coding with confidenceTest Driven Development

    Tuesday, May 22, 12

  • 7/31/2019 TDD with PHP - the secret of coding with confidence

    2/27

    Tuesday, May 22, 12

  • 7/31/2019 TDD with PHP - the secret of coding with confidence

    3/27

    Tuesday, May 22, 12

  • 7/31/2019 TDD with PHP - the secret of coding with confidence

    4/27

    Tuesday, May 22, 12

  • 7/31/2019 TDD with PHP - the secret of coding with confidence

    5/27

    What is TDD? Getting into the habit of writing tests before

    writing code.

    TDD are continuousshort 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.Tuesday, May 22, 12

  • 7/31/2019 TDD with PHP - the secret of coding with confidence

    6/27

    What is TDD? Old way:

    Write code first, test if you have time.

    Often times, you'll be too exhausted (mentally)

    at the end of coding process to write tests.

    Tuesday, May 22, 12

  • 7/31/2019 TDD with PHP - the secret of coding with confidence

    7/27

    What is TDD?

    Tuesday, May 22, 12

  • 7/31/2019 TDD with PHP - the secret of coding with confidence

    8/27

    What is TDD?

    Tuesday, May 22, 12

  • 7/31/2019 TDD with PHP - the secret of coding with confidence

    9/27

    What is TDD?

    Tuesday, May 22, 12

  • 7/31/2019 TDD with PHP - the secret of coding with confidence

    10/27

    What is TDD?

    Tuesday, May 22, 12

  • 7/31/2019 TDD with PHP - the secret of coding with confidence

    11/27Tuesday, May 22, 12

  • 7/31/2019 TDD with PHP - the secret of coding with confidence

    12/27

    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!

    Tuesday, May 22, 12

  • 7/31/2019 TDD with PHP - the secret of coding with confidence

    13/27

    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

    Tuesday, May 22, 12

  • 7/31/2019 TDD with PHP - the secret of coding with confidence

    14/27

    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 first.

    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

  • 7/31/2019 TDD with PHP - the secret of coding with confidence

    15/27

  • 7/31/2019 TDD with PHP - the secret of coding with confidence

    16/27

    Benefits

    Detect if other parts of your app is affected bythe latest code change.

    Coverage reports help you discover potentialweak spots / blind spots in your code.

    Tests with examples will help in simulating

    actual use cases in an automated manner.

    Tests becomes the documentation.

    Tuesday, May 22, 12

  • 7/31/2019 TDD with PHP - the secret of coding with confidence

    17/27

    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.

    Tuesday, May 22, 12

  • 7/31/2019 TDD with PHP - the secret of coding with confidence

    18/27

    Adoption Strategies New Projects

    Establish a workflow and make the

    automated test part of your release process.

    Tuesday, May 22, 12

  • 7/31/2019 TDD with PHP - the secret of coding with confidence

    19/27

    Adoption Strategies

    Developers who write their own tests mighthave a tendency to take shortcuts by writingconservative test cases.

    One way to prevent this is to pair-program withanother developer or a product owner whenpreparing the test cases.

    Spec documents that includes examples canbe useful to validate code behavior.

    Tuesday, May 22, 12

  • 7/31/2019 TDD with PHP - the secret of coding with confidence

    20/27

    PHPUnit is the de-facto standard for unit

    testing in PHP projects. It provides both aframework 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

    http://phpunit.de/http://phpunit.de/http://phpunit.de/http://phpunit.de/
  • 7/31/2019 TDD with PHP - the secret of coding with confidence

    21/27

    Demo

    Tuesday, May 22, 12

  • 7/31/2019 TDD with PHP - the secret of coding with confidence

    22/27

    Demo Script

    Installing PHPUnit.

    Use existing class to generate tests.

    Use test cases to generate class file.

    Coverage Report.

    Behavior Driven Development.

    Tuesday, May 22, 12

  • 7/31/2019 TDD with PHP - the secret of coding with confidence

    23/27

    Installing PHPUnit

    # pear config-set auto_discover 1

    # pear install pear.phpunit.de/PHPUnit

    Tuesday, May 22, 12

  • 7/31/2019 TDD with PHP - the secret of coding with confidence

    24/27

    Sample Code

    https://github.com/miccheng/TDD-PHP-Sample

    Tuesday, May 22, 12

    https://github.com/miccheng/TDD-PHP-Samplehttps://github.com/miccheng/TDD-PHP-Sample
  • 7/31/2019 TDD with PHP - the secret of coding with confidence

    25/27

    Behavior Driven

    Development (BDD) Not just testing individual units, but testing the

    codes as a user would use your app (ie. how

    diff parts of your code work together).

    Process is the same, just that the tests are

    story-based.

    User stories = Specs with examples

    Tuesday, May 22, 12

  • 7/31/2019 TDD with PHP - the secret of coding with confidence

    26/27

    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'tprevent unintended usage from breaking the code.

    Bugs are test cases you have not written yet.

    Tuesday, May 22, 12

  • 7/31/2019 TDD with PHP - the secret of coding with confidence

    27/27

    End of File