https://docs.moodle.org/dev/PHPUnit#Initialisation_of_test_environment
Installation of PHPUnit via Composer
1 go to https://getcomposer.org/download/ and download the Composer-Setup.exe file
2 run composer install --dev in moodle directory
Troubleshooting:
- On Windows if you are behind a proxy you will need to setup an environment variable called HTTP_PROXY with a value detailing your HTTP Proxy address and port before composer will correctly download files.
- You may be prompted for github credentials when installing composer dependencies.
- This is used to generate an personal access token to avoid being rate limited by github.
- If you have Two Factor Authentication enabled on your github account, or do not wish to supply your own credentials you will need to generate a token manually:
- Visit https://github.com/settings/applications and request personal access token
- Copy this token and add it to your ~/.composer/config.json
How to run only some tests
Running a single test quickly
The fastest way to run a single test is:
vendor/bin/phpunit my_test_class_name my/tests/filename.php
This is faster than other methods because it avoids the need to search for the test file, but you should be careful because it may be possible to run tests this way which are not included in the normal run. If you use this method, do at least one full test run (or --group run, as below) to ensure the test can be found.
Please note the path name is optional if the testcase class and file names are standardised as described in Writing PHPUnit tests.
Using the @group annotation
If you add annotations like
/**
* Unit tests for {@link stack_cas_keyval}.
* @group qtype_stack
*/
class qtype_stack_cas_keyval_exception_testcase extends basic_testcase {
to all the classes in your plugin, then you can run just the tests for your plugin by doing
phpunit --group qtype_stack
Therefore, it is suggested that you annotate all your tests with the Frankenstyle name of your plugin.
Using multiple phpunit.xml files
It's easy to create alternative phpunit.xml files defining which tests must be run together. For reference, take a look to the default /phpunit.xml available in your base directory once the testing environment has been initialised. After creating the custom file you will be able to run those tests with
vendor/bin/phpunit -c path/to/your/alternative/phpunit/file.xml
Also, for commodity, you can use this command:
php admin/tool/phpunit/cli/util.php --buildcomponentconfigs
It will, automatically, create one valid phpunit.xml file within each component (plugin or subsystem) and other important directories, so later you will be able to execute tests like
vendor/bin/phpunit -c mod/forum[/phpunit.xml] // Note that it's not needed to specify the name of the file (if it is 'phpunit.xml'). vendor/bin/phpunit -c question vendor/bin/phpunit -c question/type/calculated vendor/bin/phpunit -c backup vendor/bin/phpunit -c lib/dml ...
or, also
cd directory/with/phpunit.xml phpunit