|
|
| (94 intermediate revisions by 26 users not shown) |
| Line 1: |
Line 1: |
| {{Moodle 2.3}} | | {{Template:Migrated|newDocId=/docs/guides/testing/}} |
| | |
| Moodle PHPUnit integration is designed to allow easy adding of new tests. At the start of each test the state is automatically reset to fresh new installation (unless explicitly told not to reset).
| |
| | |
| =Testcase classes=
| |
| | |
| There are two basic test class that are supposed to used in all Moodle unit tests - basic_testcase and advanced_testcase.
| |
| | |
| ;basic_testcase : Very simple tests that do not modify database, dataroot or any PHP globals. It can be used for example when trying examples from the official PHPUnit tutorial.
| |
| ;advanced_testcase : Enhanced testcase class enhanced for easy testing of Moodle code.
| |
| | |
| There is a third testcase class that is specially designed for testing of our Moodle database layer, it should not be used for other purposes.
| |
| | |
| ==Sample plugin testcase== | |
| | |
| PHPUnit tests are located in <code>tests/*_test.php</code> files in your plugin, for example mod/myplugin/tests/myplugin_test.php, the file should contain only one class that extends <code>advanced_testcase</code>:
| |
| | |
| <code php>
| |
| class mod_myplugin_testcase extends advanced_testcase {
| |
| public function test_adding() {
| |
| $this->assertEquals(2, 1+2);
| |
| }
| |
| }
| |
| </code>
| |
| | |
| ==Automatic state reset==
| |
| By default after each test Moodle database and dataroot is automatically reset to the original state which was present right after installation. make sure to use $this->resetAfterTest() to indicate that the database or changes of standard global variables are expected.
| |
| | |
| <code php>
| |
| class mod_myplugin_testcase extends advanced_testcase {
| |
| public function test_deleting() {
| |
| global $DB;
| |
| $this->resetAfterTest(true);
| |
| $DB->delete_records('user');
| |
| $this->assertEmpty($DB->get_records('user'));
| |
| }
| |
| public function test_tables_reset() {
| |
| global $DB;
| |
| $this->assertNotEmpty($DB->get_records('user'));
| |
| }
| |
| }
| |
| </code>
| |
| | |
| =Generators=
| |
| | |
| Tests that need to modify default installation may use generators to create new courses, users, etc. All examples on this page should be used from test methods of a test class derived from advanced_testcase.
| |
| | |
| ==Creating users==
| |
| At the start of each test there are only two users present - guest and administrator. If you need to add more test accounts use:
| |
| <code php>
| |
| $user = $this->getDataGenerator->create_user();
| |
| </code>
| |
| | |
| You may also specify properties of the user account, for example:
| |
| <code php>
| |
| $user1 = $this->getDataGenerator->create_user(array('email'=>'user1@example.com', 'username'=>'user1');
| |
| </code>
| |
| | |
| By default no user is logged-in, use setUser() method to change current $USER value:
| |
| <code php>
| |
| $this->setUser($user1);
| |
| </code>
| |
| | |
| Guest and admin accounts have a shortcut methods:
| |
| <code php>
| |
| $this->setGuestUser();
| |
| $this->setAdminUser();
| |
| </code>
| |
| | |
| Null can be used to set current user back to not-logged-in:
| |
| <code php>
| |
| $this->setUser(null);
| |
| </code>
| |
| | |
| ==Creating course categories==
| |
| | |
| <code php>
| |
| $category1 = $generator->create_category();
| |
| $category2 = $generator->create_category(array('name'=>'Some subcategory', 'parent'=>$category1->id));
| |
| </code>
| |
| | |
| ==Creating courses==
| |
| | |
| <code php>
| |
| $course1 = $generator->create_course();
| |
|
| |
| $category = $generator->create_category();
| |
| $course2 = $generator->create_course(array('name'=>'Some course', 'category'=>$category->id));
| |
| </code>
| |
| | |
| ==Creating activities==
| |
| | |
| Some activity plugins include instance generators. The generator class are defined in plugindirectory/tests/generator/lib.php.
| |
| | |
| Example of creation of new course with one page resource:
| |
| | |
| <code php>
| |
| $course = $this->getDataGenerator()->create_course();
| |
| $generator = $this->getDataGenerator()->get_plugin_generator('mod_page');
| |
| $generator->create_instance(array('course'=>$course->id));
| |
| </code>
| |
| | |
| ==Creating cohorts==
| |
| | |
| Since 2.4 there the data generator supports creation of new cohorts.
| |
| | |
| <code php>
| |
| $cohort = $this->getDataGenerator()->create_cohort();
| |
| </code>
| |
| | |
| =Long tests=
| |
| | |
| All standard test should execute as fast as possible. Tests that take a loner time to execute (>10s) or are otherwise expensive (such as querying external servers that might be flooded by all dev machines) should be execute only when PHPUNIT_LONGTEST is true. This constant can be set in phpunit.xml or directly in config.php.
| |
| | |
| =See also=
| |
| * [[PHPUnit integration]]
| |
| | |
| [[Category:Unit testing]]
| |