1. Tests

TestingPyramid
Figure 1. The testing pyramid (source here)
TestingIceberg
Figure 2. The testing pyramid at work (source here)

2. Features

artifacts r303
Figure 3. Artifacts traceability (Inspired from here)
artifacts r303 bis
Figure 4. One more artifact

3. Principles

  • Given

  • When

  • Then

  • And

4. Examples

4.1. Some review activity (No code)

PhD Feature
Figure 5. Review PhD feature

4.2. This course materials! (No code)

PhD Feature
Figure 6. Check URLs feature

4.3. Android example

Android app
Figure 7. An Android app (source here)
A feature for this app (source here)
Feature: Book Search
    Scenario: Search books by author
      Given there's a book called "Tips for job interviews" written by "John Smith"
        And there's a book called "Bananas and their many colors" written by "James Doe"
        And there's a book called "Mama look I'm a rock star" written by "John Smith"
      When an employee searches by author "John Smith"
      Then 2 books should be found
        And Book 1 has the title "Tips for job interviews"
        And Book 2 has the title "Mama look I'm a rock star"

4.4. Angular example

A "Getting Started" app

Angular App Example
Figure 8. An Angular example app

Some feature description

Cucumber feature
Figure 9. Some feature example

Running Cucumber (fail)

cucumber fail
Figure 10. First run of the tests

Write Steps definitions

This is code linking the assertions with the running code.

Steps definition
import { When, Then } from 'cucumber';

When('we request the products list', function () {
    // Write code here that turns the phrase above into concrete actions
    return 'pending';
});

Then('we should receive', function (dataTable) {
    // Write code here that turns the phrase above into concrete actions
    return 'pending';
});

Running Cucumber (pass)

Passing tests
Figure 11. New run of the tests

5. Supported languages

6. Build

Make sure to integrate BDD in your build.

7. Find a plugin for your IDE

Feature in IDE
Figure 12. Cucumber plugin in use in VS Code

8. Gherkin tips

8.1. Avoid duplication

gh bad
gh good

Sometimes you want to relax your language, to make it flow better.

gh alt
Generic Step definition
@Given("I go to the {string} page")
public void i_want_to_open_page(String webpage) {
  webpageFactory.openPage(webpage);
}

8.2. Grouping step definitions

One file for each major domain object.

Example of step definition files
EmployeeStepDefinitions.java
EducationStepDefinitions.java
ExperienceStepDefinitions.java
AuthenticationStepDefinitions.java

8.3. Scenario outline (template)

gh outline

8.4. Language support

Over 70 languages!

Don’t forget the # language: fr!
French feature
Figure 13. Example of feature in French (source)

9. How-to

bdd practices diagram
Figure 14. Discovery, Formulation and Automation (Source here)

10. Concepts

10.1. Feature

What follows is a basic description or name of the feature being tested or documented.

10.2. Rule

  • represent a business rule that should be implemented

  • used to group together several scenarios

New concept (since Gherkin v6)

10.3. Background

Steps that will be ran before every scenario in the feature.

10.4. Scenario

Name or basic description of a particular scenario testing the feature.

10.5. Scenario Outline

The scenario will run N times for every argument listed in examples explicitly passed by column name wrapped in angled brackets.

A Scenario Outline must contain one or more Examples

10.6. Examples

The list of static arguments that will be passed into a scenario outline.

10.7. The AAA pattern

// arrange
var component = ComponentFixture<UserUploadComponent>;
var user = new User(component);
// act
user.Save();
// assert
mock.Received.SomeMethod();

10.8. Given

A given step, or precondition that is assumed before continuing.

In the Arrange, Act, Assert paradigm, given represents "Arrange".

10.9. When

A when step, or the behavior that is to be asserted against.

In the Arrange, Act, Assert paradigm, given represents "Act".

10.10. Then

A then step, or in other words, the step in which a behavior’s result is validated.

In the Arrange, Act, Assert paradigm, given represents "Assert".

10.11. And

Used in conjunction with any of the keywords above.

If you have two given statements, instead of explicitly calling Given twice, you can say, " Given A And B".
artifacts r303 bis
artifacts r303 ter
artifacts r303 ter
release

13. CI/CD and BDD

ci bdd

14. BDD vs. TDD

mauerhan1
Figure 15. (source here)
mauerhan2
Figure 16. (source here)
mauerhan3
Figure 17. (source here)

15. Not just for code!

step1
step2
step3 1
step3 2
step4
step5
step6
step7