Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

Web Services Unit Test: Difference between revisions

From MoodleDocs
No edit summary
m (Protected "Web Services Unit Test": Developer Docs Migration ([Edit=Allow only administrators] (indefinite)))
 
(21 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{Moodle 2.3}}Since Moodle 2.3 we are using PHP Unit framework. Writing a unit test before writing an external function is very helpful. This is due to the fact that the easiest external function is not an easy task, you most likely discover this page through [[How_to_contribute_a_web_service_function_to_core]]. Writing PHPUnit you will:
{{Template:Migrated|newDocId=/docs/apis/subsystems/external/testing}}
* discover use cases you didn't think about.
* understand the feelings and the needs of the web service client developer.
* end up with a function usable by everybody, not only by your own client.
* reach integration way faster as you joined a proof of validity
* make the QA process a breeze
 
== How to run the PHPUnit ==
you should read first [[PHPUnit]] documentation to have a grasp of PHPUnit in Moodle/
 
== How to write an external function PHPUnit test ==
If it doesn't exist create a COMPONENTFOLDER/tests/COMPONENTexternallib_test.php file.
 
<code php>
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
 
/**
* COMPONENT External functions unit tests
*
* @package    core
* @category  phpunit
* @copyright  20XX Your Name
* @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
 
defined('MOODLE_INTERNAL') || die();
 
 
class COMPONENTexternallib_testcase extends advanced_testcase {
   
    /**
    *
    */
    protected function setUp() {
        global $CFG;
        require_once($CFG->dirroot . '/COMPONENT/externallib.php'); 
    }
 
    /**
    * Test
    */
    public function test_FUNCTION_NAME() {
        global $USER;
 
        $this->resetAfterTest(true);
 
        $params = array(PARAM1, PARAM2, ...);
 
        $returnvalue = COMPONENT_external::FUNCTION_NAME($params);
 
        // Some PHPUnit assert
        $this->assertEquals(EXPECTED_VALUE, RETURNED_VALUE);
    }
}
</code>
 
But the quickest way is most likely to look at some example like course/tests/courseexternallib_test.php. Also read [[Writing_PHPUnit_tests]].
 
=== Coding style ===
* external functions often check many capabilities. Remember to assign the correct one to the $USER and also test when the $USER doesn't have them.
 
 
 
[[Category:Unit testing]]

Latest revision as of 13:13, 31 December 2022

Important:

This content of this page has been updated and migrated to the new Moodle Developer Resources. The information contained on the page should no longer be seen up-to-date.

Why not view this page on the new site and help us to migrate more content to the new site!