Slide 1

Slide 1 text

Unit Testing: What? Why? How?

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

Unit Testing: What? Why? How?

Slide 4

Slide 4 text

You’re Going to L VE Unit Testing

Slide 5

Slide 5 text

That’s Enough of That

Slide 6

Slide 6 text

Unit Testing: What? Why? How?

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

Tests typically run on the command line CLI

Slide 11

Slide 11 text

Tests are highly efficient because they're quickly repeatable Automated

Slide 12

Slide 12 text

Crossing your fingers and hoping… is not a unit test

Slide 13

Slide 13 text

Unit Testing: What? Why? How?

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

Unit tests give you confidence to make changes Confidence

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

Unit tests can be liberating Clarity

Slide 19

Slide 19 text

Unit Testing: What? Why? How?

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

“But I know that false isn’t true”

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

Arrange Act Assert AAA

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

Accuracy Assurance Confidence Quality?… The Result

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

WUT

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

Provide Verification Isolated Automated Recap

Slide 40

Slide 40 text

Accuracy Assurance Confidence Quality Recap

Slide 41

Slide 41 text

Arrange Act Assert Recap

Slide 42

Slide 42 text

I Hope You’ll Fall In L VE With Testing

Slide 43

Slide 43 text

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

Slide 44

Slide 44 text

John Blackbourn @johnbillion