Goal
Cover the store's business logic with tests: calculations, validation, integrations.
Key concepts
- Unit tests for pure logic (a fee calculation, discount rules) — without WordPress
- Integration tests with WordPress and WooCommerce:
WP_UnitTestCase, a test database - Environment:
@wordpress/env(wp-env) with WooCommerce in.wp-env.json; integration tests run inside it (wp-env run tests-cli) - The WordPress test suite works with PHPUnit 9 plus
yoast/phpunit-polyfills - E2E: Playwright for the cart and checkout (classic and block)
- Test behavior, not implementation: input → result in the cart or the order
Diagram
flowchart TB
E2E["E2E Playwright<br/>full checkout — few"] --> INT["Integration<br/>WP + WC — some"]
INT --> UNIT["Unit<br/>pure logic — many"]
Code
Pure logic that's easy to test:
<?php
namespace MyWcExtension\Pricing;
final class CodFee {
public function amount( float $cart_total ): float {
return $cart_total >= 2000 ? 0.0 : 30.0;
}
}
A unit test:
<?php
use MyWcExtension\Pricing\CodFee;
use PHPUnit\Framework\TestCase;
final class CodFeeTest extends TestCase {
public function test_fee_for_small_order(): void {
$this->assertSame( 30.0, ( new CodFee() )->amount( 500 ) );
}
public function test_free_from_2000(): void {
$this->assertSame( 0.0, ( new CodFee() )->amount( 2000 ) );
}
}
An integration test with WooCommerce:
<?php
final class SupplierCodeTest extends WP_UnitTestCase {
public function test_meta_saved_via_crud(): void {
$product = new WC_Product_Simple();
$product->set_name( 'Test' );
$product->set_regular_price( '100' );
$product->update_meta_data( '_supplier_code', 'SUP-1' );
$id = $product->save();
$this->assertSame( 'SUP-1', wc_get_product( $id )->get_meta( '_supplier_code' ) );
}
}
.wp-env.json:
{
"core": "WordPress/WordPress#6.8",
"plugins": [ "https://downloads.wordpress.org/plugin/woocommerce.latest-stable.zip", "." ],
"config": { "WP_DEBUG": true }
}
Common mistakes
| Symptom | Cause | Fix |
|---|---|---|
| Tests fail at random | Dependence on order or shared data | Each test has its own data set |
| The logic can't be tested | The calculation lives inside a hook callback | Move it to a class; the hook only calls it |
| E2E covers only the classic checkout | The site moved to blocks | Separate scenarios for both |
Weak and strong answer
Question: How do you test WooCommerce code?
Weak answer. I check it manually on staging.
Strong answer. I split it: business logic (calculations, rules) goes into classes covered by unit tests. The WooCommerce integration — tests on
WP_UnitTestCasein wp-env with WooCommerce. The critical purchase path — a few E2E tests on Playwright. CI runs the tests on every PR. Manual checks on staging stay, but as the last check, not the only one.
Why the second is stronger: The test pyramid, an architecture built for testing and automation.
Practice
In the terminal: install the dev dependencies, run the unit suite, start wp-env and run all the tests inside it.
On your own project: move the fee calculation from lesson 4.3 into a class and cover the boundary values with unit tests. Write an integration test for the "Supplier code" field.
Interview Questions
Move the logic into separate classes with pure methods, and only call them from the hook. Then unit tests cover the logic and integration tests cover the hook registration.
The @wordpress/env tool that runs WordPress in Docker with the plugins you need, from a .wp-env.json config. Handy for local development and for tests in CI.
The critical money path: add to cart, apply a coupon, place an order with a test payment method, get the thank-you page. For both classic and block checkout if both are used.