Tutorial 3: Step Parameters¶
Goal: | Use step parameter to handover parameters to step functions. |
---|
Write the Feature Test¶
# file:features/tutorial03_step_parameters.feature
Feature: Step Parameters (tutorial03)
Scenario: Blenders
Given I put "apples" in a blender
When I switch the blender on
Then it should transform into "apple juice"
The feature description contains a number of parameters, where different values can be filled in. This also makes the test automation layer much simpler, because the number of step definitions is reduced.
Hint
BEST PRACTICE: Put parameters in double-quoted text to make variation-points visible. The test runner output does not have this problem, because it often marks these parameters as bold text.
Provide the Test Automation¶
# file:features/steps/step_tutorial03.py
# ----------------------------------------------------------------------------
# STEPS:
# ----------------------------------------------------------------------------
from behave import given, when, then
from hamcrest import assert_that, equal_to
from blender import Blender
@given('I put "{thing}" in a blender')
def step_given_put_thing_into_blender(context, thing):
context.blender = Blender()
context.blender.add(thing)
@when('I switch the blender on')
def step_when_switch_blender_on(context):
context.blender.switch_on()
@then('it should transform into "{other_thing}"')
def step_then_should_transform_into(context, other_thing):
assert_that(context.blender.result, equal_to(other_thing))
Provide the Domain Model¶
# file:features/steps/blender.py
# -----------------------------------------------------------------------------
# DOMAIN-MODEL:
# -----------------------------------------------------------------------------
class Blender(object):
TRANSFORMATION_MAP = {
"Red Tree Frog": "mush",
"apples": "apple juice",
"iPhone": "toxic waste",
"Galaxy Nexus": "toxic waste",
}
def __init__(self):
self.thing = None
self.result = None
@classmethod
def select_result_for(cls, thing):
return cls.TRANSFORMATION_MAP.get(thing, "DIRT")
def add(self, thing):
self.thing = thing
def switch_on(self):
self.result = self.select_result_for(self.thing)
Run the Feature Test¶
When you run the feature file from above:
$ behave ../features/tutorial03_step_parameters.feature Feature: Step Parameters (tutorial03) # ../features/tutorial03_step_parameters.feature:1 Scenario: Blenders # ../features/tutorial03_step_parameters.feature:3 Given I put "apples" in a blender # ../features/steps/step_tutorial03.py:39 When I switch the blender on # ../features/steps/step_tutorial03.py:44 Then it should transform into "apple juice" # ../features/steps/step_tutorial03.py:48 1 feature passed, 0 failed, 0 skipped 1 scenario passed, 0 failed, 0 skipped 3 steps passed, 0 failed, 0 skipped, 0 undefined Took 0m0.001s