Introduction to WordPress Unit Testing
summary
Unit testing claims to give a safety net by isolating functions with test doubles so code behavior is constant and the database is not hit. The demo builds a scaffolded plugin with WP‑CLI scaffolding, uses Composer to require PHPUnit and PHP‑Mock, shows mocking get_option to return "bar" and then adding array defaults, and runs on GitHub with Travis CI. The setup avoids the WP test database, speeds runs (about 200 tests in 2 seconds reported), and the example tests pass.
AI-written summary · about
chapters (26)
- 0:00Start
- 2:53WordPress tests are integration tests
- 3:04It's about how pieces of code work together
- 3:22Neither is better
- 3:36Operating System: unix-based
- 3:56PHP version: 5.3 (for code) / 5.4 (for tests)
- 4:28PHPUnit
- 4:38WP-CLI (optional)
- 5:20Add composer.json
- 6:11Modify bootstrap.php (created by WP-CLI)
- 6:32Our first plugin function
- 7:00Our first unit test
- 8:03Testing demo_get_option
- 8:59Let's try to run it
- 9:20Missing test double
- 9:45Verifies how you interact with it
- 11:32Let's make our function do this for us
- 11:50Gives you a taste for test-driven development
- 12:13First, a small change to demo_get_option
- 12:29Update our test as well
- 13:15Creating our failing test
- 13:30Changing inputs, outputs and expected values
- 14:42Building your testing habit
- 15:01Just one loop in your safety net
- 15:50Testing is a lot like picking up a new habit
- 16:00Hard to stay motivated Chips are soooo tasty!
