Tutorial 9: Use Background¶
- Goal
Use the
Background
concept to execute a number of steps before each scenario.
Hint
This example is based on the Ninja Survival Rate examples from [SecretNinja10].
Write the Feature Test¶
# file:features/tutorial09_background.feature
Feature: Using Background -- Fight or Flight (Natural Language Part2, tutorial09)
Background: Ninja fight setup
Given the ninja encounters another opponent
Scenario: Weaker opponent
Given the ninja has a third level black-belt
When attacked by a samurai
Then the ninja should engage the opponent
Scenario: Stronger opponent
Given the ninja has a second level black-belt
When attacked by Chuck Norris
Then the ninja should run for his life
Provide the Test Automation¶
# file:features/steps/step_tutorial02.py
# ----------------------------------------------------------------------------
# BACKGROUND-STEPS: Needed for tutorial09
# ----------------------------------------------------------------------------
@given('the ninja encounters another opponent')
def step_the_ninja_encounters_another_opponent(context):
"""
BACKGROUND steps are called at begin of each scenario before other steps.
"""
# -- SETUP/TEARDOWN:
if hasattr(context, "ninja_fight"):
# -- VERIFY: Double-call does not occur.
assert_that(context.ninja_fight, is_not(equal_to(None)))
context.ninja_fight = None
# file:features/steps/step_tutorial02.py
# ----------------------------------------------------------------------------
# STEPS:
# ----------------------------------------------------------------------------
from behave import given, when, then
from hamcrest import assert_that, equal_to, is_not
@given('the ninja has a {achievement_level}')
def step_the_ninja_has_a(context, achievement_level):
context.ninja_fight = NinjaFight(achievement_level)
@when('attacked by a {opponent_role}')
def step_attacked_by_a(context, opponent_role):
context.ninja_fight.opponent = opponent_role
@when('attacked by {opponent}')
def step_attacked_by(context, opponent):
context.ninja_fight.opponent = opponent
@then('the ninja should {reaction}')
def step_the_ninja_should(context, reaction):
assert_that(reaction, equal_to(context.ninja_fight.decision()))
Provide the Domain Model¶
# file:features/steps/step_tutorial02.py
# ----------------------------------------------------------------------------
# PROBLEM DOMAIN:
# ----------------------------------------------------------------------------
class NinjaFight(object):
"""
Domain model for ninja fights.
"""
# pylint: disable=R0903
def __init__(self, with_ninja_level=None):
self.with_ninja_level = with_ninja_level
self.opponent = None
def decision(self):
"""
Business logic how a Ninja should react to increase his survival rate.
"""
assert self.with_ninja_level is not None
assert self.opponent is not None
if self.opponent == "Chuck Norris":
return "run for his life"
if "black-belt" in self.with_ninja_level:
return "engage the opponent"
else:
return "run for his life"
Run the Feature Test¶
When you run this feature test with the background functionality:
$ behave ../features/tutorial09_background.feature Feature: Using Background -- Fight or Flight (Natural Language Part2, tutorial09) # ../features/tutorial09_background.feature:1 Background: Ninja fight setup # ../features/tutorial09_background.feature:3 Scenario: Weaker opponent # ../features/tutorial09_background.feature:6 Given the ninja encounters another opponent # ../features/steps/step_tutorial02.py:78 Given the ninja has a third level black-belt # ../features/steps/step_tutorial02.py:57 When attacked by a samurai # ../features/steps/step_tutorial02.py:61 Then the ninja should engage the opponent # ../features/steps/step_tutorial02.py:69 Scenario: Stronger opponent # ../features/tutorial09_background.feature:11 Given the ninja encounters another opponent # ../features/steps/step_tutorial02.py:78 Given the ninja has a second level black-belt # ../features/steps/step_tutorial02.py:57 When attacked by Chuck Norris # ../features/steps/step_tutorial02.py:65 Then the ninja should run for his life # ../features/steps/step_tutorial02.py:69 1 feature passed, 0 failed, 0 skipped 2 scenarios passed, 0 failed, 0 skipped 8 steps passed, 0 failed, 0 skipped, 0 undefined Took 0m0.001s