wp-env: Failing to run PHPUnit tests using wp-env in GitLab CI
What problem does this address?
Hey,
I’m encountering issues running PHPUnit tests for my WordPress plugin using wp-env inside a GitLab CI pipeline. Despite several attempts and command variations, the tests are not executing properly.
Project: example-plugin-env-ci
Problem Summary
I’m using wp-env to spin up the environment and run tests via vendor/bin/phpunit with a phpunit.xml.dist config file located at the plugin root.
All my attempts to run tests inside the tests-cli container are failing with errors like:
- stat vendor/bin/phpunit: no such file or directory
- or Could not read "phpunit.xml.dist"
- or simply outputting PHPUnit usage info without running any test.
- Could not find /wordpress-phpunit/includes/functions.php, have you run bin/install-wp-tests.sh ?
When I run the following inside the container:
wp-env run tests-cli bash cd / ls -lah
I noticed that the wordpress-phpunit directory is not present inside the container like it is when running wp-env locally. This could potentially explain why some files like /wordpress-phpunit/includes/functions.php are missing and cause errors during PHPUnit runs.
If anyone has:
- A working CI setup with wp-env + PHPUnit in GitLab
- Faced and solved similar missing directory issues in CI
Please share your solution or insights!
Thanks in advance
Two separate causes here:
-
vendor/bin/phpunitnot found,phpunit.xml.distunreadable, or PHPUnit usage output:wp-env runstarts in the WordPress root, not your plugin. Pass--env-cwd=wp-content/plugins/<your-plugin>. -
Missing
/wordpress-phpunit/includes/functions.php: withdocker:dind, the daemon is a separate container, so the host paths wp-env bind-mounts (the PHPUnit suite and your plugin) don't exist there and Docker mounts empty directories. I reproduced this with@wordpress/env12.1.0: both/wordpress-phpunitand the plugin directory were empty inside the container. Having the project andWP_ENV_HOMEat the same path on both the job and the daemon should avoid it, though I haven't tested that yet.
Running as root is a separate problem, tracked in #76564.