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
towpunit.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:
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.
Setup the Docker Environment
Build and start the 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 to run the full test suite in the docker container.
Run the full test suite
Run specific tests in Docker shell
To run individual tests, you will need to build the ‘app' docker image.
In the 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):
Run specific tests in testing environment
Use the environment variable SUITES to specify individual files or lists of files to test. This is useful when working on one test file and wanting to limit test execution to the single file or test. Also see the contributing documentation on enabling xdebug in the testing docker environment.
Notes:
- If you make a change that requires
composer install
to be rerun, run composter build-app again. - Leave the container shell by typing
exit
. - Docker artifacts will usually be cleaned up automatically when the script completes. In case it doesn't do the job, try these solutions:
- Run this command:
docker system prune
- https://docs.docker.com/config/pruning/#prune-containers
- Run this command: