From 562c0cc93212e898d76e308908bba7fc66ecdd8f Mon Sep 17 00:00:00 2001 From: Ben Thomson Date: Tue, 28 May 2024 09:53:17 +0800 Subject: [PATCH] Plugin unit testing doc improvements - Tweak examples - Add instructions on running tests - Add instructions on setting up automated unit tests --- plugin/unit-testing.md | 115 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 108 insertions(+), 7 deletions(-) diff --git a/plugin/unit-testing.md b/plugin/unit-testing.md index f70e7b84..a53089df 100644 --- a/plugin/unit-testing.md +++ b/plugin/unit-testing.md @@ -55,24 +55,125 @@ plugins/ `-- blog/ |-- tests/ # The tests folder | |-- cases/ # The test cases folder - | | |-- BlogCategoryTestCase.php # An example test case - | | `-- BlogPostTestCase.php # Another example test case + | | |-- BlogCategoryTest.php # An example test case + | | `-- BlogPostTest.php # Another example test case | `-- fixtures/ # The test fixtures folder - | |-- first-blog.md # An example fixture - | `-- second-blog.md # Another example fixture + | |-- first-blog.md # An example fixture + | `-- second-blog.md # Another example fixture |-- Plugin.php `-- phpunit.xml ``` A unit test within a plugin should extend the `System\Tests\Bootstrap\TestCase` class. This base test case class includes several features for properly testing plugins, including running migrations for the plugins and ensuring that the environment is set up to properly test plugins. +In order to be picked up by PHPUnit, test cases must match the following conventions: + +- Test cases must be defined within a class ended with the `Test` suffix - generally, the test case should match the path and class name being tested, however only the suffix is required. For example, if you are testing a `Post` model, stored in the `models/Post.php` file in your plugin, it is best to define the test cases for this model within a class called `PostTest` and stored in the `tests/cases/models/PostTest.php` path. +- The test case class must extend the `System\Tests\Bootstrap\TestCase` class. +- Test case methods must be public and must be prefixed with the `test` prefix in the method name. Alternatively, the method must have a docblock that defines the `@test` annotation. +- Test cases must make at least one assertion. + +An example test case class is below: + ```php -