Unit and Integration Tests

Pest is used for unit and integration tests in this project.

All tests are located in the tests directory. The tests are split into two directories: Unit and Integration.

Unit tests are not dependent on a working WordPress installation and can be run without a database connection. patchwork and Brain Monkey are used to mock WordPress functions and classes in the unit tests.

Integration tests on the other hand bootstrap a WordPress installation and run tests that require a working WordPress installation.

Test environment setup

Before running the tests, you have to set up the test environment:

  1. Create a .env.testing file in the root of the project and set up the environment values that are needed for the tests. Here is an example of the .env.testing file:
DB_NAME=aubrie-app_testing
DB_USER=root
DB_PASSWORD=
DB_HOST='127.0.0.1'
REDIS_HOST='0.0.0.0' # Invalid value to prevent Redis from being used in tests

WP_HOME='https://aubrie-app.example.com'
WP_SITEURL="${WP_HOME}/wp"
  1. Run the following command to set up the test database. This will perform a core installation of WordPress and install all available plugins.
./scripts/setup-tests.sh

[!TIP] If some major changes are made to the database or plugin setup, you can just run the ./scripts/setup-tests.sh script again to reset the test environment.

Running the tests

To run the tests, execute the following command:

composer run test:unit # Runs only unit tests
composer run test:integration # Runs only integration tests
composer run test # Runs unit tests, then integration tests

Most IDEs such as PHPStorm or Visual Studio Code have built-in support for running Pest tests. You can also run the tests directly from the IDE.

[!WARNING] Some IDEs or tools (such as running pest without any arguments) may try to run the unit and the integration test in one process. This will cause the integration tests to fail because mocked functions interfere with the WordPress bootstrap process. Always run the tests using the provided Composer scripts.

Database tests

All integration tests are wrapped in a transaction that is rolled back after the test is finished. This ensures that the database is in the same state before and after the test. This is done by using the DatabaseTransactions trait in the integration tests.