12

Consider a Behave scenario:

When some magic number is generated
Then the number should be greater than 5

So I have a @when function that produces (say) a random number and I need that number to be present in the @then conditional test.

How do I pass the result of one step to another?

Tony Ennis
  • 12,000
  • 7
  • 52
  • 73

1 Answers1

20

You can set data on the context object passed in to the steps. From the documentation:

@given('I request a new widget for an account via SOAP')
def step_impl(context):
    client = Client("http://127.0.0.1:8000/soap/")
    context.response = client.Allocate(customer_first='Firstname',
        customer_last='Lastname', colour='red')

@then('I should receive an OK SOAP response')
def step_impl(context):
    eq_(context.response['ok'], 1)

You can also modify the context at various other points in the test run, before and after every step, feature, scenario, tag, etc.

jllopezpino
  • 868
  • 2
  • 9
  • 17
Peter Wood
  • 23,859
  • 5
  • 60
  • 99
  • Right, but where is the context object defined? In documents it is said that it is an instance of behave.runner.Context but its an undefined object hanging around. – Kasra Aug 16 '17 at 11:24
  • 2
    @Kasra Why is it important where it's defined? Is there a problem? – Peter Wood Aug 18 '17 at 21:41
  • 2
    At the moment of writing my comment, I was trying to make use of context. And I didn't know that 'behave' is handling it as sort of a universal object. And you're right, no point to talk about where the context is passed from in this topic. Thanks! – Kasra Aug 22 '17 at 11:13