- Reduces time + chance of human error
- Easily reproducible
- Gateway to CI/CD
- Unit – Test the smallest possible unit of an app. Often a single function
- Integration – How individual components interact
- End-to-End(E2E) – An entire path through an application
Automated Testing Pyramid
ROI for Testing
System Under Test (SUT)
- The system we’re currently testing:
- A single method
- A class
- A whole feature
WordPress Testing is Complicated
- Tightly-coupled system
- Difficult to test anything in true isolation
PhpUnit – https://phpunit.de/
- Test Suite – Collection of test classes
- Test Class (class) – Collection of test methods
- Test Case (method) – A single scenario to be tested
- True or False
- Verify Contents – contains, regular expression matching
- Negative Assertions
Replacing actual systems with test versions.
- Stub out/mock an API
- Return known values
Mockery – popular library for creating test doubles
PhpUnit Markup Assertions – Assertions powered by DOMDocument
WP Core Test Suite
Scaffolding Out Test Suite
Generate test scaffolding via WP-CLI
What Do We Get?
What We Don’t Get
- Locate the WordPress installation
- Gain access to the tests_add_filter() function.
- Load the main plugin file.
- Bootstrap WordPress core.
Base Test Class
Generate users, posts, and more for testing.
Impersonating Users (WordPress)
Act as users with different roles & capabilities
Change the current page context.
Checking for WP_ERRORs
Was the response an instance of WP_ERROR?
Arrange – Act – Assert
- Arrange – Set up the scenario
- Act – Execute the code
- Assert – Verify things happened as you expected
Registering a Custom Post Type
Stubbing HTTP Requests
Basic Test-Driven Development (TDD)
- Write a failing test to describe the functionality or behavior (Red)
- Write the code to make the test pass (Green)
- Refactor, rinse, repeat (Refactor)