I'm using phpunit to do TDD on a WordPress plugin. One of the requirements is that plugin activation fail if a taxonomy with a predefined name, let's say "apartments", is already registered (either by the theme or another plugin).

The problem is, using the standard WordPress phpunit testing suite, the plugin is activated as a "mu-plugin" automatically while the WordPress environment is bootstrapped. This post suggesting manually activating the plugin but we've tried that and it doesn't seem to work (WordPress 5.2.1). In fact, there is zero indication the function in that post does anything. I have a feeling it is because the plugin activation code probably won't run if a plugin is mu or already activated.

Before going through the code trying to figure out what's happening, I'm wondering:

  1. Is this even a proper use of phpunit in the context of WordPress plugins?
  2. If it is a proper use, how should I go about setting up such a test (checking if plugin activation fails)?

Help is greatly appreciated.

  1. Testing of WP plugins and themes with phpunit is absolutely proper and widely used. You can see an example of pure phpunit tests in our plugin Cyr2Lat (please look at test folder).

What you can find there, it is an example of pure phpunit tests, which work with testing class only and mock everything other.

Tests based on WP_UnitTestCase we call "integration tests", as they load the whole WordPress core and work with database.

  1. You can make testing of plugin activation by both approaches mentioned above. With pure phpunit tests, you can just check behavior of your class responsible for activation.

If you prefer to use WP_UnitTestCase, you should modify bootstrap.php to prevent direct loading of the plugin, hook to 'taxonomy_registered' for post, register your own taxonomy, do action 'activate' for your plugin, and check if it became activated. This is how I see the sequence.

  • The link to the sample tests is very helpful, thanks. I was really hoping for a way to use the WP_UnitTestCase without having to modify bootstrap.php to keep the tests portable but I understand the issue. Thanks! – Ted Stresen-Reuter Jun 20 '19 at 21:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.