PHPUnit in Moodle

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:

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值