DEV Community

Elie Hanna
Elie Hanna

Posted on

How to Run PHPUnit in a WordPress Plugin on Windows Using LocalWP

Table of Contents

Requirements

Select Git Bash as the default terminal

Once Local is installed, click on the hamburger menu on the top left then click on preferences.

Under default apps, make sure that Git Bash is selected as the terminal app.

If you can't select Git Bash, make sure that Git Bash for Windows is installed in this location C:\Program Files\Git
LocalWP Settings

Create a new WordPress plugin

If you do not have a LocalWP site already, you can create one by clicking on the bottom left button in Local and go through the setup wizard, when you are done, you should be taken to your Local WP site dashboard that looks like this:
LocalWP New Site

Now, open the site terminal by clicking on Open Site Shell
LocalWP Open Terminal

Run wp scaffold plugin sample-plugin to create a plugin, this will also add the test files that we need.
wp scaffold Command Terminal

Note that if you wish to add the test files to an existing plugin you should run wp scaffold plugin-tests sample-plugin instead.

The file structure of our generated plugin sample-plugin should look like this:
New Plugin File Structure

Installing WP tests

Let's dive into our plugin directory with cd wp-content/plugins/sample-plugin and run the install-wp-tests.sh <db-name> <db-user> <db-pass> [db-host] like this:

bin/install-wp-tests.sh wp_tests root root localhost 
Enter fullscreen mode Exit fullscreen mode

db-name: this is our test database name that will be used to run tests, it could be anything as long as it's not our site's database name.

db-user, db-pass, db-host: are the values shown in your Local site Database tab:
LocalWP Database Info

If this installation ran correctly, you shouldn't see any errors in the Git Bash terminal and you should see two new folders in your %TEMP% directory wordpress and wordpress-tests-lib.

More about the %TEMP% directory below.

You should also see our created wp-tests database if you run:

mysql
show databases;
Enter fullscreen mode Exit fullscreen mode

mysql show databases

Install PHPUnit

We are going to install PHPUnit using Composer, LocalWP should come with Composer installed, to install PHPUnit run:

composer require --dev phpunit/phpunit
Enter fullscreen mode Exit fullscreen mode

Install PHPUnit Polyfills

PHPUnit Polyfills are required to create PHPUnit tests in WordPress.

To install them, Run:

composer require --dev yoast/phpunit-polyfills
Enter fullscreen mode Exit fullscreen mode

Change the Temp directory

If you were to run vendor/bin/phpunit now, you would get some errors on the console missing files in directories. This is only an issue on Windows. To fix this open the wordpress-tests-lib/wp-tests-config.php in your Temp directory and edit the constant ABSPATH on line 4 and prepend your %TEMP% directory.

You can find the location of your Temp folder by typing echo %TEMP% on the Windows Terminal:

My Temp folder path is C:\Users\Elie\AppData\Local\Temp so my ABSPATH looks like this:

define( 'ABSPATH', 'C:\Users\Elie\AppData\Local\Temp/wordpress/' );
Enter fullscreen mode Exit fullscreen mode

Running our first test

Our plugin comes with a sample test in tests/test-sample.php, to enable it open phpunit.xml and comment out the line that has the <exclude> tags:
phpunit.xml

On your Git Bash terminal, run:

vendor/bin/phpunit
Enter fullscreen mode Exit fullscreen mode

You should see OK (1 test, 1 assertion) that means that the sample test was tested successfully!

Top comments (0)