Slides from my talk at WP Hooked, February 2017.
https://www.meetup.com/wp_hooked/
Unit Testing:What? Why? How?
View Slide
John BlackbournWordPress core developerWorking with WordPress for 10+ yearsSenior Engineer at Human Made@johnbillion
You’re Going toL VEUnit Testing
That’s Enoughof That
A unit test is a piece of codewhich exists only to verify expectationsabout the behaviour of another piece of codeVerification
a.k.a.“Does my function dowhat I expect it to do?”
A unit test operates in isolationin order to avoid external interferenceIsolated
Tests typically run on the command lineCLI
Tests are highly efficientbecause they're quickly repeatableAutomated
Crossing your fingersand hoping…is not a unit test
A unit test can cover many scenariosand never misses anythingAccuracy
Unit tests give you increased assurancethat changes are less likely to cause breakageAssurance
Unit tests give youconfidence to make changesConfidence
Unit tests help you write better codeSmaller functionsSeparation of concernsQuality
Unit tests can be liberatingClarity
“But I know that false isn’t true”
ArrangeActAssertAAA
Each time a WordPress test runs,it operates on a fresh installationRemember
AccuracyAssuranceConfidenceQuality?…The Result
AssertionsassertTrue( $actual )assertFalse( $actual )assertEquals( $expected, $actual )assertCount( $expected, $actual )assertObjectHasAttribute( $key, $actual )assertArrayHasKey( $key, $actual )assertWPError( $actual )assertCanonical( $actual, $expected )
a.k.a.A Real World GotchaType Juggling Kills Me
WUT
Provide VerificationIsolatedAutomatedRecap
AccuracyAssuranceConfidenceQualityRecap
ArrangeActAssertRecap
I Hope You’ll FallIn L VEWith Testing
Read the WordPress Unit TestsVisit PHPUnit website for referenceCheck out WP-CLI scaffoldingWhat Next?
John Blackbourn@johnbillion