Homepage
Link to WPGraphQL on Github
Link to WPGraphQL on WordPress.org

Testing

This document will be most useful for developers that want to contribute to WPGraphQL and want to run tests locally.

In order to run tests, you must clone the plugin from Github. Downloading from Composer or Packagist will not include the dev dependencies needed to run tests.

Testing Locally with Codeception

Running tests locally with Codeception is the fastest way to run tests, but it requires some initial setup on your local machine.

Pre-requisites

  • Command line access
  • PHP installed and running on your machine
  • MySQL installed and running on your machine
  • Composer

Install WordPress Test Environment

WPGraphQL includes a script to install a local test environment.

Running this script will install WordPress to your machines tmp directory, and will add WPGraphQL as a plugin and activate it.

NOTE: Because this installs to a tmp directory, if you restart your computer, this will go away so you would need to run this script again to setup your test environment again.

Run the following command, replacing the variables with data for your local Database

For example:

This should output similar to the following:

Configure Test Suites

Within the /tests/ directory of WPGraphQL are the Codeception .yml configuration files.

To run tests locally, copy the .yml file for the test suite you want to run and rename it without the .dist.

For example:

  • copy: wpunit.suite.dist.yml to wpunit.suite.yml

Then update the details of the .yml file to point to your local database by changing the fields:

  • dbName
  • dbHost
  • dbUser
  • dbPassword

The other fields should be able to remain the same, but update as necessary.

Install Composer Dependencies

Run the following script from the root of the WPGraphQL plugin:

This installs the dev dependencies needed to run the tests.

Run the tests

To run the tests, run the following commands (you can use control + c to exit):

This will run all of the tests of the wpunit suite.

To run an individual test file, you can specify the file like so:

Or you can specify one specific test like so:

The tests should start running and you should see something similar to the following:

Screenshot of Codeception tests running in the terminal

Testing with Docker

Testing in docker is slower than testing locally with Codeception, but it allows you to test in a consistent environment and not have to worry about a local environment issue getting in the way of running the tests.

Pre-Requisites

In order to run tests with Docker, you should have Docker running on your machine, and you should be logged in to Docker.

You’ll need two terminal windows for this.

The first window is to start the Docker containers needed for running tests.

The second window is where you’ll log into one of the running Docker containers (which will have OS dependencies already installed) and run your tests as you make code changes.

Setup the Docker Environment

In the first terminal window, start up a pristine Docker testing environment by running this command:

This step will take several minutes the first time it’s run because it needs to install OS dependencies. This work will be cached so you won’t have to wait as long the next time you run it. You are ready to go to the next step when you see output similar to the following:

In the second terminal window, access the Docker container shell from which you can run tests:

You should eventually see a prompt like this:

Now you are ready to work in your IDE and test your changes by running any of the following commands in the second terminal window):

Notes:

  • If you make a change that requires composer install to be rerun, shutdown the testing environment and restart it to automatically rerun the composer install in the testing environment.
  • Leave the container shell (the second terminal window) by typing exit.
  • Shutdown the testing environment (the first terminal window) by typing Ctrl + c
  • Docker artifacts will usually be cleaned up automatically when the script completes. In case it doesn’t do the job, try these solutions: