kztd
kztd

Reputation: 3415

For behave, can I log the step text from inside the step definition

For a step in Behave, is there some variable I can use to log the current step text? e.g.

When I go running

@when(u'I go running')
def step_impl(context):
  log(f'Behave().currentStepText}')
  ...

Upvotes: 0

Views: 905

Answers (1)

user20132914
user20132914

Reputation: 26

Not directly, but you can use before_step in the environment.py file to save the step.name into the context and then use that in the step implementation.

Run the example below using 'behave --no-capture' so that the prints in the steps implementation show up on stdout.

environment.py:

def before_step(context, step):
    context.step_name = step.name

feature/feature.py:

Feature: Print step name
Scenario: Print step name in each step
Given given step and KEYWORD
When when step
Then then step

steps/step.py:

@given('given step and {keyword}')
def step_impl(context, keyword):
    # The trailing newline is necessary to have this line show up since
    # behave color formatting will hide it otherwise. Could also run it
    # as 'behave --no-capture --no-color'.
    print(f"GIVEN step name: '{context.step_name}'\n")

@when('when step')
def step_impl(context):
    print(f"WHEN step name: '{context.step_name}'\n")

@then('then step')
def step_impl(context):
    print(f"THEN step name: '{context.step_name}'\n")

Upvotes: 1

Related Questions