Testing Overview
WooCommerce uses multiple testing frameworks to ensure code quality:PHPUnit
Unit and integration tests for PHP code
Jest
Unit tests for JavaScript and React components
Playwright
End-to-end and API testing
Test Environment Setup
WooCommerce uses@wordpress/env (wp-env) to create isolated Docker-based testing environments.
Prerequisites
- Docker installed and running
- PNPM installed
- Sufficient disk space for Docker containers
Environment Commands
Run these commands fromplugins/woocommerce directory:
Each plugin in the monorepo has its own isolated environment to prevent conflicts.
Environment Details
- PHP Version: 8.1 (default), configurable per test
- WordPress: Latest version (default), configurable
- PHPUnit: Version 9.6.24
- URL: http://localhost:8888 (default)
- Admin: http://localhost:8888/wp-admin (username:
admin, password:password)
PHPUnit Tests
PHPUnit tests validate PHP code functionality, including classes, functions, and WordPress integration.Running PHPUnit Tests
- Local (without Docker)
- Docker Environment
- Specific Tests
PHPUnit Test Structure
Tests are organized to mirror the source code structure:Writing PHPUnit Tests
1
Create Test Class
tests/php/src/Internal/Admin/OrdersTest.php
2
Write Test Methods
3
Use Factory Methods
PHPUnit Configuration
Thephpunit.xml file configures PHPUnit:
phpunit.xml
JavaScript/Jest Tests
Jest tests validate JavaScript and React component functionality.Running Jest Tests
Writing Jest Tests
- Component Tests
- Hook Tests
- Utility Tests
OrderStatus.test.js
End-to-End (E2E) Tests
Playwright tests simulate real user interactions in a browser environment.Running E2E Tests
Running API Tests
Writing E2E Tests
1
Create Test File
tests/e2e-pw/tests/merchant/create-order.spec.ts
2
Write Test Cases
E2E Test Helpers
Performance Tests
K6 performance tests measure system performance under load.Test Coverage Requirements
When contributing code, ensure appropriate test coverage:What to Test
- New Features
- Bug Fixes
- Refactoring
- Unit tests for all new classes and methods
- Integration tests for WordPress interactions
- E2E tests for user-facing functionality
- API tests for new endpoints
Coverage Guidelines
Aim for:
- 80%+ code coverage for new PHP classes
- 100% coverage for critical payment/order processing code
- E2E tests for all user-facing features
- API tests for all REST API endpoints
CI/CD Testing
All pull requests automatically run tests in CI:Automated Test Runs
- PHP Unit Tests: PHP 7.4, 8.5 with WordPress latest/latest-1
- E2E Tests: Multiple shards for parallel execution
- API Tests: Full API test suite
- Linting: PHP, JavaScript, and markdown linting
- PHPStan: Static analysis for type safety
Test Environments
CI tests run in multiple configurations:Debugging Tests
PHPUnit Debugging
Playwright Debugging
Common Testing Issues
Docker not running
Docker not running
Ensure Docker Desktop is running before starting wp-env:
Port conflicts
Port conflicts
If port 8888 is in use, wp-env will fail to start:
Database reset needed
Database reset needed
Sometimes tests fail due to database state:
Chromium install fails
Chromium install fails
If Playwright browser installation fails:
Next Steps
Git Workflow
Learn the Git workflow for committing and creating PRs
Coding Standards
Review coding standards and linting requirements