【问题标题】:How to reuse steps definition of one feature file implementation in another one in pytest_bdd?如何在 pytest_bdd 中的另一个功能文件实现中重用一个功能文件实现的步骤定义?
【发布时间】:2020-01-31 07:11:39
【问题描述】:

我有以下文件夹结构

folder root
    features
         file1.feature
         file2.feature
    source
         file1.py
         file2.py

file1.feature的实现在file1.py中,file2.featurefile2.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


【解决方案1】:

选项 1:

您可以在conftest.py创建常用步骤

@given("User is logged-in")
def logging_in():
    print('logging_in')


@given("User is in Home page")
def homepage():
    print('homepage')

并添加常用功能文件common_steps.feature

Scenario: All steps are declared in the conftest
    Given User is logged-in
    Given User is in Home page

并在测试中添加另一个@scenario

@scenario('common_steps.feature', 'All steps are declared in the conftest')
@scenario('file1.feature', 'Creating a new Action 1')
def test_login_page():
    pass


@when("User clicks on New in the Dashboard")
def new():
    print('new')


@when("User selects Action 1")
def act1():
    print('act1')


@then("Action 1 is created")
def logged_in():
    print('logged_in')

选项 2:

common_steps.feature 中使用Background

Feature: Common steps

Background:
    Given User is logged-in
    And User is in Home page

Scenario: Creating a new Action 1
    When User clicks on New in the Dashboard
    And User selects Action 1
    Then Action 1 is created

Scenario: Creating a new Action 2
    When User clicks on New in the Dashboard
    And User selects Action 2
    Then Action 2 is created

还有测试

@scenario('common_steps.feature', 'Creating a new Action 1')
def test_login_page():
    pass


@given("User is logged-in")
def logging_in():
    print('logging_in')


@given("User is in Home page")
def homepage():
    print('homepage')


@when("User clicks on New in the Dashboard")
def new():
    print('new1')


@when("User selects Action 1")
def act1():
    print('act1')


@then("Action 1 is created")
def logged_in():
    print('logged_in')

选项 3:

test_common.py 中定义pytest.fixture

@pytest.fixture
def common_logging_in():
    print('common logging_in')


@pytest.fixture
def common_homepage():
    print('common homepage')

并将其作为值发送到测试步骤

@scenario('file1.feature', 'Creating a new Action 1')
def test_login_page():
    pass


@given("User is logged-in")
def logging_in(common_logging_in):
    pass


@given("User is in Home page")
def homepage(common_homepage):
    pass


@when("User clicks on New in the Dashboard")
def new():
    print('new1')


@when("User selects Action 1")
def act1():
    print('act1')


@then("Action 1 is created")
def logged_in():
    print('logged_in')

答案基于Pytest-BDD Documentation的想法

【讨论】:

  • 选项 1:如果您的任何常用步骤使用了 conftest 文件中定义的夹具,那么他们将无法找到该夹具,并且测试将引发 FixtureLookupError。选项 2:在我看来不是一个非常干净的解决方案,因为任何产品中可能没有任何“通用步骤”功能。功能文件应该描述实际的业务功能,而不是作为 pytest 管道胶带的地方。选项 3 只是部分解决方案,因为仍然必须定义步骤
猜你喜欢
  • 2018-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多