Wordpress tests with Pest and WP Setup
Tags: wordpress, pest, php, tutorial
Published: 2024-04-17 23:55:34

## Introduction

In my last post [here](https://dev.to/lucascarvalhopl/introducing-wp-setup-11fb), I presented my new WordPress environment tool called **[WP Setup](https://www.npmjs.com/package/wp-setup)**, and at that moment it was missing a test environment.

Today, I finished the first implementation of this environment, adding [Pest](https://pestphp.com/) and [PHPUnit](https://phpunit.de/index.html) in v10.5, which is currently not supported by default with [WP Env](https://developer.wordpress.org/block-editor/reference-guides/packages/packages-env/).

So, let's put our hands a little bit into the code and see how to start doing tests with **WP Setup** and **Pest** in a few steps creating a basic plugin.

## The plugin basis

To build this simple example, create a directory called `my-plugin` with just a `my-plugin.php` file containing the [WP header requirements](https://developer.wordpress.org/plugins/plugin-basics/header-requirements/).

![Initial plugin](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bw4m0a4qc4ecjr24426z.png)

Then, start an [npm](https://npmjs.com/) project in this directory by typing `npm init -y` in a terminal.

Now we just need to add WP Setup as a **development package** by typing the following command in the terminal:

```bash
npm install wp-setup --save-dev
```

And in the `scripts` section of the created `package.json` file, add the json bellow:

```json
"scripts": {
	"wp-setup": "wp-setup",
	"env:start": "wp-setup start",
	"env:stop": "wp-setup stop",
	"env:destroy": "wp-setup destroy",
	"env:run": "wp-setup run",
	"env:wp": "wp-setup wp",
	"env:help": "wp-setup help",
	"env:composer": "wp-setup run wp-cli --workdir . composer",
	"env:pest": "wp-setup run wp-test-cli --workdir . global-pest"
}
```

## The init command

In this last update, the `init` command was added, and with this, it's now only two commands to set up the environment.

Create the `wp-setup.json` file:

```bash
npm run wp-setup init
```

Create the test configuration:

```bash
npm run wp-setup -- --tests
```

After this, you will end up with this structure:

![Basic plugin structure](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/me01m0vf10fmg2wm8mib.png)

## Starting the environment

Now, we are fully set up and just need to start the environment.

**Please, be sure to have the ports :80 and :443 available on your machine and [Docker](https://www.docker.com/get-started/) started.**

Run:

```bash
npm run env:start
```

After a while, you can visit your environment at `https://my-plugin.localhost`.

![Default WordPress Site Homepage](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rjxw8sjdmx984r0im4qn.png)

## Finally running the tests

Now that all is up and running, we can start our tests with Pest by running:

```bash
npm run env:pest
```

![Tests results](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mqssboneydjg98lpjrw8.png)

As you can see, the example tests ran as expected, and you can create new tests following the [Pest documentation](https://pestphp.com/docs/) and the [WordPress Tests Handbook](https://make.wordpress.org/core/handbook/testing/automated-testing/writing-phpunit-tests/).

## Stopping the environment

Now that we've finished our work here, we can stop our environment, removing the existing Docker containers and freeing the used ports. The images and development volumes will persist on your machine, making it fast to start again.

```bash
npm run env:stop
```

Also, if you prefer, you can destroy the entire environment by running:

```bash
npm run env:destroy
```

## Conclusion

I hope this simple tutorial helps you effectively utilize **WP Setup** with Pest for your WordPress development projects.

If you have any questions or need further assistance, please feel free to comment out here. Happy coding!!!