Enumeration (String-to-Value Mapping)

An enumeration maps a number of unique string-based words/strings to values.

Feature Example

Assuming you want to write something like this:

# file:datatype.features/enum.feature
Feature: User-Defined Enum Type (Name-to-Value Mapping)

    Scenario:
        When Romeo asks Julia: "Do you love me?"
        Then the answer is "yes"

    Scenario:
        When Romeo asks Julia: "Do you hate me?"
        Then the answer is "no"

    Scenario:
        When Romeo asks Julia: "Do you kiss me?"
        Then the answer is "jubilee"

Define the Data Type

# file:datatype.features/steps/step_enum.py
# ------------------------------------------------------------------------
# USER-DEFINED TYPES:
# ------------------------------------------------------------------------
from behave import register_type
from parse_type import TypeBuilder

# -- ENUM: Returns True (for "yes" and "jubilee"), False (for "no")
parse_yesno = TypeBuilder.make_enum({"yes": True, "no": False, "jubilee": True })
register_type(YesNo=parse_yesno)

Note

The TypeBuilder.make_enum() function performs the magic. It computes a regular expression pattern for the given enumeration of words/strings and stores them in parse_yesno.pattern attribute.

Provide the Step Definitions

# file:datatype.features/steps/step_enum.py
# ----------------------------------------------------------------------------
# STEPS:
# ----------------------------------------------------------------------------
from behave import when, then
from hamcrest import assert_that, equal_to

@when(u'Romeo asks Julia: "{question}"')
def step_when_romeo_asks_julia(context, question):
    context.question = question

@then(u'the answer is "{answer:YesNo}"')
def step_then_the_answer_is(context, answer):
    assert_that(answer, equal_to(answer_oracle.get(context.question, None)))

Run the Test

Now we run this example with behave (and all steps are matched):

$ behave ../datatype.features/enum.feature
Feature: User-Defined Enum Type (Name-to-Value Mapping)   # ../datatype.features/enum.feature:1

  Scenario:                                  # ../datatype.features/enum.feature:3
    When Romeo asks Julia: "Do you love me?" # ../datatype.features/steps/step_enum.py:45
    Then the answer is "yes"                 # ../datatype.features/steps/step_enum.py:49

  Scenario:                                  # ../datatype.features/enum.feature:7
    When Romeo asks Julia: "Do you hate me?" # ../datatype.features/steps/step_enum.py:45
    Then the answer is "no"                  # ../datatype.features/steps/step_enum.py:49

  Scenario:                                  # ../datatype.features/enum.feature:11
    When Romeo asks Julia: "Do you kiss me?" # ../datatype.features/steps/step_enum.py:45
    Then the answer is "jubilee"             # ../datatype.features/steps/step_enum.py:49

1 feature passed, 0 failed, 0 skipped
3 scenarios passed, 0 failed, 0 skipped
6 steps passed, 0 failed, 0 skipped, 0 undefined
Took 0m0.001s

The Complete Picture

# file:datatype.features/steps/step_enum.py
# -*- coding: UTF-8 -*-
"""
Feature: User-Defined Enum Type (Name-to-Value Mapping)

    Scenario:
        When Romeo asks Julia: "Do you love me?"
        Then the answer is "yes"

    Scenario:
        When Romeo asks Julia: "Do you hate me?"
        Then the answer is "no"

    Scenario:
        When Romeo asks Julia: "Do you kiss me?"
        Then the answer is "silence"
"""

# ------------------------------------------------------------------------
# DOMAIN MODEL:
# ------------------------------------------------------------------------
answer_oracle = {
    "Do you love me?": True,
    "Do you hate me?": False,
    "Do you kiss me?": True,
}

# @mark.user_defined_types
# ------------------------------------------------------------------------
# USER-DEFINED TYPES:
# ------------------------------------------------------------------------
from behave import register_type
from parse_type import TypeBuilder

# -- ENUM: Returns True (for "yes" and "jubilee"), False (for "no")
parse_yesno = TypeBuilder.make_enum({"yes": True, "no": False, "jubilee": True })
register_type(YesNo=parse_yesno)

# @mark.steps
# ----------------------------------------------------------------------------
# STEPS:
# ----------------------------------------------------------------------------
from behave import when, then
from hamcrest import assert_that, equal_to

@when(u'Romeo asks Julia: "{question}"')
def step_when_romeo_asks_julia(context, question):
    context.question = question

@then(u'the answer is "{answer:YesNo}"')
def step_then_the_answer_is(context, answer):
    assert_that(answer, equal_to(answer_oracle.get(context.question, None)))