wordpress-testing-qa
WordPress plugin and theme testing with PHPUnit integration tests, WP_Mock unit tests, PHPCS coding standards, and CI/CD workflows
By bobmatnyc · 350 installs
npx skills add bobmatnyc/claude-mpm-skills --skill wordpress-testing-qa
Source repository · Upstream listing
WordPress Testing & Quality Assurance
progressive disclosure:
entry point:
summary: "WordPress plugin and theme testing with PHPUnit, WP Mock, PHPCS, and CI/CD for quality assurance"
when to use:
"Testing WordPress plugins with PHPUnit integration tests"
"Unit testing without loading WordPress core (WP Mock)"
"Enforcing coding standards with PHPCS"
quick start:
"Set up PHPUnit with WordPress test suite"
"Write unit tests with WP Mock"
"Configure PHPCS with WPCS ruleset"
Testing Strategy
Testing Pyramid for WordPress
The WordPress Testing Hierarchy:
Test Distribution Guidelines:
Unit Tests (60%): Fast, isolated, no WordPress
Pure PHP functions
Class methods with clear inputs/outputs
Business logic without side effects
Integration Tests (30%): WordPress loaded tests
Database operations
Hook/filter interactions
Custom post type registration
Settings API functionality
E2E Tests (10%): Browser automation
Critical user workflows
Admin panel interactions
Frontend form submissions
When to Use PHPUnit vs WP Mock
Use PHPUnit (Integration Tests) when:
✅ Testing database operations ( $wpdb , post creation, meta data)
✅ Testing WordPress hooks (actions/filters actually firing)
✅ Testing template rendering and output
✅ Testing plugin activation/deactivation logic
✅ Testing with actual WordPress functions
Use WP Mock (Unit Tests) when:
✅ Testing pure business logic
✅ Testing functions that call WordPress functions but logic is independent
✅ Need fast test execution (no database setup)
✅ Testing in isolation without side effects
✅ Mocking external API calls
Test Coverage Goals
Minimum Coverage Requirements:
New Code: 80% minimum coverage
Critical Paths: 95% coverage (payment processing, authentication, data validation)
Legacy Code: Gradual improvement, prioritize high risk areas
Public APIs: 100% coverage for all public methods
What to Test (Priority Order):
1. Security Functions: Nonce verification, sanitization, capability checks
2. Data Operations: Database CRUD, data validation, transformation
3. Business Logic: Calculations, workflows, state transitions
4. Hook Callbacks: Action/filter handlers
5. Public APIs: REST endpoints, WP CLI commands
What NOT to Test:
❌ WordPress core functions (assume they work)
❌ Third party library internals
❌ Simple getters/setters with no logic
❌ Configuration files (theme.json, block.json)
PHPUnit Integration Testing
WordPress Test Suite Setup
Step 1: Install Dependencies
Step 2: Install WordPress Test Library
Step 3: Configure phpunit.xml.dist
WP UnitTestCase Base Class
tests/bootstrap.php:
Factory Objects for Test Data
Using Built in Factories:
Available Factory Objects:
$this factory post Posts, pages, custom post types
$this factory user Users with roles
$this factory term Terms (categories, tags, custom taxonomies)
$this factory category Categories specifically
$this factory tag Tags specifically
$this factory comment Comments
$this factory blog Multisite blogs
Database Fixtures and Teardown
setUp() and tearDown() Methods:
setUpBeforeClass() and tearDownAfterClass():
Complete Plugin Test Example
tests/test plugin functionality.php:
WP Mock Unit Testing
What is WP Mock and When to Use It
WP Mock Purpose:
Test PHP code without loading WordPress
Mock WordPress functions to return expected values
Verify WordPress functions are called with correct arguments
Much faster than integration tests (no database setup)
When to Use WP Mock:
✅ Perfect for:
Pure business logic that calls WordPress functions
Data transformation/validation functions
Service classes with WordPress dependencies
Testing in continuous integration (faster CI builds)
❌ NOT Suitable for:
Testing actual database operations
Testing hook interactions between plugins
Testing template rendering
Testing functions that rely on WordPress state
Installation and Setup
tests/bootstrap wp mock.php:
phpunit wp mock.xml.dist:
Mocking WordPress Functions
tests/unit/test data processor.php:
Mocking Filters and Actions
Testing add filter() Calls:
Testing in Isolation (No WordPress Dependency)
Example: Email Service Class:
Unit Test Without WordPress:
PHPCS & Coding Standards
Installing PHPCS and WPCS
via Composer (Recommended):
.phpcs.xml.dist Configuration
Complete Configuration File:
Running PHPCS and PHPCBF
Command Line Usage:
composer.json Scripts:
Pre commit Hooks
Install pre commit hook (.git/hooks/pre commit):
Make hook executable:
IDE Integration
Visual Studio Code (.vscode/settings.json):
PHPStorm Configuration:
1. Go to Settings → PHP → Quality Tools → PHP CodeSniffer
2. Set Configuration path: {PROJECT ROOT}/vendor/bin/phpcs
3. Go to Settings → Editor → Inspections → PHP → Quality Tools
4. Enable "PHP CodeSniffer validation"
5. Set Coding standard: "Custom"
6. Set Path: {PROJECT ROOT}/.phpcs.xml.dist
GitHub Actions CI/CD
Workflow File Structure
.github/workflows/tests.yml:
Matrix Testing (Multiple PHP/WP Versions)
Strategy Explanation:
Matrix Results:
Creates 18 test jobs (3 PHP × 6 WordPress versions)
Ensures compatibility across supported versions
Identifies version specific issues early
PHPCS Checks in CI
Dedicated PHPCS Job:
PHPUnit Test Execution
With Code Coverage:
Coverage Reporting
Codecov Integration:
Coveralls Integration:
Complete Workflow Example
.github/workflows/ci.yml (Production Ready):
Testing Best Practices
Test Naming Conventions
Method Naming Pattern:
Examples:
Class Naming:
Arrange Act Assert Pattern
Structure Every Test:
Complete Example:
Data Providers
Purpose: Test same logic with multiple inputs
Complex Data Provider:
Testing Hooks and Filters
Testing add action/add filter:
Testing Hook Callbacks:
Testing AJAX Handlers
AJAX Test Setup:
Common Testing Patterns
Testing Custom Post Types
Testing Settings/Options
Testing Database Operations
Testing REST API Endpoints
Related Skills:
When testing WordPress applications, consider these complementary skills (available in the skill library):
WordPress Plugin Fundamentals : Core plugin architecture and hooks essential foundation for understanding what to test
WordPress Security & Validation : Security patterns and data validation critical for security testing strategies
Python pytest Testing : Modern testing patterns concepts applicable to WordPress testing approaches
GitHub Actions CI/CD : CI/CD automation integrate WordPress tests into automated pipelines
Further Reading:
[WordPress PHPUnit Documentation](https://make.wordpress.org/core/handbook/testing/automated testing/writing phpunit tests/)
[WP Mock GitHub Repository](https://github.com/10up/wp mock)
[WordPress Coding Standards](https://developer.wordpress.org/coding standards/)
[PHPUnit Documentation](https://phpunit.de/documentation.html)