Behat
Last updated
Was this helpful?
Last updated
Was this helpful?
What is Behat
Why Behat versus others?
Behat Components
How Behat Works
Where used in Acquia (Behat with BLT and Lightning)
Behat is an open source Behavior Driven Development framework for PHP.
It is a tool to test the behavior of your application, described in a special language called Gherkin.
Behat integrates well with . Blt uses Behat for Functional testing. Behat comes baked in when you install a drupal site with BLT
Lightning, a very popular Drupal distribution by Acquia, uses Behat for all its functional testing. Both BLT and Lightning are primarily used together for client projects in Professional Services.
Behat helps you to start developing project from “how it must behave” and not from “what concrete components it must include in particular”.
Clients can sign off on the tests as “acceptance criteria” for completing the project.
The tests are written in a human-readable text files, which can be easily extended by writing additional PHP methods
Non-coders can read and understand test and can help write tests.
It can be integrated with and other browser emulators to generate screenshots of failures.
Gherkin is the language that Cucumber uses to define test cases. It is designed to be non-technical and human readable, and collectively describes use cases relating to a software system. Gherkin serves two purposes: serving as your project’s documentation and automated tests.
Gherkin Example
Gherkin is a line-oriented language that uses indentation to define structure.
Line endings terminate statements (called steps) and either spaces or tabs may be used for indentation.
Finally, most lines in Gherkin start with a special keyword ( Feature, Scenario.. etc)
The parser divides the input into features, scenarios and steps
In Gherkin, each line that isn't blank has to start with a Gherkin keyword, followed by any text you like. The main keywords are:
Feature
Scenario
Given, When, Then, And, But (Steps)
Background
Scenario Outline
Examples
There are a few extra keywords as well:
""" (Doc Strings)
| (Data Tables)
@ (Tags)
# (Comments)
A feature is a Use Case that describes a specific function of the application being tested. There are three parts to a Feature:
The Feature: keyword
The Feature name (on the same line as the keyword)
An optional description on the following lines
Feature: Withdraw Money from ATM
A user with an account at a bank would like to withdraw money from an ATM.
Scenario: Scenario 1
Given preconditions
When actions
Then results
Scenario: Scenario 2
...
A step typically starts with Given, When or Then. If there are multiple Given or When steps underneath each other, you can use And or But.
Given - The purpose is to put the system in a known state before the user starts interacting with system.
When - Describes the key action the user performs.
Then - Describes the outcome resulting from actions taken
And - Logical and
But - Logically the same as And, but used in the negative form
Scenario: John wants to withdraw money from his bank account at an ATM
Given John has a valid Credit or Debit card
And his account balance is $100
When he inserts his card
And withdraws $45
Then the ATM should return $45
And his account balance is $55
As set of features will grow, there’s a good chance that the amount of different steps that you’ll have at your disposal to describe new scenarios will also grow.
Behat provides a command line option --definitions or simply -d to easily browse definitions in order to reuse them or adapt them. Existing Behat Steps can be discovered from the CLI.
$ #just list definition expressions
$ behat -dl
$ #show definitions with extended info$ behat -di
Tags are a great way to organize your features and scenarios. These can be placed before:
Feature
Scenario
Scenario Outline
Examples
A Scenario or Feature can have as many tags as you like, just separate them with spaces. @billing Feature: Verify billing @important @product Scenario: Missing product description
Behat hooks are a simple way to execute your custom code just before or just after each of Behat events/action occurs.
Behat allows you to use the following hooks:
BeforeSuite This hook is run before any feature in the suite runs. For example, you could use this to set up the project database you are testing.
AfterSuite This hook is run after all features in the suite have run. This hooks is useful to dump or print some kind of statistics or tear down your application after testing.
BeforeFeature This hook is run before a feature runs.
Sometimes you may want a certain hook to run only for certain scenarios, features or steps. This can be achieved by associating a @BeforeFeature, @AfterFeature, @BeforeScenario or @AfterScenario hook with one or more tags. You can also use OR(||) and AND (&&) tags:
Context
Context classes are a keystone of testing environment in Behat
The context class is a simple POPO (Plain Old PHP Object) that tells Behat how to test your features.Feature tells how your application behaves, then the context class is all about how to test it.
Feature Context
RawDrupalContext A context that provides no step definitions, but all of the necessary functionality for interacting with Drupal, and with the browser via Mink sessions.
DrupalContext Provides step-definitions for creating users, terms, and nodes.
Scenario Scenario is defined by a sequence of Steps outlining the preconditions and flow of events that will take place. Every scenario starts with the Scenario: keyword (or localized keyword), followed by an optional scenario title. Each Feature is made of a collection of scenarios and every scenario consists of one or more .
Mink Mink is an open source browser controller/emulator for web applications, written in PHP 5.3. PhantomJS PhantomJS is a scripted, used for automating web page interaction. PhantomJS provides a API enabling automated navigation