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/

23e12888dcd87d07434b7621bc164958?s=128

John Blackbourn

February 14, 2017
Tweet

Transcript

  1. Unit Testing: What? Why? How?

  2. John Blackbourn WordPress core developer Working with WordPress for 10+

    years Senior Engineer at Human Made @johnbillion
  3. Unit Testing: What? Why? How?

  4. You’re Going to L VE Unit Testing

  5. That’s Enough of That

  6. Unit Testing: What? Why? How?

  7. A unit test is a piece of code which exists

    only to verify expectations about the behaviour of another piece of code Verification
  8. a.k.a. “Does my function do what I expect it to

    do?”
  9. A unit test operates in isolation in order to avoid

    external interference Isolated
  10. Tests typically run on the command line CLI

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

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

  13. Unit Testing: What? Why? How?

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

    anything Accuracy
  15. Unit tests give you increased assurance that changes are less

    likely to cause breakage Assurance
  16. Unit tests give you confidence to make changes Confidence

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

    of concerns Quality
  18. Unit tests can be liberating Clarity

  19. Unit Testing: What? Why? How?

  20. None
  21. None
  22. None
  23. None
  24. “But I know that false isn’t true”

  25. None
  26. Arrange Act Assert AAA

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

    fresh installation Remember
  28. None
  29. None
  30. None
  31. None
  32. Accuracy Assurance Confidence Quality?… The Result

  33. Assertions assertTrue( $actual ) assertFalse( $actual ) assertEquals( $expected, $actual

    ) assertCount( $expected, $actual ) assertObjectHasAttribute( $key, $actual ) assertArrayHasKey( $key, $actual ) assertWPError( $actual ) assertCanonical( $actual, $expected )
  34. a.k.a. A Real World Gotcha Type Juggling Kills Me

  35. None
  36. None
  37. WUT

  38. None
  39. Provide Verification Isolated Automated Recap

  40. Accuracy Assurance Confidence Quality Recap

  41. Arrange Act Assert Recap

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

  43. Read the WordPress Unit Tests Visit PHPUnit website for reference

    Check out WP-CLI scaffolding What Next?
  44. John Blackbourn @johnbillion