1 |
efrain |
1 |
PHPUnit testing support in Moodle
|
|
|
2 |
==================================
|
|
|
3 |
|
|
|
4 |
|
|
|
5 |
Documentation
|
|
|
6 |
-------------
|
|
|
7 |
* [Moodle PHPUnit integration](https://moodledev.io/general/development/tools/phpunit)
|
|
|
8 |
* [Moodle Writing PHPUnit tests](https://moodledev.io/general/development/tools/phpunit#writing-new-tests)
|
|
|
9 |
* [PHPUnit online documentation](http://www.phpunit.de/manual/current/en/)
|
|
|
10 |
* [Composer dependency manager](http://getcomposer.org/)
|
|
|
11 |
|
|
|
12 |
|
|
|
13 |
Composer installation
|
|
|
14 |
---------------------
|
|
|
15 |
Composer is a dependency manager for PHP projects.
|
|
|
16 |
It installs PHP libraries into /vendor/ subdirectory inside your moodle dirroot.
|
|
|
17 |
|
|
|
18 |
1. install Composer - [http://getcomposer.org/doc/00-intro.md](http://getcomposer.org/doc/00-intro.md)
|
|
|
19 |
2. install PHUnit and dependencies - go to your Moodle dirroot and execute `php composer.phar install`
|
|
|
20 |
|
|
|
21 |
|
|
|
22 |
Configure your server
|
|
|
23 |
---------------------
|
|
|
24 |
You need to create a new dataroot directory and specify a separate database prefix for the test environment,
|
|
|
25 |
see config-dist.php for more information.
|
|
|
26 |
|
|
|
27 |
* add `$CFG->phpunit_prefix = 'phpu_';` to your config.php file
|
|
|
28 |
* and `$CFG->phpunit_dataroot = '/path/to/phpunitdataroot';` to your config.php file
|
|
|
29 |
|
|
|
30 |
|
|
|
31 |
Initialise the test environment
|
|
|
32 |
-------------------------------
|
|
|
33 |
Before first execution and after every upgrade the PHPUnit test environment needs to be initialised,
|
|
|
34 |
this command also builds the phpunit.xml configuration files.
|
|
|
35 |
|
|
|
36 |
* execute `php admin/tool/phpunit/cli/init.php`
|
|
|
37 |
|
|
|
38 |
|
|
|
39 |
Execute tests
|
|
|
40 |
--------------
|
|
|
41 |
* execute `vendor/bin/phpunit` from dirroot directory
|
|
|
42 |
* you can execute a single test case class using class name followed by path to test file `vendor/bin/phpunit lib/tests/phpunit_test.php`
|
|
|
43 |
* it is also possible to create custom configuration files in xml format and use `vendor/bin/phpunit -c mytestsuites.xml`
|
|
|
44 |
|
|
|
45 |
|
|
|
46 |
How to add more tests?
|
|
|
47 |
----------------------
|
|
|
48 |
1. create `tests/` directory in your add-on
|
|
|
49 |
2. add test file, for example `local/mytest/tests/my_test.php` file with `my_test` class that extends `basic_testcase` or `advanced_testcase`
|
|
|
50 |
3. set the test class namespace to that of the class being tested
|
|
|
51 |
4. add some `test_*()` methods
|
|
|
52 |
5. execute your new test case `vendor/bin/phpunit local/mytest/tests/my_test.php`
|
|
|
53 |
6. execute `php admin/tool/phpunit/cli/init.php` to get the plugin tests included in main phpunit.xml configuration file
|
|
|
54 |
|
|
|
55 |
|
|
|
56 |
Windows support
|
|
|
57 |
---------------
|
|
|
58 |
* use `\` instead of `/` in paths in examples above
|