Currently Available: Need a skilled Software Developer for your next project?
Categories
WordPress

Modern Testing Strategies for WordPress Plugins

A WordPress plugin runs inside a site, where its PHP code interacts with WordPress functions, the database, the browser, and other plugins and themes. Code that passes a test by itself can still fail when WordPress loads the plugin, saves its data, or displays its interface.

You need several kinds of tests, each aimed at a different part of the plugin. Fast code checks and focused unit tests catch local errors. Tests running inside WordPress show whether your plugin works with the platform. Browser tests check important tasks from a user's point of view. When one layer passes, it does not prove that the others work. You can run these checks in continuous integration (CI), an automated service that runs your test suite whenever code changes, and then make accurate compatibility claims.

Match each test to the behavior it checks

To understand a test's scope, look at what it runs and checks rather than relying on its label. WordPress Core’s testing guidance notes that the platform’s structure makes fully isolated tests difficult and that WordPress uses “unit test” loosely in some contexts. Define your tests by what they run and what they verify.

Test layer What it checks WordPress plugin example What it does not prove by itself
Static checks Source code without running the plugin Coding standards or suspicious code patterns That runtime behavior works
Isolated unit tests A small function or class with WordPress dependencies minimized or replaced A validation rule or formatter That WordPress hooks or database behavior work
WordPress integration tests Plugin behavior in a WordPress test installation A setting saves correctly or a post type registers That a user can complete the workflow in a browser
Browser tests A visible workflow on a running WordPress site Open a settings page, save a value, and confirm the result That every supported version or integration works
Manual checks Experience that is difficult to automate reliably Keyboard use, screen-reader behavior, or a theme conflict Repeatable regression protection unless findings become tests

A WordPress hook is a point where WordPress lets plugins run code or change behavior. For example, a plugin can register a callback on a hook to add a setting or alter displayed content. Test plugin-owned behavior at these connection points with WordPress running. You can test a pure formatting function separately when its result does not depend on WordPress.

Run PHP tests with WordPress when behavior depends on it

PHPUnit runs PHP tests by comparing actual results with expected results. When your plugin code calls WordPress functions or reads and writes to the database, the WordPress test framework gives you a WordPress environment and tools called factories. Factories create test data like posts and users. The WordPress Core testing guide explains how to use assertions, fixtures, and factories for this kind of work.

For example, a test for a custom post type can check that WordPress registers it with the expected properties, create a test post, and verify that WordPress handles it properly. Because that test depends on WordPress, calling it an integration test describes its scope more accurately than calling it an isolated unit test.

The WP-CLI test scaffold command generates a PHPUnit configuration, a test bootstrap, a sample test, an installation script, and CI configuration. Treat those files as a starting point, and check that the bootstrap, dependencies, PHP versions, and CI settings match your plugin’s support policy.

You can run tests with wp-env, which includes the WordPress PHPUnit test files for your installed WordPress version and provides an environment for tools like PHPUnit and WP-CLI. You can also set up tests through Composer. A WordPress Developer Blog tutorial walks through a Composer setup using wp-phpunit/wp-phpunit, which the tutorial notes is an unofficial, community-maintained fork. Because these two setups have different maintenance arrangements, pick the one that fits your plugin’s dependency policy and existing development setup instead of treating them as interchangeable.

Good PHP tests focus on the behavior your plugin owns. Cover input validation, settings, hooks, and error handling, along with permission-sensitive actions and past bug fixes. Include invalid and missing input cases wherever those values can reach your plugin. When you test a permission-sensitive action, check what happens when an unauthorized user tries to run it, in addition to checking the successful path.

Test JavaScript and browser workflows separately

JavaScript unit tests check small pieces of browser-side logic, such as a function that formats a value or picks which message to show. The current @wordpress/scripts documentation names Vitest as the default for new JavaScript unit tests. The package does not automatically include a simulated browser, React transformations, aliases, or CSS mocks, so you need to configure the test environment to match the code you are testing. Existing Jest setups still have a maintenance adapter, though the documentation states that new testing features target Vitest.

A browser end-to-end (E2E) test checks a user-visible workflow on a running WordPress site. For example, a Playwright test can activate a plugin, open its settings, submit a form, and confirm that the right result appears on screen. Keep these tests focused on high-value paths, such as a core settings task or an editor interaction that depends on WordPress’s interface. Because a browser test requires more setup than a focused unit test, save it for workflows that smaller tests cannot verify.

