WordPress PHP Unit Testing

The WordPress PHP unit testing framework is an extension of the PHPUnit framework. It is part of WordPress Core, and powers the test suite for the project.

This section contains documentation the WordPress PHP unit testing framework. It also contains articles that explain how to best implement testing for various features–whether in WordPress Core, or in your own projects.

Topics covered include:

  • The WordPress unit test factory (WP_UnitTest_Factory).
  • The WordPress unit test case (WP_UnitTestCase) which is the base for all unit tests.
  • Automated deletion of WordPress unit test data.

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