首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Pytest-bdd:导入公共步骤

Pytest-bdd:导入公共步骤
EN

Stack Overflow用户
提问于 2018-09-10 21:18:17
回答 2查看 3.3K关注 0票数 6

编辑:我不再从事这个项目,但我将把这个问题留到有人回答之前,以防它对任何人有用。

我正在实现pytest-bdd,并试图从一个名为ui_shared.py的不同文件中导入使用步骤。

目前,我的目录是结构化的:

代码语言:javascript
复制
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使用:

代码语言:javascript
复制
from ui_shared import *

但我想避免使用import *。

我尝试过import ui_shared.pyfrom ui_shared.py import common_step,其中common_step是我想要导入的step函数,我得到了错误:

代码语言:javascript
复制
StepDefinitionNotFoundError: Step definition is not found: Given "common function".

我发现了一个相关的问题:Behave: How to import steps from another file?以及其他几个问题,其中大多数都要求将步骤导入公共步骤文件,在我的例子中是ui_shared.py,我已经这样做了。

以下是ui_shared.py的代码

代码语言:javascript
复制
#!/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

代码语言:javascript
复制
Scenario Outline: ui_file
    Given common step
    And another given step
    When some step
    Then last step

test_file.py

代码语言:javascript
复制
#!/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

代码语言:javascript
复制
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的常见步骤

EN

回答 2

Stack Overflow用户

发布于 2021-07-02 12:46:33

conftest.py中添加以下行

代码语言:javascript
复制
pytest_plugins = [
   "lib.ui_shared"
]

这样,ui_shared.py中的所有固定装置或pytest-bdd步骤都可以用于其他每个文件,就好像它在conftest.py中一样。

注意事项

lib必须是一个包,这意味着lib文件夹中应该有一个__init__.py文件。

票数 1
EN

Stack Overflow用户

发布于 2020-03-04 10:06:25

你可以试试这样的东西:

代码语言:javascript
复制
from .ui_file import *

这就是我为我的项目写的:

代码语言:javascript
复制
from .devices_steps import *
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52265620

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档