如何将一个Behave步骤中的数据传递给后续步骤?

Ton*_*nis 6 python python-behave

考虑一个Behave场景:

When some magic number is generated
Then the number should be greater than 5
Run Code Online (Sandbox Code Playgroud)

所以我有一个@when函数产生(比方说)一个随机数,我需要在@then条件测试中出现这个数字.

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

Pet*_*ood 10

您可以在传递给步骤的上下文对象上设置数据.从文档:

@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)
Run Code Online (Sandbox Code Playgroud)

您还可以在测试运行的各个其他点,每个步骤,功能,方案,标记等之前和之后修改上下文.

  • 在撰写评论时,我正在尝试利用上下文。我不知道“行为”是将其作为一种通用对象来处理。你是对的,没有必要谈论本主题中的上下文是从哪里传递的。谢谢! (3认同)
  • @Kasra 为什么它的定义很重要?有问题吗? (2认同)