London, United Kingdom

(+44) 07788.377.460 [email protected]

[draft] BDD: Behat & Gherkin

Behaviour-driven development (BDD) is an Agile software development methodology that emphasises collaboration among developers, QA experts, and customer representatives / stake holders. It is based on the concept of test-driven development (TDD) and is focused on end-user requirements and their interactions with the product / service.

BDD aims to solve communication issues by forcing developers and business leaders to speak in a shared language.

Behaviour-Driven Development (BDD) involves naming software tests using domain language to describe the behaviour of the code; domain-specific language (DSL) refers to natural-language constructs (e.g., English-like sentences) that can express the behaviour and the expected outcomes.

Behat

Behat is a tool to close the Behaviour Driven Development (BDD) communication loop, and test the behaviour of your application (described in a special language called Gherkin).
https://behat.org/en/latest/quick_start.html

$> composer require --dev behat/behat
$> vendor/bin/behat -V
$> vendor/bin/behat --init

The newly created features/bootstrap/FeatureContext.php will have an initial context class to get you started.

Gherkin language

Gherkin language is used to document requirements and define test scenarios. It is designed to be non-technical and human readable, and collectively describes use case story Feature Scenarios relating to software systems.

Gherkin is a Business Readable, Domain Specific Language created specifically for behaviour descriptions. Gherkin’s natural language syntax is designed to provide simple documentation of the code under test and also provides a basis for automated testing.

Like YAML and Python, Gherkin is a whitespace-oriented language that uses indentation to define structure. Line endings terminate statements / steps, and either spaces or tabs may be used for indentation (spaces are suggested for portability).

Every *.feature file conventionally consists of a single feature. Lines starting with the keyword Feature: followed by three indented lines starts a feature. A feature usually contains a list of scenarios.

Every scenario consists of a list of Steps, which must start with one of the keywords Given, When, Then, But or And.

@tagname @othertag
Feature: Multiple site support

  Background:
    Given a global administrator named "Greg"
    And a blog named "Greg's anti-tax rants"
    And a customer named "Wilson"
    And a blog named "Expensive Therapy" owned by "Wilson"

  Scenario: Wilson posts to his own blog
    Given I am logged in as Wilson
    When I try to post to "Expensive Therapy"
    Then I should see "Your article was published."

  Scenario: Greg posts to a client's blog
    Given I am logged in as Greg
    When I try to post to "Expensive Therapy"
    Then I should see "Your article was published."

  Scenario: A different situation
[...]

Gherkin Hooks

Hooks allow execution of custom code before / after each of these actions:

  • BeforeSuite runs before any feature in the suite runs (e.g. use this to set up the project tested).
  • AfterSuite runs after all features in the suite have run (useful to dump or print some kind of statistics, or tear down your application after testing).
  • BeforeFeature runs before a feature runs.
  • AfterFeature runs after Behat finishes executing a feature.
  • BeforeScenario runs before a specific scenario will run.
  • AfterScenario runs after Behat finishes executing a scenario.
  • BeforeStep runs before a step runs.
  • AfterStep runs after Behat finishes executing a step.

https://behat.org/en/latest/user_guide/context/hooks.html