来自一个 Behave 步骤的数据如何传递到后面的步骤?

How is data from one Behave step passed to a later step?

考虑一个 Behave 场景:

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

所以我有一个生成(比方说)随机数的@when 函数,我需要该随机数出现在@then 条件测试中。

如何将一步的结果传递给另一步?

您可以在传递给步骤的上下文对象上设置数据。来自 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)

您还可以modify the context at various other points在测试运行中,在每个步骤、功能、场景、标签等之前和之后