python - 行为 : How to import steps from another file?

标签 python bdd python-behave

我刚开始使用 behave ,一个使用 Gherkin syntax 的 Pythonic BDD 框架. behave 需要一个特征,例如:

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

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

...
@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)
...

并将它们组合成一个漂亮的测试报告:

Test results

其中一些步骤 - 例如:

  • 元数据响应是 JSON
  • 响应状态码为{expected_status_code}

在我的许多项目中都有使用,我想将它们分组到一个我可以导入和重复使用的通用步骤文件中。

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

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

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

最佳答案

仅供所有(像我一样)尝试导入单步定义的人使用: 不要!

只需导入整个模块。

这里是所有还需要更多细节的人(比如我......):

如果您的项目结构如下所示:

foo/bar.py
foo/behave/steps/bar_steps.py
foo/behave/bar.feature
foo/common_steps/baz.py

就这样

import foo.common_steps.baz

在 foo/behave/steps/bar_steps.py(这是你的常规步骤文件)

关于python - 行为 : How to import steps from another file?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21523579/

相关文章:

Python Behave 可选参数

Python xml.etree.ElementTree 问题

python - 使用 pandas 的 python 中的频率表

c++ - 用于检测分号终止的 C++ for 和 while 循环的正则表达式

javascript - 使用 Protractor 和 Gulp 在 CucumberJS 中加载 Hook

cucumber - Python 行为 : How to read examples from external excel file for Scenarios Outline

python - Steam API 获取价格列表

ruby-on-rails - 在进行验收测试和遵循 BDD 时,您是断言数据库更改还是仅断言用户看到的内容?

java-8 - 始终使用Cucumber-Java8获取异常 "Wrong type at constant pool index"

python - Selenium 中的单例实现 [Python]