本文介绍了行为:如何从另一个文件导入步骤?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用行为,这是一个使用小黄瓜语法.行为具有一项功能,例如:

I have just started using behave, a Pythonic BDD framework using Gherkin syntax. behave takes a feature, e.g.:

Scenario: Calling the metadata API
   Given A matching server
   When I call metadata
   Then metadata response is JSON
   And response status code is 200

还有一个步骤文件,例如:

And a steps file, e.g.:

...
@then('response status code is {expected_status_code}')
def step_impl(context, expected_status_code):
    assert_equals(context.response.status_code, int(expected_status_code))

@then('metadata response is JSON')
def step_impl(context):
    json.loads(context.metadata_response.data)
...

并将它们组合成精美的测试报告:

And combines them to a beautiful test resport:

其中一些步骤-例如:

  • metadata response is JSON
  • response status code is {expected_status_code}
  • metadata response is JSON
  • response status code is {expected_status_code}

我的许多项目中都使用了

,我想将它们分组到一个通用的步骤文件中,以供导入和重用.

Are used in many of my projects, and I would like to group them into a general steps file which I can import and reuse.

我尝试将有用的步骤提取到一个单独的文件并导入,但是收到以下错误:

I tried extracting useful steps to a separate file and importing it, but received the following error:

@then('response status code is {expected_status_code}')
NameError: name 'then' is not defined

如何创建通用步骤文件并将其导入?

How do I create a generic steps file and import it?

推荐答案

在导入的文件中,必须导入行为修饰符(例如then):

In the imported file, the behave decorators (like then) must be imported:

from behave import then
from nose.tools import assert_equals

@then('response status code is {expected_status_code}')
def step_impl(context, expected_status_code):
    assert_equals(context.response.status_code, int(expected_status_code))

...

这篇关于行为:如何从另一个文件导入步骤?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 23:22