Common Unit Test Issues in WordPress

Unit testing PHP code in WordPress is not a straightforward task. Automated tests in WordPress use PHPUnit as a base. But there is lots of additional code added on top to adapt the framework to work with WordPress.

In this article we’ll look at the most common unit test issues in WordPress. For each issue, we’ll try and provide a fix, or a workaround.

Read More

Understanding WordPress PHP Unit Test Fixture Setup and Teardown

WordPress PHP Unit Testing When I get started writing unit tests in WordPress, I continuously ran into what seemed to be strange behaviours by the WordPress Unit Testing framework. Strange when comparing to other PHP unit tests anyway.

It wasn’t until I read all the code of the bootstrap and install scripts, as well as the base WordPress unit test case, that I was able to make sense of it all.

So in this article, I’ll detail how WordPress Unit Testing framework handles fixtures. In addition, I’ll touch on a few things to watch out for when writing your own tests.

Read More

WP_UnitTest_Factory: The Missing Documentation

Most WordPress functions interact with data objects like posts, users, or terms. This creates a problem for developers, as every test starts with an empty WordPress installation. The WordPress Unit Test Factory helps to solve this issue.

It provides a set offers a set of helper classes to create these database objects, called database fixtures in a unit testing scenario.

The test framework offers factories for creating:

  • Attachments
  • Comments
  • Posts
  • Categories, Tags, and Terms
  • Users

It also has multisite specific factories for:

  • Networks
  • Blogs

In this article, we’ll see about how you can interact with these factories, and what issues to watch out for.

Read More

The Guide to WordPress Unit Testing Assertions

The WordPress Unit Testing Framework contains a set of custom assertion helper methods which help with writing unit test.

In this post, we’ll present these helper methods available through the WP_UnitTestCase case class. Each method is presented with its function signature, a description of what it does, as well as a sample usage in a unit test.

Read More