编辑:我不再从事这个项目,但我将保留这个问题,直到得到答复,以防它对任何人有用。
我正在努力实现 pytest-bdd,并尝试从名为 ui_shared.py 的不同文件导入使用步骤。
目前我的目录结构如下:
proj/lib/ui_file.py
proj/lib/ui_shared.py
proj/tests/test_file.py
proj/features/file.feature
Run Code Online (Sandbox Code Playgroud)
Pytest-bdd 能够识别 ui_shared.py 中的步骤并执行测试,只要 ui_file.py 导入如下:
from ui_shared import *
Run Code Online (Sandbox Code Playgroud)
但我想避免使用 import *.
我尝试过import ui_shared.py,from ui_shared.py import common_step我common_step想要导入的步骤函数在哪里,但出现错误:
StepDefinitionNotFoundError: Step definition is not found: Given "common function".
Run Code Online (Sandbox Code Playgroud)
我发现了一个相关问题:
行为:如何从另一个文件导入步骤?
以及其他一些内容,其中大多数都说将步骤导入到通用步骤文件中,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
Run Code Online (Sandbox Code Playgroud)
以下是其他相关代码:
在file.feature:
Scenario Outline: ui_file
Given common step
And another given step
When some step
Then last step
Run Code Online (Sandbox Code Playgroud)
在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"""
Run Code Online (Sandbox Code Playgroud)
并在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
Run Code Online (Sandbox Code Playgroud)
上面应该按原样工作,但是如果更改导入方法,则 pytest 会失败并显示E StepDefinitionNotFoundError.
我正在寻找的是一种导入ui_shared.py除我未使用的方法之外定义的所有名称的方法。
基本上,如何在from file import不使用 * 的情况下导入 using 并允许我ui_file.py使用 中的常见步骤ui_shared.py?
小智 0
你可以尝试这样的事情:
from .ui_file import *
Run Code Online (Sandbox Code Playgroud)
这就是我为我的项目写的:
from .devices_steps import *
Run Code Online (Sandbox Code Playgroud)