Behave - Enumeration


Enumeration is used to map the multiple distinctive string based words to the values.

We may require a user-defined data type having the following characteristics −

  • A handful of words must be matched.

  • Pre-defined values prior to the test execution.

For the above scenarios, enumeration based on string can be used.

Feature File

Consider a feature file for the Feature titled payment process, as mentioned below −

Feature − Payment Process
Scenario − Response
      When User asks "Is payment done?"
      Then response is "No"

In the step implementation file, TypeBuilder.make_enum function evaluates a regular expression pattern for the provided enumeration of words or strings. The method register_type is used to register a user defined type that can be parsed for any type conversion at the time of matching the step.

Also, we shall pass the parameter: user-defined enum datatype enclosed in "{}".

Corresponding Step Implementation File

The step implementation file for the above Feature is as follows −

from behave import *
from behave import register_type
from parse_type import TypeBuilder
# -- ENUM: Yields True (for "yes"), False (for "no")
parse_response = TypeBuilder.make_enum({"yes": True, "no": False})
@when('User asks "{q}"')
def step_question(context, q):
   print("Question is: ")
@then('response is "{a:Response}"')
def step_answer(context, a):
   print("Answer is: ")


The output obtained after running the feature file is mentioned below. Here, we have used the command behave --no-capture -f plain.

Enumeration Data Type

The output shows Is payment done? and False. The output False comes from the enumeration data type.