Tutorial 4: Scenario Outline¶
Goal: | Use scenario outline as a parametrized template (avoid too many similar scenarios). |
---|
A Scenario Outline
provides a parametrized scenario script
(or template)
for the feature file writer. The Scenario Outline
is executed for each
example row in the Examples
section below the Scenario Outline
.
Write the Feature Test¶
# file:features/tutorial04_scenario_outline.feature
Feature: Scenario Outline (tutorial04)
Scenario Outline: Use Blender with <thing>
Given I put "<thing>" in a blender
When I switch the blender on
Then it should transform into "<other thing>"
Examples: Amphibians
| thing | other thing |
| Red Tree Frog | mush |
| apples | apple juice |
Examples: Consumer Electronics
| thing | other thing |
| iPhone | toxic waste |
| Galaxy Nexus | toxic waste |
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))
Note
Test automation layer reused from Tutorial 3: Step Parameters.
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)
Note
Domain model reused from Tutorial 3: Step Parameters.
Run the Feature Test¶
When you run the feature file from above (with plain formatting):
$ behave ../features/tutorial04_scenario_outline.feature Feature: Scenario Outline (tutorial04) # ../features/tutorial04_scenario_outline.feature:1 Scenario Outline: Use Blender with Red Tree Frog -- @1.1 Amphibians # ../features/tutorial04_scenario_outline.feature:10 Given I put "Red Tree Frog" 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 "mush" # ../features/steps/step_tutorial03.py:48 Scenario Outline: Use Blender with apples -- @1.2 Amphibians # ../features/tutorial04_scenario_outline.feature:11 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 Scenario Outline: Use Blender with iPhone -- @2.1 Consumer Electronics # ../features/tutorial04_scenario_outline.feature:15 Given I put "iPhone" 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 "toxic waste" # ../features/steps/step_tutorial03.py:48 Scenario Outline: Use Blender with Galaxy Nexus -- @2.2 Consumer Electronics # ../features/tutorial04_scenario_outline.feature:16 Given I put "Galaxy Nexus" 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 "toxic waste" # ../features/steps/step_tutorial03.py:48 1 feature passed, 0 failed, 0 skipped 4 scenarios passed, 0 failed, 0 skipped 12 steps passed, 0 failed, 0 skipped, 0 undefined Took 0m0.003s