Writing PHPUnit tests: Difference between revisions
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
{{Moodle 2.3}} | {{Moodle 2.3}} | ||
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) | 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). | ||
= | =Generators= | ||
Tests that need to modify default installation may use generators to create new courses, users, etc. | |||
==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> | |||
$user = $this->getDataGenerator->create_user(); | |||
</code> | |||
You may also specify properties of the user account, for example: | |||
<code> | |||
$user1 = $this->getDataGenerator->create_user(array('email'=>'user1@example.com', 'username'=>'user1'); | |||
</code> | |||
Use setUser() method to set current $USER: | |||
<code> | |||
$this->setUser($user1); | |||
</code> | |||
Guest and admin accounts have a shortcut methods: | |||
<code> | |||
$this->setGuestUser(); | |||
$this->setAdminUser(); | |||
</code> | |||
Null can be used to set not-logged-in user again: | |||
<code> | |||
$this->setUser(null); | |||
</code> | |||
==Creating course categories== | |||
==Creating course== | |||
==Creating activities== | |||
==Creating cohorts== | |||
=Long tests= | =Long tests= |
Revision as of 20:53, 21 September 2012
Moodle 2.3
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).
Generators
Tests that need to modify default installation may use generators to create new courses, users, etc.
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:
$user = $this->getDataGenerator->create_user();
You may also specify properties of the user account, for example:
$user1 = $this->getDataGenerator->create_user(array('email'=>'user1@example.com', 'username'=>'user1');
Use setUser() method to set current $USER:
$this->setUser($user1);
Guest and admin accounts have a shortcut methods:
$this->setGuestUser();
$this->setAdminUser();
Null can be used to set not-logged-in user again:
$this->setUser(null);
Creating course categories
Creating course
Creating activities
Creating cohorts
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.