首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >找不到pytest fixture (pytest-bdd)

找不到pytest fixture (pytest-bdd)
EN

Stack Overflow用户
提问于 2020-12-01 10:50:32
回答 2查看 1.5K关注 0票数 3

我有以下步骤定义,这些步骤定义会导致错误,因为即使在target_fixture中定义了@given fixture,也没有找到它

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

这将导致以下错误:

代码语言:javascript
复制
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会被认为是固定物?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-12-03 18:28:04

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

代码语言:javascript
复制
@when(...)
def register(admin_login, ...):
    ...

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

应该行得通。

票数 3
EN

Stack Overflow用户

发布于 2020-12-04 07:06:44

小黄瓜步骤函数的名称或前缀并不重要:在pytest-bdd中,所有场景部分都被转换为具有特殊形式的名称损坏的fixture。据我所知,这是无法阻止的,并且是BDD和相关工具的一长串问题和大量过度复杂的问题之一,这些问题使QA学科倒退了可能十年。

但是如果您被迫使用BDD (学校作业等),只需将任何小黄瓜步骤函数也标记为常规的pytest fixture,您就可以按照预期的方式(通过函数名,无需调用括号)将其作为fixture传递,即使使用pytest-bdd框架也是如此:

""“

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

    ...

""“

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65084044

复制
相关文章

相似问题

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