How to Run PHPUnit in a WordPress Plugin on Windows Using LocalWP
Tags: wordpress, phpunit, localwp
Published: 2022-07-28 00:03:31

#### Table of Contents
- [Requirements](#requirements)
- [Select Git Bash as the default terminal](#select-git-bash-as-the-default-terminal)
- [Create a new WordPress plugin](#create-a-new-wordpress-plugin)
- [Installing WP tests](#installing-wp-tests)
- [Install PHPUnit](#install-phpunit)
- [Install PHPUnit Polyfills](#install-phpunit-polyfills)
- [Change the Temp directory](#change-the-temp-directory)
- [Running our first test](#running-our-first-test)

#### Requirements

- Install [Local](https://localwp.com/)
- Install [Git Bash for Windows](https://gitforwindows.org/)


#### 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](https://gitforwindows.org/) is installed in this location `C:\Program Files\Git`
![LocalWP Settings](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kx70jrj0wtc2ez3mmpdq.png)

#### 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/riq84nag3gscpfp8cxl2.png)

Now, open the site terminal by clicking on Open Site Shell 
![LocalWP Open Terminal](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hi06r8e0fazcz5wfryba.png)

Run `wp scaffold plugin sample-plugin` to create a plugin, this will also add the test files that we need.
![wp scaffold Command Terminal](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/33ubm68chbzu5238mn8u.png)
 
_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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gs03bylvmxpbw9adiy0q.png
)
 
 
#### 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 
```

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0fw9ca3cvodrijj844op.png)
 
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](#change-the-temp-directory)._

You should also see our created `wp-tests` database if you run:
```
mysql
show databases;
```
![mysql show databases](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eq8wrjb4nm49q4n1hy03.png)

#### 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
```

#### Install PHPUnit Polyfills
[PHPUnit Polyfills](https://github.com/Yoast/PHPUnit-Polyfills/) are required to create PHPUnit tests in WordPress.

To install them, Run:
```
composer require --dev yoast/phpunit-polyfills
```

#### 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:
![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jcj67twnyroti6gymtex.png)

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/' );
```



#### 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8crncf70sudyh6woj77h.png)


On your Git Bash terminal, run:
```
vendor/bin/phpunit
```
 
You should see `OK (1 test, 1 assertion)` that means that the sample test was tested successfully!