编辑:我不再从事这个项目,但是在任何人都可以使用的情况下,我将一直待回答此问题。
我正在努力实现pytest-bdd,并且试图从另一个名为ui_shared.py的文件中导入使用步骤。
当前我的目录结构:
proj/lib/ui_file.py
proj/lib/ui_shared.py
proj/tests/test_file.py
proj/features/file.feature
Pytest-bdd能够识别ui_shared.py中的步骤并执行测试,只要ui_file.py可以通过以下方式导入:
from ui_shared import *
但我想避免使用导入*。
我尝试了import ui_shared.py
和from ui_shared.py import common_step
,其中common_step
是我要导入的步进函数,但出现错误:
StepDefinitionNotFoundError: Step definition is not found: Given "common function".
我发现了一个相关的问题:Behave: How to import steps from another file?以及其他一些,其中大多数表示将步骤导入到通用步骤文件中(在我的情况下为ui_shared.py
,已经完成。)>
这里是ui_shared.py
的代码:
#!/usr/bin/python import pytest from pytest_bdd import ( scenario, given, when, then ) @pytest.fixture(scope="function") def context(): return{} @given('common step') def common_step(input): #some method
这里是其他相关代码:
在file.feature
中:
Scenario Outline: ui_file Given common step And another given step When some step Then last step
在
test_file.py
中:
#!/usr/bin/python @pytest.fixture def pytestbdd_strict_gherkin(): return False @scenario('proj/features/file.feature', 'ui_file') def test_ui_file(): """ui_file"""
并且在
ui_file.py
中:
import pytest from pytest_bdd import ( scenario, given, when, then ) from ui_shared import * #This is what I am trying to change @given('another given step') def another_given_step(input) #some method @when('some step') def some_step(input) #some method @then('last step') def last_step(input) #some method
以上代码应该可以正常工作,但是如果导入方法发生了变化,则pytest会失败,并显示
E StepDefinitionNotFoundError
。
我正在寻找的是一种导入ui_shared.py
中定义的所有名称的方法,除了我不使用的方法。
[基本上,如何使用不带*的from file import
进行导入,并允许我的ui_file.py
使用ui_shared.py
中的常用步骤?
编辑:我不再从事这个项目,但是我将把这个问题一直待解决,直到有人回答为止。我正在实现pytest-bdd,正在尝试导入使用...
您可以尝试这样的事情: