Warning: filemtime(): stat failed for /home/staging-yoast/staging-platform.yoast.com/versions/1928051702b5b9621963fa689280a3582727e2da/web/app/themes/yoast-theme/blocks/yoasie-runner/assets/yoasie-runner.js?v=938448 in /home/staging-yoast/staging-platform.yoast.com/versions/1928051702b5b9621963fa689280a3582727e2da/web/app/themes/yoast-theme/php/class-theme.php on line 324 Unit testing WordPress plugins with PHPUnit in PhpStorm • YoastSkip to content
🚀 New: Yoast SEO AI+ is live. Explore your complete search visibility package!
A nice way to optimize your test flow is by having PhpStorm run your unit tests for you. It can be quite some work to set this up for WordPress plugins, since they depend so heavily on WordPress. In this post, I’ll go over all the steps you’ll have to take to do this.
1) Install PHPUnit
To start unit testing your code you first have to install PHPUnit.
In my situation and examples I’ll use PHPUnit installed with homebrew.
2) Set up tests for WordPress plugins
There are a few things you need, to write Unit Tests for WordPress plugins.
WP develop
As we’re developing a WordPress plugin, the code will be coupled to WordPress. This is not ideal for unit testing, but since WordPress has lots of global state and functions, it’s virtually impossible (or at least quite impractical) to isolate the plugin code from it. This also means we want to run our unit tests on top of WP develop. A guideline on how to setup WP develop unit tests can be found on the WordPress website.
bootstrap.php
To run PHPUnit you need to first bootstrap your environment. Since WordPress is part of our environment, we’ll also require the WP develop bootstrap.php (you can find this in the wp-develop directory). We’ll put the bootstrap.php file in a tests directory in the project root.
In our situation the bootstrap file can be found under the /tests directory. The target directory where PHPUnit will look for the file will be /tests as well. According to convention, all the test files have to begin with test- and should end with .php (the extension) in order to be picked up by PHPUnit.
We should end up in a file structure like this.
3) Configure PhpStorm
The first thing we need to do is make sure if we’ve configured an interpreter under Preferences > Languages & Frameworks > PHP.
Choose a PHP interpreter (1) or select a PHP interpreter in your filesystem (2). Then add wp-develop and the PHPUnit framework (3) to have some autocompletion niceness.
Next we need to tell PHPStorm where it can find PHPUnit. You can do this under Preferences > Languages > PHP > PHPUnit.
Fill in the path to phpunit.phar
We can now start configuring a testrunner. You can do this under under Run > Edit Configurations… where you’ll get a screen like this:
You can configure PHPUnit in four steps.
There are four steps to take:
Give the configuration a name, for example: PHP Unit.
Check the option ‘defined in the configuration file’.
Check ‘use alternative configuration file’ and set the path to the phpunit.xml file.
Set environment variables, you can do this by clicking the ‘…’ after the input field. You’ll get the next screen:
Add WP_DEVELOP_DIR as an environment variable.
You have to enter ‘WP_DEVELOP_DIR’ as name and the path to the wp-develop directory we’ve cloned before. We use the WP_DEVELOP_DIR environment variable in our bootstrap.php to require the WordPress unit test bootstrap.php.
Run tests
For this article we write an example test to show how PHPUnit works. The tests have to be put in the /tests folder.
Our example class will look something like:
[code lang=”php”]
class Foo_Test extends WP_UnitTestCase {
public function test_foo_is_foo() {
$this->assertTrue( ‘foo’ === ‘foo’ );
}
}
[/code]
We extend the WP_UnitTestCase because we are in a WordPress context. If this is not the case you can use PHPUnit_Framework_TestCase instead.
You can run the test by pressing the play icon in the run toolbar or under >Run> Run ‘PHP Unit’. PhpStorm will now open a small window in which it will run your tests:
PhpStorm will show you a window like this when it runs tests.
Congratulations. You’ve setup PhpStorm to run your unit tests.
Andy Meerwaldt
Andy Meerwaldt is an ambitious web developer at Yoast. In his spare time he loves to read about both World Wars.
October 27 - 28, 2025
Team Yoast is Attending, Speaking at State of Search 2025! Click through to see who will be there, what we will do, and more!
See where you can find us next »
Discussion (1)