Testmode
Testmode keeps live and generated content out of Behat assertions. While test mode is on, it filters the listings in the views you register down to the items whose title, name, or email matches a test pattern. A scenario then sees only what it created, even on a site full of demo pages or a copy of production.
This page is for developers writing Behat tests on a project built from Vortex. It shows how to register a view with Testmode and how to turn test mode on for a scenario. The module documentation is the reference for its full API.
How it works
Testmode filtering relies on 3 conventions:
- Test content created in Behat features has titles that start with
[TEST]. - The views to filter are registered in the
testmode.settingsconfiguration. - Behat scenarios tagged with
@testmodeturn filtering on before they run and off after they end.
While filtering is on, the registered views show only content that matches
the test patterns, such as [TEST% for node titles.
Configuration
Testmode is configured through the testmode.settings configuration object:
| Key | Type | Description |
|---|---|---|
views_node | string[] | Node view machine names to filter |
views_term | string[] | Term view machine names to filter |
views_user | string[] | User view machine names to filter |
pattern_node | string[] | MySQL LIKE patterns for node titles |
pattern_term | string[] | MySQL LIKE patterns for term names |
pattern_user | string[] | MySQL LIKE patterns for user emails |
list_term | bool | If true, filters the taxonomy term overview page with the term patterns. If false, leaves that page unfiltered |
Vortex adds testmode to config_exclude_modules in
settings.testmode.php,
so the module and its settings stay out of the exported configuration.
Configuration import therefore never enables it. The module is a declared
dependency of the ys_demo module, so installing the demo module brings it
in. Without the demo module, install Testmode from your own module's
dependencies or a deploy step.
Registering a view programmatically
The Testmode singleton reads and writes that configuration. The settings
stay out of the exported configuration, so register a view from a deploy
hook in your module's MODULE.deploy.php file, as ys_demo does in
ys_demo.deploy.php:
use Drupal\testmode\Testmode;
function ys_demo_deploy_configure_testmode(): string {
$testmode = Testmode::getInstance();
$views = $testmode->getNodeViews();
if (!in_array('ys_demo_pages', $views)) {
$views[] = 'ys_demo_pages';
$testmode->setNodeViews($views);
}
return 'Configured testmode to filter the pages view.';
}
The same singleton exposes getTermViews(), getUserViews(), and the
matching pattern getters and setters. It also has enableTestMode(),
disableTestMode(), and isTestMode() for driving the mode directly.
Behat integration
The @testmode tag turns test mode on for a single scenario and off again
when it ends. It comes from TestmodeTrait in
behat-steps. FeatureContext
extends the DrupalContext class from behat-steps, so the tag works
without extra wiring:
@testmode
Scenario: Pages view shows only test content when test mode is enabled
Given the following page content exists:
| title | status | moderation_state |
| [TEST] First test page | 1 | published |
| [TEST] Second test page | 1 | published |
When I go to "/pages"
Then I should see "[TEST] First test page"
And I should see "[TEST] Second test page"
And I should not see "Demo page"
The [TEST] prefix in content titles matches the default [TEST% pattern
configured in Testmode. Only matching content appears in registered views,
so the generated Demo page nodes stay out of the assertions.
Example in Vortex
The ys_demo module puts these pieces together:
- It ships a pages view at
/pages. - It registers the view with Testmode in the
ys_demo_deploy_configure_testmodedeploy hook. - It's covered by the
pages.featureBehat feature, which demonstrates the@testmodetag against the generated content it filters out.