-
Notifications
You must be signed in to change notification settings - Fork 219
Writing Tests
WP Rocket currently has a unit & integration tests suite, based on PHPUnit, Brain Monkey and the WordPress test suite.
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()
andrewrite_url()
from theWP_Rocket\CDN\CDN
class - The file containing the tests for
rewrite()
is located in the directorytests/Unit/CDN/CDN
with the nametestRewrite.php
- the file containg the tests for
rewrite_url()
is located in the directorytests/Unit/CDN/CDN
with the nametestRewriteURL.php
Each test file is focused on testing one method from a class, with one or more tests and assertions.
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.
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()
namespace WP_Rocket\Tests\Unit\TestedClass;
use WP_Rocket\Tests\Unit\TestCase;
use Brain\Monkey\Functions;
class TestMethodTested extends TestCase {
public function testShouldDoSomethingWhenCondition() {}
}
namespace WP_Rocket\Tests\Integration\TestedClass;
use WP_Rocket\Tests\Integration\TestCase;
class TestMethodTested extends TestCase {
public function testShouldDoSomethingWhenCondition() {}
}
Only test public methods. Private methods behaviours will be asserted by the result of the public methods using them.