Setting up PHPUnit tests for WordPress plugins on Windows involves several steps. Follow this guide to ensure a smooth setup.
-
You can install WPCLI using Composer with the following command:
composer global require wp-cli/wp-cli-bundle
-
Verify if WPCLI is installed:
wp
If you encounter any issues, refer to the WordPress Official Documentation for additional installation options.
To install the SVN command line tools:
-
Download and install TortoiseSVN Software
-
Install the command line client tools. Here's an image illustrating the process:
-
To ensure MySQL is appropriately set up:
mysqladmin --version
-
If the MySQL admin tool is not installed, but you are using a local server such as Laragon, WampServer, or XAMPP, add the MySQL bin directory to your system's environment variables.
-
If you're not using a local server, download MySQL from the Official Website and install it.
PHPUnit can be installed globally using Composer:
-
Install using Composer with the following command:
composer global require phpunit/phpunit
-
If you encounter version compatibility issues, visit Packagist to find your required version. Install PHPUnit with your desired version using:
composer global require phpunit/phpunit:9.6.11
To generate the necessary files for running PHPUnit tests within a plugin, follow these steps:
-
Open the terminal on your plugin directory
-
Generate the files using the WP-CLI scaffold command:
wp scaffold plugin-tests my-plugin
-
Install Yoast/PHPUnit-Polyfills
composer require --dev yoast/phpunit-polyfills:"^2.0" -
Define the path to Polyfills in your phpunit.xml[.dist] file:
<php> <const name="WP_TESTS_PHPUNIT_POLYFILLS_PATH" value="vendor/yoast/phpunit-polyfills"/> </php>
-
Run the Command
Execute the following command, replacing
<db-name>,<db-user>,<db-pass>, and<db-host>with your actual database details:bash bin/install-wp-tests.sh <db-name> <db-user> <db-pass> <db-host>
-
Example
As an example, here's how you might run the command:
bash bin/install-wp-tests.sh wordpress_test root '' localhost latest
phpunitIf you encounter a fatal error like the following:
```
Warning: require_once(/tmp/wordpress//wp-includes/class-phpmailer.php): failed to open stream: No such file or directory in /tmp/wordpress-tests-lib/includes/mock-mailer.php on line 2
Fatal error: require_once(): Failed opening required '/tmp/wordpress//wp-includes/class-phpmailer.php' (include_path='.:/opt/lampp/lib/php') in /tmp/wordpress-tests-lib/includes/mock-mailer.php on line 2
```
- Open your
C:\Users\{USER\AppData\Local\Temp\wordpress-tests-lib\wp-tests-config.phpfile and change the ABSPATH with the absolute path to your TEMP folder, for example:C:/Users/{USER}/AppData/Local/Temp/wordpress-tests-lib/.
By following these steps, you empower yourself to develop high-quality WordPress plugins with confidence. Automated testing not only prevents regressions and bugs but also fosters a culture of continuous improvement in your development process. So, take the time to set up your testing environment and enjoy the benefits of robust and reliable plugin development. Happy coding!
