DEV Community

Lucas Carvalho
Lucas Carvalho

Posted on • Updated on

Wordpress tests with Pest and WP Setup

Introduction

In my last post here, I presented my new WordPress environment tool called WP Setup, and at that moment it was missing a test environment.

Today, I finished the first implementation of this environment, adding Pest and PHPUnit in v10.5, which is currently not supported by default with WP 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.

Initial plugin

Then, start an npm 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:

npm install wp-setup --save-dev
Enter fullscreen mode Exit fullscreen mode

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

"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"
}
Enter fullscreen mode Exit fullscreen mode

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:

npm run wp-setup init
Enter fullscreen mode Exit fullscreen mode

Create the test configuration:

npm run wp-setup -- --tests
Enter fullscreen mode Exit fullscreen mode

After this, you will end up with this structure:

Basic plugin structure

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 started.

Run:

npm run env:start
Enter fullscreen mode Exit fullscreen mode

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

Default WordPress Site Homepage

Finally running the tests

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

npm run env:pest
Enter fullscreen mode Exit fullscreen mode

Tests results

As you can see, the example tests ran as expected, and you can create new tests following the Pest documentation and the WordPress Tests Handbook.

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.

npm run env:stop
Enter fullscreen mode Exit fullscreen mode

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

npm run env:destroy
Enter fullscreen mode Exit fullscreen mode

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!!!

Top comments (0)