3

我有以下步骤定义,这会导致错误,因为@given找不到夹具,即使它定义在target_fixture

import pytest
from pytest_bdd import scenario, given, when, then, parsers
from admin import Admin

@scenario('../features/Admin.feature',
          'register a new user')

def test_admin():
    pass

@given('I\'m logged in as an admin at <host_name> with email <admin_email> and password <admin_password>', target_fixture="admin_login")
def admin_login(host_name, admin_email, admin_password):
    admin = Admin(admin_email, admin_password)
    admin.login(host_name)
#    assert admin.status_code == 200
    return admin

@when('I call the register method for host <host_name> with email <user_email> and password <user_password> and firstName <first_name> and last name <last_name>')
def test_register(admin_login, host_name, user_email, first_name, last_name):
    admin_login.register(host_name, user_email, first_name, last_name)
    assert admin_login.status_code == 200

@then('the user will be able to log in to <host_name> with email <user_email> and password <user_password>')
def test_login(admin_login):
    print(admin_login)
    assert 3 == 3

这会导致错误:

platform darwin -- Python 3.8.5, pytest-6.1.2, py-1.9.0, pluggy-0.13.1
rootdir: /Users/davidjoseph/work/
plugins: bdd-4.0.1
collected 3 items                                                                                                                                       

tests/step_defs/test_admin.py EEF                                                                                                                 [100%]

======================================================================== ERRORS =========================================================================
____________________________________________________________ ERROR at setup of test_register ____________________________________________________________
file /Users/davidjoseph/work/tests/step_defs/test_admin.py, line 18
  @when('I call the register method for host <host_name> with email <user_email> and password <user_password> and firstName <first_name> and last name <last_name>')
  def test_register(admin_login, host_name, user_email, first_name, last_name):
E       fixture 'admin_login' not found
>       available fixtures: cache, capfd, capfdbinary, caplog, capsys, capsysbinary, doctest_namespace, monkeypatch, pytestbdd_given_I'm logged in as an admin at <host_name> with email <admin_email> and password <admin_password>, pytestbdd_given_trace, pytestbdd_then_the user will be able to log in to <host_name> with email <user_email> and password <user_password>, pytestbdd_then_trace, pytestbdd_when_I call the register method for host <host_name> with email <user_email> and password <user_password> and firstName <first_name> and last name <last_name>, pytestbdd_when_trace, pytestconfig, record_property, record_testsuite_property, record_xml_attribute, recwarn, tmp_path, tmp_path_factory, tmpdir, tmpdir_factory
>       use 'pytest --fixtures [testpath]' for help on them.

谁能告诉我为什么admin_login被认为是固定装置?

4

2 回答 2

3

降级到pytest-bdd<4仍然接受此行为的位置,或者通过删除test_前缀重命名步骤以防止pytest将它们识别为单独的测试。

@when(...)
def register(admin_login, ...):
    ...

@then(...)
def login(admin_login):
    ...

应该管用。

于 2020-12-03T10:28:04.410 回答
1

Gherkin 步进函数的名称或前缀无关紧要:所有场景部分都在 pytest-bdd 中通过特殊形式的名称修饰转换为“幕后”的固定装置。据我所知,这是无法阻止的,并且是 BDD 和相关工具的一长串问题和大量过度复杂性之一,这些问题可能使 QA 学科倒退了十年。

但是,如果您被迫使用 BDD(学校作业等),只需将任何 Gherkin step 函数也标记为常规 pytest 夹具,您可以按照您期望的方式将其作为夹具传递(通过函数名称,不调用括号),即使使用 pytest-bdd 框架:

"""

@pytest.fixture
@given('I\'m logged in as an admin at <host_name> with email <admin_email> and password <admin_password>') # Remove """target_fixture""" arg
def admin_login(host_name, admin_email, admin_password):

    ...

"""

于 2020-12-03T23:06:44.353 回答