- Behave - Discussion
- Behave - Useful Resources
- Behave - Quick Guide
- Behave - Debugging
- Behave - Hooks
- Behave - Reports
- Behave - Retry Mechanism
- Behave - Exclude Tests
- Behave - Runner Script
- Behave - Step Parameters
- Behave - Step Functions
- Behave - Multi-Methods
- Behave - Optional Part
- Behave - Regular Expressions
- Behave - Step Matchers
- Behave - Enumeration
- Behave - Tags
- Behave - Data Types
- Behave - Background
- Behave - Steps in a Step
- Behave - Setup Table
- Behave - Multiline Text
- Behave - Scenario Outlines
- Behave - Step Parameters
- Behave - Supported Languages
- Behave - First Steps
- Behave - Step Implementations
- Behave - Feature Files
- Behave - Gherkin Keywords
- Behave - Feature Testing Setup
- Behave - Configuration Files
- Behave - Command Line
- Behave - Installation
- Behave - Introduction
- Behave - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
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}) 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.
The output shows Is payment done? and False. The output False comes from the enumeration data type.
Advertisements