Late last month, I started talking about writing unit tests in PHPUnit for WordPress-based code. This primarily included the idea of setting up PHPUnit, the setUp function, and writing basic tests.
This did not, however, discuss what I know about the tearDown function which is still an important feature of writing using tests. Further, there’s also two ways to consider writing tests for WordPress projects.
Namely:
- writing tests specifically for plugins and application-layer functionality,
- running unit tests against the WordPress application.
Before moving forward with this particular post, though, I recommend catching up on what I’ve covered thus far. This includes the following posts:
- A WordPress Development Environment (Using a Package Manager)
- An IDE for WordPress Development
- Working with User Settings in Visual Studio Code
- Writing Unit Tests with PHPUnit, Part 1: The Set Up
Once you’ve done that, return to this post and let’s continue discussing the tearDown function and what unit tests in the context of WordPress actually look like.
Unit Tests with PHPUnit, Part 2: The Tear Down
Before moving forward, remember that developers often treat the setUp function like the constructor and the tearDown function like a destructor; however, remember that the latter is not always needed.
Here’s a good rule of thumb to remember:
- Anything a test function needs will call the setUp function so that necessary classes are needed.
- The tearDown function is not always needed since the setUp function can reinitialize a class.
So what does this mean for the tearDown function if it’s not resetting data that’s created during the setUp function?