PYTEST-BDD:导入常用步骤 [英] Pytest-bdd: Importing common steps

查看:80
本文介绍了PYTEST-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 ui_shared import *

但我希望避免使用IMPORT*。

我已经尝试了import ui_shared.pyfrom 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中的常见步骤?

推荐答案

conftest.py

中添加以下行
pytest_plugins = [
   "lib.ui_shared"
]

这样,ui_shared.py中的所有装置或pytest-bdd步骤将可供其他所有文件使用,就像它位于conftest.py

中一样

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

这篇关于PYTEST-BDD:导入常用步骤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