Behat and custom roles

Behat and custom roles

by Rex Lorenzo -
Number of replies: 7

We are starting to write our own Behat tests and it has been going pretty well. 

However, one of the problems we are coming up against is the need to use custom roles while doing tests for our plugins. We have customized Instructor, Teaching Assistant, and several "guests" roles that we want to be automatically tested.

What is the best approach for loading custom roles in a Behat environment? We are running Moodle 2.5.4.

Average of ratings: -
In reply to Rex Lorenzo

Re: Behat and custom roles

by David Monllaó -

Hi Rex,

Good to hear that smile What you can do with the current step definitions is to follow the required step to create a new role, this would be useful if you would just need to test a couple of features related with your custom role, but I guess that you, like many other organizations using custom roles, would base most of your scenarios on these roles so you need a quicker way to set them up; if you are interested in always having the same custom roles in each scenario you can create a new step Given I add rex custom roles or even more generic Given I add rex data set in local/rexrules/tests/behat/behat_local_rexrules.php and use PHP code to generate all you need, using testing data generators or direct Moodle APIs calls, then each feature requiring these datasets can include it. Note that this approach (to use PHP code to set data) goes against the black-box principle of acceptance testing so, at a practical level, you should restrict the use of this steps to the Given section (settingthe scenario contexts).

An addition, what I would be happy to include in core would be a data generator for create roles, as I guess more organizations would be interested in using it. (https://tracker.moodle.org/browse/MDL-44108)

Regards,
David

Average of ratings: Useful (2)
In reply to David Monllaó

Re: Behat and custom roles

by Rex Lorenzo -

David, I took a look at the tracker ticket you created and this wouldn't exactly solve our problem. We already are currently just renaming the existing roles to match the role shortnames our custom code is looking for.

However, the next step we want to do automated testing is the certain capabilities that our custom roles have set.

I know that Moodle 2.6 introduced role export/import and would like to see some Moodle core support to specify the importing of these role definitions, similar to the way there are very easy backup/restore steps already defined that do all the heavy lifting of uploading a backup and selecting the appropriate settings.

In reply to Rex Lorenzo

Re: Behat and custom roles

by David Monllaó -

Hi Rex,

If you want to override capabilities we also have a data generator for it (http://docs.moodle.org/dev/Acceptance_testing#Available_elements - Permission overrides) you can automate it as I commented above using a rex custom step, or also you could add a step definition with the @BeforeScenario annotation so it will be auto executed, but I am thinking that you will need to ensure that it is executed after behat_hooks::before_scenario which resets the database and the dataroot directory status.

In reply to David Monllaó

Re: Behat and custom roles

by Urs Hunkler -
Picture of Core developers

David, I try to understand your feedback - can you please explain why you call the proposed feature "rexrules"? I guess the name has a meaning but I don't understand it may be important.

In reply to Urs Hunkler

Re: Behat and custom roles

by Itamar Tzadok -

Afaict the name doesn't have a particular meaning. The idea is that you can create tests for core components with steps that are not provided by core, by adding a local plugin (e.g. local_mysteps, local_davidrules etc.) defining the steps there and then writing the tests with those additional steps. smile

Average of ratings: Useful (1)
In reply to Itamar Tzadok

Re: Behat and custom roles

by Urs Hunkler -
Picture of Core developers

Thank you Itamar for the explanation. I thought "rexrules" might be a special name - I associated the name with REGEX wink