- Introduction
- Factories
- Why Not Latest PHPUnit
- Woocommerce Developer Blog
- Wordpress Writing PHP Tests
- Get Started
- Mocking
Writing test cases for your code ensures code quality, proper implementation of business logic, and easy refactoring. It is an essential part of the development process.
- Write a Test: Define the desired functionality.
- Run the Test: Ensure it fails (since the functionality is not yet implemented).
- Write Code: Implement the minimum code required to pass the test.
- Run Tests: Verify that all tests pass.
- Refactor: Improve the code structure while ensuring that tests still pass.
- Repeat: Continue this process for each new feature.
The following libraries are used for TDD. All methods and features of these libraries are available for writing test cases:
- PHPUnit
- Mockery
- Brain Monkey: A unit test utility for PHP, specifically for mocking and testing WordPress code.
You can run the test cases from the plugin's root directory terminal using one of the following methods:
Option 1:
Run tests using the ./vendor/bin/phpunit command:
./vendor/bin/phpunit
./vendor/bin/phpunit --filter {your-filter-key}Option 2:
Use the custom commands test and test-f added to the composer script:
composer test
composer test-f {your-filter-key}We can only use PHPUnit versions that are supported by WP PHPUnit.