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

Test-Driven Development that Feels Great

Toby Ho
April 13, 2013

Test-Driven Development that Feels Great

A lot of people view Test-Driven-Development (TDD) as a rigorous practice that takes a lot of self displine. I will dispel this myth by showing how TDD can be fun, as well as give tips and tricks on how to keep your TDD experience fast, flowing, and enjoyable. I will also do a live coding session showing the way I do TDD.

Toby Ho

April 13, 2013
Tweet

More Decks by Toby Ho

Other Decks in Programming

Transcript

  1. Let's TDD it!
    Test-Driven Development that Feels
    Good
    by Toby Ho tobyho.com @airportyh

    View Slide

  2. Toby Ho
    tobyho.com
    @airportyh

    View Slide

  3. Got 'Scripts? Test'em

    View Slide

  4. Our Agenda
    TDD Quick Start
    Why TDD?
    Getting Faster and Better
    Main Event: Man vs Machine!

    View Slide

  5. TDD
    Quick Start!!

    View Slide

  6. How to do TDD
    1. Write a test
    2. See it fail
    3. Write some code
    4. See it pass
    5. Refactor as see fit (optional)
    6. Start again at 1

    View Slide

  7. What A Test Looks Like:
    Jasmine
    d
    e
    s
    c
    r
    i
    b
    e
    ( "
    h
    e
    l
    l
    o t
    e
    s
    t
    "
    , f
    u
    n
    c
    t
    i
    o
    n
    (
    ) {
    i
    t
    (
    '
    a
    d
    d
    s 1 a
    n
    d 2 t
    o g
    e
    t 3
    '
    , f
    u
    n
    c
    t
    i
    o
    n
    (
    )
    {
    e
    x
    p
    e
    c
    t
    (
    a
    d
    d
    (
    1
    , 2
    )
    )
    .
    t
    o
    E
    q
    u
    a
    l
    (
    3
    )
    ;
    }
    )
    ;
    }
    )
    ;

    View Slide

  8. See it in action

    View Slide

  9. Why TDD?

    View Slide

  10. Why TDD?
    Reduced defect rates
    Protection against regression
    Better API designs
    Re-use tests as your specs

    View Slide

  11. I will write unit tests. I will write unit tests. I will write unit tests. I will
    write unit tests. I will write unit tests. I will write unit tests.I will write
    unit tests. I will write unit tests. I will write unit tests. I will write unit
    tests. I will write unit tests. I will write unit tests. I will write unit
    tests. I will write unit tests. I will write unit tests. I will write unit
    tests. I will write unit tests. I will write unit tests. I will write unit
    tests. I will write unit tests. I will write unit tests. I will write unit
    tests. I will write unit tests. I will write unit tests. I will write unit
    tests. I will write unit tests. I will write unit tests. I will write unit
    tests. I will write unit tests. I will write unit tests. I will write unit
    tests. I will write unit tests. I will write unit tests. I will write unit
    tests. I will write unit tests. I will write unit tests. I will write unit
    tests. I will write unit tests. I will write unit tests. I will write unit
    tests.

    View Slide

  12. View Slide

  13. “Pragmatism: do the right
    thing, if it's easy.”

    View Slide

  14. View Slide

  15. View Slide

  16. Pain

    View Slide

  17. View Slide

  18. Poor Feedback

    View Slide

  19. Good Feedback

    View Slide

  20. Getting Good
    Feedback

    View Slide

  21. Good Feedback 1:
    Workflow

    View Slide

  22. Workflow Tips
    1. Don't use slow software
    2. Autorun your tests!
    3. Don't switch windows, tile'em
    4. Typing fast helps!

    View Slide

  23. Good Feedback
    2: Fast Tests

    View Slide

  24. < 1 Second
    Whatever it takes

    View Slide

  25. Write Lots of Fast Tests,
    Little to No Slow Tests

    View Slide

  26. Run Test Subsets

    View Slide

  27. Run Individual Test Suite
    (Mocha)
    The mocha unit testing framework has this great feature called
    exclusive tests, where if you mark a certain test suite as the "only"
    test suite, Mocha then will run only this one.
    d
    e
    s
    c
    r
    i
    b
    e
    .
    o
    n
    l
    y
    (
    '
    m
    y t
    e
    s
    t s
    u
    i
    t
    e
    '
    , f
    u
    n
    c
    t
    i
    o
    n
    (
    )
    {
    .
    .
    .
    }
    )

    View Slide

  28. Run Individual Test Case
    (Mocha)
    i
    t
    .
    o
    n
    l
    y
    (
    '
    m
    y t
    e
    s
    t c
    a
    s
    e
    '
    , f
    u
    n
    c
    t
    i
    o
    n
    (
    )
    {
    .
    .
    .
    }
    )

    View Slide

  29. .
    o
    n
    l
    y
    for
    Jasmine?

    View Slide

  30. Good Feedback
    3. Take Small
    Steps

    View Slide

  31. Smaller Steps ->
    More Frequent
    Test Runs

    View Slide

  32. How Small Steps Helps
    Debugging
    Context of what you changed
    Easy undo back to last green point

    View Slide

  33. Small Steps is a
    skill

    View Slide

  34. Rules of Thumb
    1. Do one thing at a time.
    2. Always work on passing one test at a
    time.
    3. Don't fight the wall of red head on.

    View Slide

  35. Test Patterns

    View Slide

  36. View Slide

  37. Small Step Test Patterns
    1. Child tests
    2. Fake it 'til you make it
    3. One to many
    4. Isolated change
    5. Back up and refactor

    View Slide

  38. Main Event!!!
    vs

    View Slide

  39. Thank You!
    Any Questions?
    Toby Ho
    tobyho.com
    @airportyh

    View Slide

  40. References
    Jasmine (pivotal.github.io/jasmine)
    Sinon.js (sinonjs.org)
    Test'em (github.com/airportyh/testem)
    Mocha (visionmedia.github.io/mocha/)
    describe.only for Mocha
    (visionmedia.github.io/mocha/#exclusive
    tests)
    describe.only for Jasmine
    (github.com/pivotal/jasmine/pull/309)

    View Slide