Skip to content
Rémy Perona edited this page Dec 13, 2019 · 3 revisions

WP Rocket currently has a unit & integration tests suite, based on PHPUnit, Brain Monkey and the WordPress test suite.

Tests Organization

File Structure

Tests are located in the tests directory, under the Unit and Integration sub-directories. The file structure should follow the same structure as the codebase, and each tests for a method should be split under their own file. Here is an example:

  • We are testing the methods rewrite() and rewrite_url() from the WP_Rocket\CDN\CDN class
  • The file containing the tests for rewrite() is located in the directory tests/Unit/CDN/CDN with the name testRewrite.php
  • the file containg the tests for rewrite_url() is located in the directory tests/Unit/CDN/CDN with the name testRewriteURL.php

Content of a Test File

Each test file is focused on testing one method from a class, with one or more tests and assertions.

Base Structure

All tests for a method are contained inside a class, starting with Test and the name of the method tested. Example: class TestRewrite

For Unit tests, the class should extends the WP_Rocket\Tests\TestCase class that implements some common setup for unit tests specifically.

Naming Convention

Test methods names should start with test followed by a descriptive wording of what the test is doing. A good formula to follow is as below:

test should do { the expected behavior } when { this condition(s) } occurs

An example name for a test method: testShouldRewriteURLToCDNWhenHomeContainsSubdir()

Unit Test Class Boilerplate

namespace WP_Rocket\Tests\Unit\TestedClass;

use WP_Rocket\Tests\Unit\TestCase;
use Brain\Monkey\Functions;

class TestMethodTested extends TestCase {
    public function testShouldDoSomethingWhenCondition() {}
}

Integration Test Class Boilerplate

namespace WP_Rocket\Tests\Integration\TestedClass;

use WP_Rocket\Tests\Integration\TestCase;

class TestMethodTested extends TestCase {
    public function testShouldDoSomethingWhenCondition() {}
}

Testing Strategy

Only test public methods. Private methods behaviours will be asserted by the result of the public methods using them.