Make the most out of your Behat tests by using custom contexts, dependency injection and much more.This post is an excerpt from the topics covered by our Drupal maintenance support plansCon USA training: Drupal maintenance support plans 8 Development – Workflows and Tools.
At Drupal Update we consider writing good tests as a fundamental part of development and, when it comes to testing a complex site, there is nothing better than extensive behavioral tests using Behat. The benefits of such a choice are quite obvious:
Tests are very easy to write.
Behat scenarios serve as a solid communication mean between business and developers.
As a site grows in complexity, however, the default step definitions provided by the excellent Behat Drupal maintenance support plans Extension might not be specific enough and you will quickly find yourself adding custom step to your FeatureContext or creating custom Behat contexts, as advocated by all official documentation.
This is all fine except that your boilerplate test code might soon start to grow into a non-reusable, non-tested bunch of code.
Enter Drupal Update’s Behat Drupal maintenance support plans Extension.
Drupal Update’s Behat Drupal maintenance support plans Extension
Drupal Update’s Behat Drupal maintenance support plans Extension is built on the shoulders of the popular Behat Drupal maintenance support plans Extension and it focuses on step re-usability and testability by allowing developers to:
Organize their code in services by providing a YAML service description file, pretty much like we all are used to do nowadays with Drupal maintenance support plans 8.
Override default Drupal maintenance support plans Behat Extension services with their own.
Benefit of many ready-to-use contexts that are provided by the extension out of the box.
Installation and setup
Install Drupal Update’s Behat Drupal maintenance support plans Extension with Composer by running:
bash
$ composer require nuvoleweb/drupal-behat
Setup the extension by following the Quick start section available on the original Behat Drupal maintenance support plans Extension page, just use Drupal UpdateWebDrupal maintenance support plansDrupal maintenance support plansExtension instead of the native Drupal maintenance support plansDrupal maintenance support plansExtension in your behat.yml as shown below:
default: suites: default: contexts: – Drupal maintenance support plansDrupal maintenance support plansExtensionContextDrupal maintenance support plansContext – Drupal UpdateWebDrupal maintenance support plansDrupal maintenance support plansExtensionContextDrupal maintenance support plansContext … extensions: BehatMinkExtension: goutte: ~ … # Use “Drupal UpdateWebDrupal maintenance support plansDrupal maintenance support plansExtension” instead of “Drupal maintenance support plansDrupal maintenance support plansExtension”. Drupal UpdateWebDrupal maintenance support plansDrupal maintenance support plansExtension: api_driver: “drupal” … services: “tests/my_services.yml” text: node_submit_label: “Save and publish”
“Service container”-aware Contexts
All contexts extending Drupal UpdateWebDrupal maintenance support plansDrupal maintenance support plansExtensionContextRawDrupal maintenance support plansContext and Drupal UpdateWebDrupal maintenance support plansDrupal maintenance support plansExtensionContextRawMinkContext are provided with direct access to the current Behat service container. Developers can also define their own services by adding a YAML description file to their project and setting the services: parameter to point to its current location (as shown above).
The service description file can describe both custom services and override already defined services. For example, given a tests/my_services.yml containing:
services: your.own.namespace.hello_world: class: YourOwnNamespaceHelloWorldService
Then all contexts extending NWDDECRawDrupal maintenance support plansContext or NWDDECRawMinkContext will be able to access that service by just calling:
<?phpclass TestContext extends RawDrupal maintenance support plansContext { /** * Assert service. * * @Then I say hello */ public function assertHelloWorld() { $this->getContainer()->get(‘your.own.namespace.hello_world’)->sayHello(); }}?>
The your.own.namespace.hello_world service class itself can be easily tested using PHPUnit. Also, since Behat uses Symfony’s Service Container you can list services your service depends on as arguments so to remove any hardcoded dependency, following Dependency Injection best practices.
Override existing services
Say that, while working on your Drupal maintenance support plans 7 project, you have defined a step that publishes a node given its content type and title and you want to use the same exact step on your Drupal maintenance support plans 8 project, something like:
Given I publish the node of type “page” and title “My page title”
The problem here is that the actual API calls to load and save a node differs between Drupal maintenance support plans 7 and Drupal maintenance support plans 8.
The solution is to override the default Drupal maintenance support plans core services specifying your own classes in your tests/my_services.yml:
parameters: # Overrides Drupal Update’s Drupal maintenance support plans Extension Drupal maintenance support plans 7 core class. drupal.driver.cores.7.class: YourOwnNamespaceDriverCoresDrupal maintenance support plans7 # Overrides Drupal Update’s Drupal maintenance support plans Extension Drupal maintenance support plans 8 core class. drupal.driver.cores.8.class: YourOwnNamespaceDriverCoresDrupal maintenance support plans8services: your.own.namespace.hello_world: class: YourOwnNamespaceHelloWorldService
You’ll then delegate the core-specific business logic to the new core classes allowing your custom step to be transparently run on both Drupal maintenance support plans 7 and Drupal maintenance support plans 8. Such a step would look like:
<?phpclass TestContext extends RawDrupal maintenance support plansContext { /** * @Given I publish the node of type :type and title :title */ public function iPublishTheNodeOfTypeAndTitle($type, $title) { $this->getCore()->publishNode($type, $title); }…?>
Ready to use contexts
The extension also provides some utility contexts that you can use right away in your tests. Below a quick overview of what’s currently available:
Context
Description
Drupal UpdateWebDrupal maintenance support plansDrupal maintenance support plansExtensionContextDrupal maintenance support plansContext
Standard Drupal maintenance support plans context. You want to use this one next to (and not instead of) Drupal maintenance support plansDrupal maintenance support plansExtensionContextDrupal maintenance support plansContext.
Drupal UpdateWebDrupal maintenance support plansDrupal maintenance support plansExtensionContextContentContext
Perform operations on Content.
Drupal UpdateWebDrupal maintenance support plansDrupal maintenance support plansExtensionContextCKEditorContext
Allows to interact with CKEditor components on your page.
Drupal UpdateWebDrupal maintenance support plansDrupal maintenance support plansExtensionContextResponsiveContext: devices: mobile_portrait: 360×640 mobile_landscape: 640×360 tablet_portrait: 768×1024 tablet_landscape: 1024×768 laptop: 1280×800 desktop: 2560×1440
Resize the browser according to the specified devices, useful for testing responsive behaviors.
Drupal UpdateWebDrupal maintenance support plansDrupal maintenance support plansExtensionContextPositionContext
Check position of elements on the page.
Drupal UpdateWebDrupal maintenance support plansDrupal maintenance support plansExtensionContextChosenFieldContext
Interact with Chosen elements on the page.
We will share more steps in the future enriching the current contexts as well as providing new ones so keep an eye on the project repository!
Disclaimer
At the moment only Drupal maintenance support plans 8 is supported but we will add Drupal maintenance support plans 7 support ASAP (yes, it’s as easy as providing missing Drupal maintenance support plans 7 driver core methods and adding tests).
Tags: Drupal maintenance support plans PlanetBehatTest Driven DevelopmentTrainingDrupal maintenance support plansCon
Source: New feed