Python Pytest bdd:导入常用步骤

Python Pytest bdd:导入常用步骤,python,bdd,Python,Bdd,编辑:我不再从事此项目,但我会将此问题留待回答,以防对任何人有用。 我正在致力于实现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

编辑:我不再从事此项目,但我会将此问题留待回答,以防对任何人有用。

我正在致力于实现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 import common_step导入ui_shared.py和
,其中
common_step
是我要导入的步骤函数,我得到错误:

StepDefinitionNotFoundError: Step definition is not found: Given "common function".
我发现了一个相关的问题: 以及其他一些步骤,其中大多数都表示要将步骤导入公共步骤文件,
ui\u shared.py
,在我的例子中,我已经这样做了

以下是
ui\u 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
#!/usr/bin/python

@pytest.fixture
def pytestbdd_strict_gherkin():
    return False

@scenario('proj/features/file.feature', 'ui_file')
def test_ui_file():
    """ui_file"""
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
以下是其他相关代码:

文件.功能中

Scenario Outline: ui_file
    Given common step
    And another given step
    When some step
    Then last step
测试文件.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
#!/usr/bin/python

@pytest.fixture
def pytestbdd_strict_gherkin():
    return False

@scenario('proj/features/file.feature', 'ui_file')
def test_ui_file():
    """ui_file"""
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
ui\u file.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
#!/usr/bin/python

@pytest.fixture
def pytestbdd_strict_gherkin():
    return False

@scenario('proj/features/file.feature', 'ui_file')
def test_ui_file():
    """ui_file"""
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\u shared.py
中定义的所有名称,但我不使用的方法除外


基本上,我如何在不使用*的情况下使用
从文件导入
,并允许我的
ui\u file.py
使用
ui\u shared.py
中的常用步骤

您可以尝试以下方法:

from .ui_file import *
这就是我为我的项目写的:

from .devices_steps import *

从ui\u文件导入上下文,common\u step
?@EPo我也尝试过,它返回相同的错误
StepDefinitionNotFoundError:step definition未找到:给定“common function”。
也许您需要创建一个最小的示例(其他文件的相关内容)允许复制错误并将其发布到问题。一旦您有了它,您还可以在project github存储库中提出问题,并从维护人员和其他用户那里获得建议。github中已经存在一些StepDefinitionNotFoundError问题,但它们有些不同。@EPo我用其他相关文件的内容编辑了我的问题,希望您可以重现错误。我也会看看他们的git回购,谢谢你的建议!