Moodle-CI errors with PHP Units tests

Moodle-CI errors with PHP Units tests

by Joseph Rézeau -
Number of replies: 4
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers Picture of Translators

Hi!

When running Moodle-CI on my github regex question type I am getting these error messages:

FOUND 2 ERRORS AFFECTING 2 LINES
------------------------------------------------------------------------------------------------------------------------------------
 44 | ERROR | [x] The setUp() method in unit tests must always call to parent::setUp().
    |       |     (moodle.PHPUnit.ParentSetUpTearDown.MissingSetUp)
 48 | ERROR | [x] The tearDown() method in unit tests must always call to parent::tearDown().
    |       |     (moodle.PHPUnit.ParentSetUpTearDown.MissingTearDown)

No idea what this means nor how I can fix the problem. Any idea anyone?

Average of ratings: -
In reply to Joseph Rézeau

Re: Moodle-CI errors with PHP Units tests

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Doesn't it say exactly what you need to do in the message?

Inside your unit tests, the function setUp() needs to make a call to parent::setUp().

i.e. the function should look something like:

public function setUp(): void {
    parent::setUp();
    // Any set up that your unit test needs.
}
Average of ratings:Useful (1)
In reply to Joseph Rézeau

Re: Moodle-CI errors with PHP Units tests

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

Hi,

for reference, you can find some more information and links @ MDL-81523, where all the cases have been recently fixed in core.

Basically, they are reported as (severe) errors, because it's a must to have all them fixed in order to be able to try different paths to upgrade to PHPUnit 10, where the setup/teardown "chains" (always calling to parent) probably will become a hard requirement in order to keep all the tests (and the internal Moodle's testing bootstrap and setup/reset between tests) working properly.

Apart from the above reason, that probably will be a requirement, it's also a good practice to, always, call to parent in those methods, only that way it can be guaranteed that all the structures created anywhere in the chain are properly managed (created and destroyed).

So, the sooner everybody get their tests fixed (BTW, note that those errors can be auto-fixed with phpcbf 99% of times), the better to become ready for any future PHPUnit upgrade.

Ciao smile

In reply to Eloy Lafuente (stronk7)

Re: Moodle-CI errors with PHP Units tests

by Joseph Rézeau -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers Picture of Translators

Thanks Davo and Eloy for your replies.

Adding parent::setUp(); and parent::tearDown(); does the trick. 

Not sure what this means "those errors can be auto-fixed with phpcbf 99% of times".

In reply to Joseph Rézeau

Re: Moodle-CI errors with PHP Units tests

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

Not sure what this means "those errors can be auto-fixed with phpcbf 99% of times".

The tool that we use to find all those coding problems (PHP_CodeSniffer) comes with 2 executables:

  • phpcs : Is the one in charge of detecting the problems and report them.
  • phpcbf: Is the ons in charge of auto-fixing (beautifying) all the problems reported as "fixable".

And, the first one is the one that is executed by the CodeChecker plugin, by moodle-plugin-ci when you use Travis or GitHub, and others...

And, all the rules/checks that are performed by those tools are defined in the Moodle-CS repository. We call to that the "moodle standard".

So, if you want to run locally the tests, before sending them to GitHub, or without using any other plugin, or if you want to get the problems being reported as soon as you are typing code in your favourite IDE... all you've to do is:

  1. Install moodle-cs, it will automatically install in your computer all the stuff needed to run the checks (or the fixes) locally.
  2. All the stuff will be installed in some directory like ~/.composer/vendor/bin or similar, ensure that you add that directory to your PATH, so the 2 executables are at hand.
  3. Then, just execute phpcs /path/to/your/plugin and you will get a list with all the problems.
  4. Or execute phpcbf /path/to/your/plugin and all the fixable errors will be auto-fixed.

That is, basically, how to run them (both the checker and the fixer), locally. Then they can be integrated in your IDE and other niceties, but that's bonus thing. First get used to them from terminal.

Ref.: https://moodledev.io/general/development/tools/phpcs

Ciao smile

Average of ratings:Useful (2)