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

Unit Testing: What? Why? How?

Unit Testing: What? Why? How?

Slides from my talk at WP Hooked, February 2017.

https://www.meetup.com/wp_hooked/

John Blackbourn

February 14, 2017
Tweet

More Decks by John Blackbourn

Other Decks in Technology

Transcript

  1. Unit Testing:
    What? Why? How?

    View Slide

  2. John Blackbourn
    WordPress core developer
    Working with WordPress for 10+ years
    Senior Engineer at Human Made
    @johnbillion

    View Slide

  3. Unit Testing:
    What? Why? How?

    View Slide

  4. You’re Going to
    L VE
    Unit Testing

    View Slide

  5. That’s Enough
    of That

    View Slide

  6. Unit Testing:
    What? Why? How?

    View Slide

  7. A unit test is a piece of code
    which exists only to verify expectations
    about the behaviour of another piece of code
    Verification

    View Slide

  8. a.k.a.
    “Does my function do
    what I expect it to do?”

    View Slide

  9. A unit test operates in isolation
    in order to avoid external interference
    Isolated

    View Slide

  10. Tests typically run on the command line
    CLI

    View Slide

  11. Tests are highly efficient
    because they're quickly repeatable
    Automated

    View Slide

  12. Crossing your fingers
    and hoping…
    is not a unit test

    View Slide

  13. Unit Testing:
    What? Why? How?

    View Slide

  14. A unit test can cover many scenarios
    and never misses anything
    Accuracy

    View Slide

  15. Unit tests give you increased assurance
    that changes are less likely to cause breakage
    Assurance

    View Slide

  16. Unit tests give you
    confidence to make changes
    Confidence

    View Slide

  17. Unit tests help you write better code
    Smaller functions
    Separation of concerns
    Quality

    View Slide

  18. Unit tests can be liberating
    Clarity

    View Slide

  19. Unit Testing:
    What? Why? How?

    View Slide

  20. View Slide

  21. View Slide

  22. View Slide

  23. View Slide

  24. “But I know that false isn’t true”

    View Slide

  25. View Slide

  26. Arrange
    Act
    Assert
    AAA

    View Slide

  27. Each time a WordPress test runs,
    it operates on a fresh installation
    Remember

    View Slide

  28. View Slide

  29. View Slide

  30. View Slide

  31. View Slide

  32. Accuracy
    Assurance
    Confidence
    Quality?…
    The Result

    View Slide

  33. Assertions
    assertTrue( $actual )
    assertFalse( $actual )
    assertEquals( $expected, $actual )
    assertCount( $expected, $actual )
    assertObjectHasAttribute( $key, $actual )
    assertArrayHasKey( $key, $actual )
    assertWPError( $actual )
    assertCanonical( $actual, $expected )

    View Slide

  34. a.k.a.
    A Real World Gotcha
    Type Juggling Kills Me

    View Slide

  35. View Slide

  36. View Slide

  37. WUT

    View Slide

  38. View Slide

  39. Provide Verification
    Isolated
    Automated
    Recap

    View Slide

  40. Accuracy
    Assurance
    Confidence
    Quality
    Recap

    View Slide

  41. Arrange
    Act
    Assert
    Recap

    View Slide

  42. I Hope You’ll Fall
    In L VE
    With Testing

    View Slide

  43. Read the WordPress Unit Tests
    Visit PHPUnit website for reference
    Check out WP-CLI scaffolding
    What Next?

    View Slide

  44. John Blackbourn
    @johnbillion

    View Slide