Behave – Enumeration

Behave - Enumeration

In this chapter, we will discuss Behave Enumeration. 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, enums

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.

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})
register_type(Response=parse_response)
@when('User asks "{q}"')
def step_question(context, q):
   print("Question is: ")
   print(q)
@then('response is "{a:Response}"')
def step_answer(context, a):
   print("Answer is: ")
   print(a)

Output

The output is below. Here, we have used the command behave –no-capture -f plain.

Behave - Enumeration

The output False comes from the Behave enumeration data type.

Next Topic – Click Here

Leave a Reply