编辑:我不再从事这个项目,但我将把这个问题留到有人回答之前,以防它对任何人有用。
我正在实现pytest-bdd,并试图从一个名为ui_shared.py的不同文件中导入使用步骤。
目前,我的目录是结构化的:
proj/lib/ui_file.py
proj/lib/ui_shared.py
proj/tests/test_file.py
proj/features/file.featurePytest-bdd能够识别来自ui_shared.py的步骤并执行测试,只要ui_file.py使用:
from ui_shared import *但我想避免使用import *。
我尝试过import ui_shared.py和from ui_shared.py import common_step,其中common_step是我想要导入的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的常见步骤
发布于 2021-07-02 12:46:33
在conftest.py中添加以下行
pytest_plugins = [
"lib.ui_shared"
]这样,ui_shared.py中的所有固定装置或pytest-bdd步骤都可以用于其他每个文件,就好像它在conftest.py中一样。
注意事项
lib必须是一个包,这意味着lib文件夹中应该有一个__init__.py文件。
发布于 2020-03-04 10:06:25
你可以试试这样的东西:
from .ui_file import *这就是我为我的项目写的:
from .devices_steps import *https://stackoverflow.com/questions/52265620
复制相似问题