【发布时间】:2020-01-31 07:11:39
【问题描述】:
我有以下文件夹结构
folder root
features
file1.feature
file2.feature
source
file1.py
file2.py
file1.feature的实现在file1.py中,file2.feature在file2.py中>。我试图在 file2.feature 中重用 file1.feature 中的一个步骤。我尝试直接在 file2.py 中导入该方法,如下所示
from source.file2 import method1
但它导致以下错误被触发
def _find_step_function(request, step, scenario, encoding):
"""Match the step defined by the regular expression pattern.
:param request: PyTest request object.
:param step: Step.
:param scenario: Scenario.
:return: Function of the step.
:rtype: function
"""
name = step.name
try:
# Simple case where no parser is used for the step
return request.getfixturevalue(get_step_fixture_name(name, step.type, encoding))
except pytest_fixtures.FixtureLookupError:
try:
# Could not find a fixture with the same name, let's see if there is a parser involved
name = find_argumented_step_fixture_name(name, step.type, request._fixturemanager, request)
if name:
return request.getfixturevalue(name)
raise
except pytest_fixtures.FixtureLookupError:
raise exceptions.StepDefinitionNotFoundError(
u"""Step definition is not found: {step}."""
""" Line {step.line_number} in scenario "{scenario.name}" in the feature "{feature.filename}""".format(
step=step,
scenario=scenario,
> feature=scenario.feature,
)
) pytest_bdd.exceptions.StepDefinitionNotFoundError: Step definition is not found: Given "User is logged-in". Line 6 in scenario "Scenario 1" in the feature "../Features/file1.feature
有什么方法可以有效地重用pytest_bdd中一个特征文件的步骤
请在下面找到功能文件file1
Feature: Action 1
Scenario: Creating a new Action 1
Given User is logged-in
And User is in Home page
When User clicks on New in the Dashboard
And User selects Action 1
Then Action 1 is created
和文件2
Feature: Action 2
Scenario: Creating a new Action 2
Given User is logged-in
And User is in Home page
When User clicks on New in the Dashboard
And User selects Action 2
Then Action 2 is created
请在下面找到 file1.py
的 stepdefinition 文件from pytest_bdd import scenario, given, when, then
@scenario('../Features/file1.feature','Creating a new Action 1')
def test_login_page():
pass
@given("User is logged-in")
def logging_in():
//some actions
pass
@given("User is in Home page")
def homepage():
//some actions
pass
@when("clicks on New in the Dashboard")
def new():
//some actions
pass
@when("User selects Action 1")
def act1():
//some actions
pass
@then("Action1 is created")
def logged_in():
//some actions
pass
我正在尝试找到在 file2.py 中实现 file2.feature 的 stepdefinition 的方法,而不重复 file1.py 中已经定义的 stepdefinition。我尝试如下直接导入方法,但导致粘贴错误。
import logging_in, homepage
【问题讨论】:
-
发布功能文件。
-
@Guy.. 完成
-
谢谢。目前还不是很清楚你到底想做什么,从另一个文件调用一个函数?从另一个功能文件中使用
@given创建一个函数?别的东西?请添加您的代码。 -
@Guy,很抱歉造成混乱,我再次编辑了问题,请检查并建议。
标签: python selenium testing automated-tests pytest