Skip to content

BDD and acceptance tests: examples instead of specifications

B2Bnetwork team · · 7 min

Testing and quality

In brief

BDD, or behavior-driven development, is a way of working in which business, developers and testers agree on the system's behavior using concrete examples before any code exists. The examples are written in the Given, When, Then format and then automated as acceptance tests. The main benefit is not the tool but the conversation: ambiguities in requirements surface while writing examples, not after deployment. Without that conversation, BDD turns into a harder-to-maintain notation for ordinary tests.

What BDD is and where it came from

The ISTQB Foundation Level 4.0 syllabus lists BDD alongside TDD and ATDD as approaches in which tests are defined as a way of guiding software development. In BDD the expected behavior is described with examples in natural language that stakeholders can understand, usually in the Given, When, Then format.

The key concepts:

  • example: a concrete case with data, not a general rule. Instead of “the system rejects transfers above the limit” we say “a customer with a 5,000 PLN limit who has sent 4,500 PLN today cannot send an 800 PLN transfer”;
  • scenario: an example written in a fixed format that can be read and automated;
  • living documentation: a set of scenarios that is both a description of the system's behavior and a test. When the system changes and the scenario does not, the test fails.

A closely related approach is called specification by example, after the title of Gojko Adzic's book “Specification by Example”. ATDD, or acceptance test-driven development, stresses the same thing: acceptance criteria written as examples come before the code.

An example instead of a specification

A requirement in a typical specification reads: “The system should verify the daily limit for instant transfers”. A developer, a tester and an analyst can understand it in three ways. Does the limit include pending transfers? Does it count the calendar day or the last 24 hours? What does the customer see after a rejection?

The same rules written as a scenario in Gherkin, the language of the Cucumber tool:

Feature: Daily limit for instant transfers

  Scenario: Transfer exceeding the remaining limit
    Given the customer has a daily instant transfer limit of 5000 PLN
    And the customer has sent instant transfers totaling 4500 PLN today
    When the customer sends an instant transfer of 800 PLN
    Then the transfer is rejected
    And the customer sees a message that 500 PLN of the limit remains

  Scenario Outline: Limit boundary
    Given the customer has a daily instant transfer limit of 5000 PLN
    And the customer has sent instant transfers totaling <sent> PLN today
    When the customer sends an instant transfer of <amount> PLN
    Then the transfer is <result>

    Examples:
      | sent | amount | result   |
      | 4500 | 500    | accepted |
      | 4500 | 501    | rejected |
      | 0    | 5000   | accepted |

Writing this scenario forces answers to questions the specification did not ask. The examples table in the scenario outline is simply boundary value analysis written so the business can check it. The lack of a row for pending transfers immediately shows that this rule still needs to be settled.

Gherkin supports keywords in many languages, including Polish. English is the default, and another language is set by a header in the first line of the file, for example `# language: pl`. In Polish, Given becomes “Zakładając” or “Mając”, When becomes “Kiedy” or “Jeżeli”, Then becomes “Wtedy”, And becomes “Oraz” and But becomes “Ale”. A team should choose one language and stick to it in all files.

How to run the conversation about examples

BDD delivers the most value before the code is written, in a short meeting for each story. In many teams it is called the “three amigos” meeting: business or the product owner, a developer and a tester. Each role brings different questions:

  • business: why the feature exists and which rules apply;
  • developer: what is technically possible, where there are dependencies on other systems;
  • tester: what happens at the boundary, with missing data, with concurrent operations, when an external system fails.

The outcome of the meeting is rules, examples for each rule and a list of open questions. A story with more questions than examples is not ready for work.

A convenient way to record such a meeting is three columns on a board or in a document: rules, examples under each rule and questions. The questions get an owner who will bring an answer to the next meeting, for example from the legal department or the card system team. The meeting should take a dozen or so minutes, a few dozen at most. If it runs longer, the story is too big and worth splitting. The ISTQB 4.0 syllabus describes this work in the section on collaboration-based approaches: writing user stories together, recording acceptance criteria, including as scenarios, and ATDD.

How to write scenarios that can be maintained

Most BDD problems come from scenarios written like test scripts. A few rules:

  1. Describe behavior, not the interface. “When they send a transfer for 800 PLN” instead of “When they click the amount field, type 800 and click the Send button”. Changing a screen layout should not require changing the scenario.
  2. One scenario, one rule. A scenario that checks the limit, the message, the transaction history and the push notification will fail for four different reasons.
  3. The Given step sets up state without the interface. Data is prepared through the API or the database, not by clicking.
  4. A domain vocabulary. The same words in all scenarios: “instant transfer”, not “quick transfer” one time and “express transfer” another. The vocabulary is part of the documentation.
  5. Not everything has to be a scenario. Rules with dozens of combinations are better checked with unit tests, leaving a few examples in a scenario that illustrate the rule for the business.

Automating scenarios is subject to the same rules as other tests through the interface: it is the top layer of the test pyramid, so many steps are worth executing through the API. An example of acceptance tests in a system deployment is the case study CRM deployment acceptance testing at a service company. The concepts in this area are organized by the ISTQB Acceptance Testing (CT-AcT) syllabus, whose exam you can take at our accredited exam center.

BDD vs. user acceptance testing

BDD scenarios and user acceptance tests (UAT) complement each other, but they are not the same thing. BDD scenarios are created before the code and verify that the system does what the team agreed with the business. UAT takes place on the finished system and validates whether the agreed behavior really fits users' work. The difference between these two kinds of checks is described in the article Software verification vs. validation.

Well-run BDD shortens UAT, because users do not discover basic rules only at acceptance. It does not replace it, though, especially when replacing a system people use every day, such as a CRM at a service company or an application handling system at a government office.

Questions and answers

Do you need Cucumber for BDD?

No. BDD is primarily a way of working: a conversation about examples before the code. Cucumber and other tools that read Gherkin let you turn examples into automated tests, but the same examples can be written in a table in a story or in ordinary tests with readable names. A tool makes sense when business really reads the scenarios. If only developers read them, the extra Gherkin layer often just makes maintenance harder.

Who writes BDD scenarios?

Business, a developer and a tester together, and a tester or analyst usually writes them down. What matters is that the scenario comes out of a conversation and is not copied from a specification by one person. The developer is responsible for automating the steps, the tester for the completeness of examples and edge cases, the product owner for the examples matching the business rules.

How does BDD differ from TDD?

TDD works at the code level: a developer writes a unit test before implementing a function. BDD works at the level of behavior visible to the business and involves people outside the technical team. In practice the two approaches combine: a BDD scenario describes what the system should do, and TDD tests are created while implementing individual rules. The ISTQB syllabus covers TDD, ATDD and BDD as related “test first” approaches.

When does BDD not work?

When business does not take part in the conversation about examples and testers write the scenarios alone after development is finished. What remains is the cost of maintaining the Gherkin layer without the benefit of shared understanding. It also works poorly in systems with mainly technical logic, for example libraries and infrastructure components, where ordinary unit and integration tests are clearer.

What it looks like in a project

Planning testing for your project? Talk to a test manager.

Let's talk

Read more: Testing and quality