The @wordpress/scripts tools support Playwright tests with WP_BASE_URL or a configured wp-env environment. WordPress also documents a Playwright workflow with WordPress Playground that skips Docker and database setup. Playground can make local browser testing simpler, but the documented setup does not prove that it reproduces every hosting provider, production configuration, or plugin combination. Pick the environment that matches the behavior your test needs to verify.

What Plugin Check catches and what it misses

Plugin Check helps you review WordPress.org directory requirements and flags issues in areas like internationalization, accessibility, performance, and security practices. Its static checks inspect your source code, while its runtime checks execute selected plugin code.

This distinction matters in CI. Plugin Check’s WP-CLI command inspects source code by default, so you must load the plugin first if you want to run checks that execute plugin code. The project advises against running the checker in production. Treat the output as evidence about the specific checks that ran, rather than proof that every plugin behavior or security issue has been covered.

WordPress Coding Standards give you conventions for catching common issues and keeping code readable. For runtime diagnosis, Query Monitor shows database queries, hooks, HTTP requests, and redirects. Both tools help you spot problems, but neither replaces tests that verify your plugin’s expected behavior.

Test security and accessibility at their boundaries

WordPress security guidance recommends checking untrusted input, cleaning it when necessary, and escaping output so it is safe for the context where it appears. Those rules map directly to concrete tests. Submit malformed input and check how your plugin responds. Render user-supplied content and verify that your plugin escapes the output properly. Attempt a protected action without the required permission and confirm that your plugin refuses it.

A nonce is a value WordPress uses to help protect requests against certain forgery scenarios. WordPress explicitly states that a nonce does not provide authentication, authorization, or access control. When an action requires both a valid nonce and a user capability, test both checks. A valid nonce by itself must never grant a user permission they do not have. The WordPress nonce guidance explains this distinction in detail.

Automated accessibility checks can catch some interface problems, but a person still needs to test keyboard interaction, focus behavior, screen-reader use, and whether error messages make sense. WordPress’s accessibility standards specify WCAG 2.2 Level AA for code integrated into the WordPress ecosystem described there, including Core, WordPress.org websites, and official plugins. That page also calls for usability testing alongside accessibility testing. Use Plugin Check findings as prompts for a manual review rather than a complete conformance check.

Make CI and compatibility claims match the tests

A practical CI setup runs quick static checks on each change, PHP tests in a WordPress environment, JavaScript tests for browser-side logic, and a small set of Playwright workflows. Run Plugin Check before release, then manually review areas that automation cannot judge reliably, such as accessibility and compatibility-sensitive integrations. The WP-CLI scaffold and @wordpress/scripts give you building blocks for this workflow without prescribing a single required pipeline.

Choose a compatibility matrix, a set of WordPress and PHP versions to test, that matches your plugin’s stated support. Start by testing your oldest supported environment and a current supported environment, then add other combinations when a major dependency or feature behaves differently across versions. The exact WordPress and PHP versions depend on your plugin’s support policy. For WordPress.org listings, the Plugin Developer FAQ states that the “Tested up to” value should reflect the WordPress version you actually tested. Do not claim a version only because you expect the code to work. See also WordPress plugin version and dependency requirements.

Before release, check that your distributed package contains production files and leaves out unnecessary tests or development tools. The FAQ also notes that new submissions should be production-ready and under 10 MB. Those are packaging requirements rather than measures of test quality.

What a passing test cannot prove

No single test layer proves that a plugin is secure, accessible, compatible with every site, or free of defects. A WordPress integration test does not exercise every browser workflow. A Playwright test does not cover every supported PHP version or hosting environment. Plugin Check only reports the checks it implements. The available WordPress guidance also sets no universal code-coverage target or compatibility matrix for every plugin.

Use each passing result only for the claim it supports. A test that checks how a setting saves gives you confidence in that behavior under the tested environment. A manual screen-reader review supports a different kind of judgment. When you keep those claims precise, you can focus your tests on plugin-owned risks instead of treating a green CI run as a blanket guarantee.

What I'm building

Delegate tasks. Get software.

Give Vroni a GitHub issue, bug report, spec, or rough idea. It reads the repo, plans the change, writes code, runs checks, and works toward a review-ready pull request.

Take a look at vroni.com

Email updates

Usually a new article and a few links I found interesting.

No spam. Unsubscribe with one click.

Leave a Reply

Your email address will not be published. Required fields are marked *