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:

  1. writing tests specifically for plugins and application-layer functionality,
  2. 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:

  1. A WordPress Development Environment (Using a Package Manager)
  2. An IDE for WordPress Development
  3. Working with User Settings in Visual Studio Code
  4. 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.

PHPUnit, Part 2: The Tear Down

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?

For Members Only

Thanks for your interest in this article; however, it's available to members only. If you'd like to review this (and have access to all previous and future articles), check out the sign-up page.

Category:
Uncategorized
Tags:
,