PHPunit test with --filter parameter no executed

PHPunit test with --filter parameter no executed

by Alvaro Larrinaga -
Number of replies: 4

Hello everyone,

A newbie question. I have not worked with phpunit test before and am following the tutorial on Moodleacademy to run phpunit test:
Course Unit Test: https://moodle.academy/mod/book/view.php?id=1162&chapterid=1154

My Moodle version is 4.2 and I have configured a new database and moodledata directory for tests in the config.php file.
If I run the command
$ ./vendor/bin/phpunit
without parameters, the script checks all the tests (20158 in total), and it takes several hours to process.

But if I want to run a single test, and I use the --filter parameter, the test is not excuted.

$vendor/bin/phpunit --filter tool_dataprivacy_metadata_registry_testcase
Moodle 4.2dev (Build: 20221128), 197aff8ec7551a497cbc5177dc1b606a7084640e
Php: 8.0.13, mysqli: 5.7.36, OS: Windows NT 10.0 AMD64
PHPUnit 9.5.25 #StandWithUkraine

No tests executed!


Do I have to configure something special to run an individual test?
Thanks in advance

Average of ratings: -
In reply to Alvaro Larrinaga

Re: PHPunit test with --filter parameter no executed

by Alvaro Larrinaga -

oh, I see that this particular unit test doesn't exist in my local moodle files, so it stands to reason that it doesn't run anything.
Other tests are executed individually without problems.

My apologies for not reviewing it better before posting it on the forum!

In reply to Alvaro Larrinaga

Re: PHPunit test with --filter parameter no executed

by Eloy Lafuente (stronk7) -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers Picture of Testers

Hola Alvaro,

note that you have different options to execute a subset of all the available tests:

  • --filter: where you can specify any test class name or method name (good for individuals).
  • --testsuite: where you can specify any of the test suites specified in the phpunit.xml file (good for complete component/plugin).
  • In recent versions of moodle, the path/to/the/tests/file_test.php (good in a number of situations too).

There are also other options (vendor/bin/phpunit --help will tell you), but I think that the 3 above are the most commonly used.

Ciao smile

Average of ratings: Useful (1)
In reply to Eloy Lafuente (stronk7)

Re: PHPunit test with --filter parameter no executed

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Just to add to what Eloy said, regarding the --testsuite parameter, there is a testsuite made for each plugin. So, the most useful form is

--testsuite tool_dataprivacy_testsuite

to run all the tests from that plugin. That can be combined with --filter, if you want. (You probably already know that.)
Average of ratings: Useful (1)
In reply to Tim Hunt

Re: PHPunit test with --filter parameter no executed

by Alvaro Larrinaga -
Hi Tim, Hi Eloy. Cleared up. Thank you very much for your recommendations smile