Tutorial 5: Multi-line Text (Step Data)

Goal:Use multi-line text (with tripple-quoted text) for large text sections.

Triple-quoted strings (ala Python docstrings) provide a possible to use large text section as step parameter. Normally, so much text would not fit on one line.

Write the Feature Test

# file:features/tutorial05_step_data.feature
Feature: Step Data (tutorial05)

   Scenario: Some scenario
     Given a sample text loaded into the frobulator:
        """
        Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
        eiusmod tempor incididunt ut labore et dolore magna aliqua.
        """
    When we activate the frobulator
    Then we will find it similar to English

Provide the Test Automation

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

@given('a sample text loaded into the frobulator')
def step_impl(context):
    frobulator = getattr(context, "frobulator", None)
    if not frobulator:
        context.frobulator = Frobulator()
    context.frobulator.text = context.text  #< STEP-DATA from context.text

@when('we activate the frobulator')
def step_impl(context):
    context.frobulator.activate()

@then('we will find it similar to {language}')
def step_impl(context, language):
    assert_that(language, equal_to(context.frobulator.seems_like_language()))

Provide the Domain Model

# file:features/steps/step_tutorial05.py
# ----------------------------------------------------------------------------
# PROBLEM DOMAIN:
# ----------------------------------------------------------------------------
class Frobulator(object):
    def __init__(self, text=None):
        self.text = None
        self.activated = False

    def activate(self):
        self.activated = True

    def seems_like_language(self):
        """
        Business logic how frobulator should react/oracle on text data.
        """
        assert self.text is not None
        assert self.activated
        if self.text.startswith("Lorem ipsum"):
            return "English"
        else:
            return "UNKNOWN"

Run the Feature Test

When you run the feature file from above:

$ behave ../features/tutorial05_step_data.feature
Feature: Step Data (tutorial05)   # ../features/tutorial05_step_data.feature:1

  Scenario: Some scenario                          # ../features/tutorial05_step_data.feature:3
    Given a sample text loaded into the frobulator # ../features/steps/step_tutorial05.py:47
      """
      Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
      eiusmod tempor incididunt ut labore et dolore magna aliqua.
      """
    When we activate the frobulator                # ../features/steps/step_tutorial05.py:54
    Then we will find it similar to English        # ../features/steps/step_tutorial05.py:58

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