- Behave - Home
- Behave - Introduction
- Behave - Installation
- Behave - Command Line
- Behave - Configuration Files
- Behave - Feature Testing Setup
- Behave - Gherkin Keywords
- Behave - Feature Files
- Behave - Step Implementations
- Behave - First Steps
- Behave - Supported Languages
- Behave - Step Parameters
- Behave - Scenario Outlines
- Behave - Multiline Text
- Behave - Setup Table
- Behave - Steps in a Step
- Behave - Background
- Behave - Data Types
- Behave - Tags
- Behave - Enumeration
- Behave - Step Matchers
- Behave - Regular Expressions
- Behave - Optional Part
- Behave - Multi-Methods
- Behave - Step Functions
- Behave - Step Parameters
- Behave - Runner Script
- Behave - Exclude Tests
- Behave - Retry Mechanism
- Behave - Reports
- Behave - Hooks
- Behave - Debugging
Behave Useful Resources
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 - payment.feature
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, stepImpPayment.py
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 obtained after running the feature file is mentioned below. Here, we have used the command behave --no-capture -f plain.
(myenv) D:\behave\myenv\pythonProject>behave --no-capture -f plain
USING RUNNER: behave.runner:Runner
Feature: Payment Process
Scenario: Response
Question is:
Is payment done?
When User asks "Is payment done?" ... passed in 0.000s
Then response is "No" ... undefined in 0.000s
Errored scenarios:
features/payment.feature:2 Response
0 features passed, 0 failed, 1 error, 0 skipped
0 scenarios passed, 0 failed, 1 error, 0 skipped
1 step passed, 0 failed, 0 skipped, 1 undefined
Took 0min 0.000s
You can implement step definitions for undefined steps with these snippets:
from behave.api.pending_step import StepNotImplementedError
@then(u'response is "No"')
def step_impl(context):
raise StepNotImplementedError(u'Then response is "No"')
The output shows Is payment done? and False. The output False comes from the enumeration data type.