What is “Specification by Example”?Knowledge in 3 minutes


What is “Specification by Example”?

Specification by Example (SBE) is a method of describing software requirements in the form of test cases, which can prevent gaps in understanding between developers and testers by describing requirements as concrete examples. SBE can also be used as a basis for automating test cases.

A test case described in SBE typically consists of the following three elements

  • Input data
  • Expected output data
  • Test execution conditions

Input data is the data that is passed to the software. Expected output data is the data that the software outputs in response to the input data. Test execution conditions are the conditions that must be met when executing a test case.

Sample written in Cucumber

Cucumber is a framework for automating test cases; with Cucumber, test cases can be written in natural language. This facilitates test case creation and maintenance.

Sample code

Feature: login

  Scenario: Login successfully
    Given I have users:
      | id        | password |
      | fuge   | hoge         |
    When I type 'fuge' in 'id'
    And I type 'hoge' in 'password'
    And I I click on 'enter'
    Then I should see 'Login successfully'

This code is a test case for the login screen written in Cucumber: Feature represents a group of test cases;

Scenario represents a test case;

Given represents the starting condition of the test case;

When represents the execution of the test case; and Then represents the expected result of the test case.

When is SBE used?

SBE is often used in agile software development. In agile software development, software development is repeated in short cycles, so test cases written in SBE can be created in parallel with software development. This improves the quality of the software.