WooCommerce Testing
Before writing code
Fetch live docs:
- Web-search
site:developer.woocommerce.com testing for WooCommerce testing guide
- Web-search
site:developer.wordpress.org plugins testing for WordPress Plugin testing handbook
- Web-search
woocommerce e2e testing playwright for E2E patterns
Test Types
Unit Tests
Isolated class/function testing:
- No WordPress/WooCommerce bootstrap
- Mock all dependencies
- Fast execution
- Test pure business logic
Integration Tests
Tests with WordPress + WooCommerce loaded:
- Extend
WC_Unit_Test_Case (which extends WP_UnitTestCase)
- Full WordPress database (using transactions for rollback)
- Access to all WooCommerce APIs and hooks
- Test plugin integration with WooCommerce
E2E (End-to-End) Tests
Browser-based tests with Playwright:
- Test complete user flows (add to cart, checkout, payment)
- WooCommerce provides Playwright utilities
- Run against a real WordPress + WooCommerce instance
Setting Up PHPUnit Tests
Scaffold with WP-CLI
wp scaffold plugin-tests my-plugin creates:
tests/bootstrap.php — test bootstrap
tests/test-sample.php — sample test
phpunit.xml.dist — PHPUnit configuration
bin/install-wp-tests.sh — install WordPress test suite
WooCommerce Test Bootstrap
After WordPress test bootstrap, load WooCommerce and your plugin:
// In tests/bootstrap.php
require_once dirname( __DIR__ ) . '/vendor/woocommerce/woocommerce/tests/legacy/bootstrap.php';
// Or manually: activate WooCommerce, then your plugin
WC_Unit_Test_Case
Base class providing:
- Transaction-based test isolation (each test rolled back)
- WooCommerce helper methods
- Proper environment setup/teardown
Test Helpers
WC_Helper_Product
Factory for creating test products:
WC_Helper_Product::create_simple_product() — simple product with defaults
WC_Helper_Product::create_variation_product() — variable + variations
WC_Helper_Product::create_grouped_product() — grouped product
WC_Helper_Product::create_external_product() — external/affiliate
WC_Helper_Order
Factory for creating test orders:
WC_Helper_Order::create_order( $customer_id ) — order with line items
- Configurable status, products, addresses
WC_Helper_Customer
WC_Helper_Customer::create_customer() — customer with address
WC_Helper_Customer::create_mock_customer() — mock customer object
WC_Helper_Shipping
WC_Helper_Shipping::create_simple_flat_rate() — flat rate shipping zone
- Set up shipping zones and methods for checkout tests
WC_Helper_Coupon
WC_Helper_Coupon::create_coupon() — discount coupon with configurable type/amount
Testing Patterns
Testing Hooks
Verify your hooks fire correctly:
- Check that a callback is registered:
has_action( 'hook_name', [ $instance, 'method' ] )
- Test the callback's effect by triggering the hook
- Use
did_action( 'hook_name' ) to verify an action fired
Testing CRUD Operations
- Create an object (product, order, etc.)
- Perform operations (update, delete, query)
- Assert expected state
- Cleanup happens automatically via transaction rollback
Testing REST API Endpoints
Use WP_REST_Server to dispatch requests:
$request = new WP_REST_Request( 'GET', '/wc/v3/products' )
$response = rest_get_server()->dispatch( $request )
- Assert status code, response data, headers
Testing Payment Gateways
- Create order, set payment method
- Call
process_payment() with test credentials
- Assert order status changes
- Test refund flow
Mocking
- Use PHPUnit mocks:
$this->createMock( ClassName::class )
- Mock HTTP requests:
add_filter( 'pre_http_request', $mock_response )
- WooCommerce provides
WC_Mock_Payment_Gateway and similar mocks
E2E Testing with Playwright
Setup
WooCommerce uses @woocommerce/e2e-utils and Playwright:
- Configure in
playwright.config.js
- Tests in
tests/e2e/specs/
- Page objects for common pages (shop, cart, checkout, my-account)
Common E2E Flows
- Add product to cart → verify cart → proceed to checkout → complete order
- Customer registration and login
- Admin: create product, manage orders, configure settings
- Payment gateway integration testing
Best Practices
- Use
WC_Unit_Test_Case for integration tests
- Use WooCommerce helper classes (
WC_Helper_Product, etc.) for test fixtures
- Test with HPOS enabled and disabled
- Test with both classic and block checkout
- Mock external API calls (payment processors, shipping carriers)
- Use
@group annotations for test organization
- Clean up created data (helpers handle this via transaction rollback)
- Run tests in CI with WordPress test suite
Fetch the WooCommerce testing documentation and WordPress testing handbook for exact helper methods, bootstrap setup, and E2E configuration before implementing.
1---2name: woo-testing3description: Test WooCommerce extensions — PHPUnit unit/integration tests, WP test suite, WooCommerce test helpers, E2E with Playwright, and WP-CLI test scaffolding. Use when writing tests for WooCommerce plugins or setting up a test environment.4allowed-tools: Read, Write, Edit, Bash, Grep, Glob, WebSearch, WebFetch5metadata:6 source: https://skillmd.com/skills/orcaqubits/woo-testing7---89# WooCommerce Testing1011## Before writing code1213**Fetch live docs**:141. Web-search `site:developer.woocommerce.com testing` for WooCommerce testing guide152. Web-search `site:developer.wordpress.org plugins testing` for WordPress Plugin testing handbook163. Web-search `woocommerce e2e testing playwright` for E2E patterns1718## Test Types1920### Unit Tests2122Isolated class/function testing:23- No WordPress/WooCommerce bootstrap24- Mock all dependencies25- Fast execution26- Test pure business logic2728### Integration Tests2930Tests with WordPress + WooCommerce loaded:31- Extend `WC_Unit_Test_Case` (which extends `WP_UnitTestCase`)32- Full WordPress database (using transactions for rollback)33- Access to all WooCommerce APIs and hooks34- Test plugin integration with WooCommerce3536### E2E (End-to-End) Tests3738Browser-based tests with Playwright:39- Test complete user flows (add to cart, checkout, payment)40- WooCommerce provides Playwright utilities41- Run against a real WordPress + WooCommerce instance4243## Setting Up PHPUnit Tests4445### Scaffold with WP-CLI4647`wp scaffold plugin-tests my-plugin` creates:48- `tests/bootstrap.php` — test bootstrap49- `tests/test-sample.php` — sample test50- `phpunit.xml.dist` — PHPUnit configuration51- `bin/install-wp-tests.sh` — install WordPress test suite5253### WooCommerce Test Bootstrap5455After WordPress test bootstrap, load WooCommerce and your plugin:56```php57// In tests/bootstrap.php58require_once dirname( __DIR__ ) . '/vendor/woocommerce/woocommerce/tests/legacy/bootstrap.php';59// Or manually: activate WooCommerce, then your plugin60```6162### WC_Unit_Test_Case6364Base class providing:65- Transaction-based test isolation (each test rolled back)66- WooCommerce helper methods67- Proper environment setup/teardown6869## Test Helpers7071### WC_Helper_Product7273Factory for creating test products:74- `WC_Helper_Product::create_simple_product()` — simple product with defaults75- `WC_Helper_Product::create_variation_product()` — variable + variations76- `WC_Helper_Product::create_grouped_product()` — grouped product77- `WC_Helper_Product::create_external_product()` — external/affiliate7879### WC_Helper_Order8081Factory for creating test orders:82- `WC_Helper_Order::create_order( $customer_id )` — order with line items83- Configurable status, products, addresses8485### WC_Helper_Customer8687- `WC_Helper_Customer::create_customer()` — customer with address88- `WC_Helper_Customer::create_mock_customer()` — mock customer object8990### WC_Helper_Shipping9192- `WC_Helper_Shipping::create_simple_flat_rate()` — flat rate shipping zone93- Set up shipping zones and methods for checkout tests9495### WC_Helper_Coupon9697- `WC_Helper_Coupon::create_coupon()` — discount coupon with configurable type/amount9899## Testing Patterns100101### Testing Hooks102103Verify your hooks fire correctly:104- Check that a callback is registered: `has_action( 'hook_name', [ $instance, 'method' ] )`105- Test the callback's effect by triggering the hook106- Use `did_action( 'hook_name' )` to verify an action fired107108### Testing CRUD Operations1091101. Create an object (product, order, etc.)1112. Perform operations (update, delete, query)1123. Assert expected state1134. Cleanup happens automatically via transaction rollback114115### Testing REST API Endpoints116117Use `WP_REST_Server` to dispatch requests:118- `$request = new WP_REST_Request( 'GET', '/wc/v3/products' )`119- `$response = rest_get_server()->dispatch( $request )`120- Assert status code, response data, headers121122### Testing Payment Gateways123124- Create order, set payment method125- Call `process_payment()` with test credentials126- Assert order status changes127- Test refund flow128129### Mocking130131- Use PHPUnit mocks: `$this->createMock( ClassName::class )`132- Mock HTTP requests: `add_filter( 'pre_http_request', $mock_response )`133- WooCommerce provides `WC_Mock_Payment_Gateway` and similar mocks134135## E2E Testing with Playwright136137### Setup138139WooCommerce uses `@woocommerce/e2e-utils` and Playwright:140- Configure in `playwright.config.js`141- Tests in `tests/e2e/specs/`142- Page objects for common pages (shop, cart, checkout, my-account)143144### Common E2E Flows145146- Add product to cart → verify cart → proceed to checkout → complete order147- Customer registration and login148- Admin: create product, manage orders, configure settings149- Payment gateway integration testing150151## Best Practices152153- Use `WC_Unit_Test_Case` for integration tests154- Use WooCommerce helper classes (`WC_Helper_Product`, etc.) for test fixtures155- Test with HPOS enabled and disabled156- Test with both classic and block checkout157- Mock external API calls (payment processors, shipping carriers)158- Use `@group` annotations for test organization159- Clean up created data (helpers handle this via transaction rollback)160- Run tests in CI with WordPress test suite161162Fetch the WooCommerce testing documentation and WordPress testing handbook for exact helper methods, bootstrap setup, and E2E configuration before implementing.