How can you unit test your plugin or theme without WordPress or other dependencies? You simulate the dependencies. My favorite tool to redefine and mock functions is Brain Monkey. In this hands-on testing lab, you will actively write unit tests using Brain Monkey.
The challenge we have when unit testing a plugin or theme is how to force the expected behavior of dependent functions. For objects, we can use Mockery. But for functions, we need another tool.
Huh? For example, your plugin or theme will likely have various WordPress functions, such as add_action
, add_filter
, wp_normalize_path
, esc_html
, esc_attr
, and many others. Right? We use these functions all the time. But how can you unit test your code without loading WordPress?
My favorite tool is Brain Monkey by Giuseppe Mazzapica. Seriously, it’s an amazing testing utility for redefining and mocking dependent functions.
In this lab, you will dive deep into Brain Monkey to discover how to build unit tests without loading WordPress or any other dependency.
What You Will Learn
In this lab, you will be actively building unit tests as you learn deeply about the process and Brain Monkey. As a test subject, you will write unit tests for beans_get_post_meta()
function in the Beans framework.
You’ll learn:
- The process of dynamically redefining a function.
- What Brain Monkey is and each of its toolsets.
- Deciding when to use
when
vs.expect
. - How to force a behavior or returned value from a mocked function.
- How to actively test the number of times a function is called.
- How to actively test the arguments being passed to the function.
- and much more.
This lab has several code challenges too.
Prerequisites
There’s a time to code and …. yup, that sums it up.
Episodes
Total Lab Runtime: 03:21:08
- 0 Lab Setupfree 07:07
- 1 The Problem - Why We Need to Simulatefree 24:50
- 2 Behind the Scenes of Redefining a Functionfree 20:31
- 3 Brain Monkey's Toolset Overviewpro 09:04
- 4 Anatomy of the Test Expectationpro 11:51
- 5 setUp and tearDownpro 09:51
- 6 Create a Test Classpro 20:42
- 7 `when()`pro 14:23
- 8 `when()` - Behavior Optionspro 14:26
- 9 `when()` - Code Challengepro 08:27
- 10 `expect()`pro 11:40
- 11 `with()`pro 05:44
- 12 More with `with()`pro 10:02
- 13 `with()` - Code Challengepro 09:53
- 14 `expect()` - Behavior Optionspro 07:47
- 15 `andAlsoExpectIt()`pro 10:46
- 16 `stubs()`pro 04:04