我刚开始使用 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)
...
并将它们组合成一个漂亮的测试报告:
其中一些步骤 - 例如:
元数据响应是 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/