Skip to content

Commit 2a3f14f

Browse files
committed
Add an explation of deprecating step definitions.
1 parent dd17877 commit 2a3f14f

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

general/development/tools/behat/writing.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,38 @@ In terms of making the Behat test work, it does not matter whether you use `@Giv
352352

353353
When defining new Step definitions in your plugin, try to make sure the step name identifies it as belonging to your plugin. So, don't make a step called `I disable UI plugins`. Call it something like `I disable UI plugins in the CodeRunner question type`.
354354

355+
### Deprecating a step definition
356+
357+
Sometimes it may be desirable to remove a step definition, when it is no longer relevant due to interface changes, or when it is replaced by another step or named selector. As it is possible for other parts of the system to use any defined step, it is necessary to mark a step as deprecated before it is completely removed.
358+
359+
To deprecate a step, create a new deprecated steps file in `tests/behat/behat_plugin_name_deprecated.php` with a class extending `behat_deprecated_base`. For example, from qbank_comments:
360+
361+
```php title="tests/behat/qbank_comment_behat_deprecated.php"
362+
<?php
363+
require_once(__DIR__ . '/../../../../../lib/behat/behat_deprecated_base.php');
364+
365+
class behat_qbank_comment_deprecated extends behat_deprecated_base {
366+
/**
367+
* Looks for the appropriate hyperlink comment count in the column.
368+
*
369+
* @Then I should see :arg1 on the comments column
370+
* @param string $linkdata
371+
* @deprecated Since Moodle 5.0 MDL-79122 in favour of the "qbank_comment > Comment count link" named selector.
372+
* @todo Final removal in Moodle 6.0 MDL-82413.
373+
*/
374+
public function i_should_see_on_the_column(string $linkdata): void {
375+
$this->deprecated_message("Use '\"{$linkdata}\" \"qbank_comment > Comment count link\" should exist'");
376+
$this->execute('behat_general::should_exist', [$linkdata, 'qbank_comment > Comment count link']);
377+
}
378+
}
379+
```
380+
381+
The deprecated step should call `$this->deprecated_message()` with a human readable example of what to do instead of using the deprecated step. It should then continue to perform its original behaviour (either using its original code, or by calling the step that replaces it) until it is completely removed.
382+
383+
If a deprecated step is called in a test, it will fail and output the deprecation message. As a temporary measure, it is possible to run tests using deprecated steps by setting `$CFG->behat_usedeprecated` in config.php.
384+
385+
A deprecated step should be documented and removed in accordance with the normal [deprecation process](../../policies/deprecation/index.md).
386+
355387
### Override behat core context for theme suite
356388

357389
To override behat step definitions so as to run behat with specified theme, you should create a contexts within `/theme/{MYTHEME}/tests/behat/` with prefix `behat_theme_{MYTHEME}_` and suffixed with the context being overridden. For example, if you want to override `behat_mod_forum` context, then you should create a class `/theme/{MYTHEME}/tests/behat/mod_forum/behat_theme_{MYTHEME}_behat_mod_forum.php`

0 commit comments

Comments
 (0)