Dutch python meeting: behaviour driven development - Jan-Hein Bührman

Tags: pun, python

(One of my summaries of a talk at a Dutch python meeting, this one in october 2021 at the Amersfoort zoo)

Behaviour driven development (BDD) is something that gets mentioned at python meetups and conferences from time to time. See this example from 2019.

What is behaviour driven development? The core point is that you specify feature behaviour. Concrete, realistic examples to create a shared understanding of how the system should behave. It is defined as a scenario:

  • Input

  • Actions

  • Outcomes

What is commonly used is the “Gherkin” language: “given …. when …. then ….”. In Java, “cucumber” is a well-known library for BDD. In python you have behave.

BDD has a big benefit: you have concrete examples in an unambiguous specification. And it is in a language that can be understood by everyone from customer to programmer. An example:

Scenario: simple google search
    Given: the google homepage is displayed
    When: the user enters "panda" in the search bar
    Then: links related to "panda" are shown on the results page.

“Given” describes the initial stable state. “When” describes the action that’s relevant for the scenario. “Then” describes the outcome of the action. You can add “and” statements to expand on the when or then statements.

He then showed a number of examples, as you have to make the tests executable. This means you have to tell “behave” how to translate a textual line in your scenario into a proper python test. So a “when the user selects line 2” turns into a python function:

@when("the user selects line {index:d}")
def test_impl(context):
    assert context.lines[index] == xyz

“Behave” can then use these functions to make your scenarios executable. An alternative to “behave” is pytest-bdd.

A book he recommends: Architecture patterns with python.

vanrees.org logo

About me

My name is Reinout van Rees and I work a lot with Python (programming language) and Django (website framework). I live in The Netherlands and I'm happily married to Annie van Rees-Kooiman.

Weblog feeds

Most of my website content is in my weblog. You can keep up to date by subscribing to the automatic feeds (for instance with Google reader):