diff --git a/docs/apis/subsystems/external/testing.md b/docs/apis/subsystems/external/testing.md index 02390fbc7..c218cf102 100644 --- a/docs/apis/subsystems/external/testing.md +++ b/docs/apis/subsystems/external/testing.md @@ -20,10 +20,18 @@ Writing unit tests for an external service function is no different to writing u ## How to write an external function PHPUnit test -You should create one unit test testcase for each external service file, and it should be named after the file that it tests. +You should create one unit test testcase for each external service class, and it should be named after the class that it tests. For example, if you have written a service function in `[componentfolder]/classes/external/get_fruit.php`, you should write a unit test in `[componentfolder]/tests/external/get_fruit_test.php`. +:::note External Services Testcase + +An external testcase has been created to make testing external services easier. + +When creating your tests, you can extend the `\core_external\tests\externallib_testcase` class instead of `\advanced_testcase`. + +::: + ```php title="mod/kitchen/tests/external/get_fruit_test.php" . +namespace mod_kitchen\external; + /** * Unit tests for the get_fruit function of the kitchen. * * @package mod_kitchen * @category external - * @copyright 20XX Your Name + * @copyright Your Name * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ - -namespace mod_kitchen\external; - -defined('MOODLE_INTERNAL') || die(); - -global $CFG; -require_once($CFG->dirroot . '/webservice/tests/helpers.php'); - -class get_fruit_test extends externallib_advanced_testcase { - +#[\PHPUnit\Framework\Attributes\CoversClass(get_fruit::class)] +class get_fruit_test extends \core_external\tests\externallib_testcase { /** * Test the execute function when capabilities are present. - * - * @covers \mod_fruit\external\get_fruit::execute */ public function test_capabilities(): void { $this->resetAfterTest(true); @@ -96,8 +96,6 @@ class get_fruit_test extends externallib_advanced_testcase { /** * Test the execute function when capabilities are missing. - * - * @covers \mod_fruit\external\get_fruit::execute */ public function test_capabilities_missing(): void { global $USER; diff --git a/docs/devupdate.md b/docs/devupdate.md index 98f76bbc2..8c612067a 100644 --- a/docs/devupdate.md +++ b/docs/devupdate.md @@ -28,3 +28,11 @@ The `maxsections` setting in course formats is now deprecated. Previously, this Although the `maxsections` setting remains available for now, it is marked as deprecated and will be removed in Moodle 6.0. Also, the `get_max_sections` from `core_courseformat\base` is also deprecated and will be removed in Moodle 6.0. If your format plugin relies on `maxsections`, you should add a custom setting in your plugin to control section limits. For reference, see the week format plugin, which now uses its own setting for this functionality. + +## Unit Testing + +### Testing External Service classes + + + +A new `\core_external\tests\externallib_testcase` has been introduced to replace the `\externallib_advanced_testcase` class. The new class can be autoloaded, and is available from Moodle 4.4 onwards.