【问题标题】:Passing Argument to Pytest Fixture, and the argument will be use in if statement将参数传递给 Pytest Fixture,参数将在 if 语句中使用
【发布时间】:2019-11-01 16:25:22
【问题描述】:

我正在尝试创建一个 Pytest 夹具并在夹具中包含“user_type”参数。然后在 if 语句中评估传递的参数。

conftest.py

import pytest

# Some custom modules...

@pytest.fixture
def generate_random_user(_db, user_type):
    # Do stuff
    if user_type == "no-subscription":
        # Do stuffs

        yield _db

        _db.drop_all()

    elif user_type == "with-subscription":
        pass

test.py

@pytest.mark.usefixtures("live_server", "client")
def test_checkout_new_customer(selenium, generate_random_user("no-subscription")):
    pass

【问题讨论】:

    标签: python-3.x flask pytest


    【解决方案1】:

    我通过使用参数化和间接=True 解决了这个问题

    conftest.py

    @pytest.fixture
    def generate_random_user(_db, request):
    
        user_type = request.param
    
        if user_type == "no-subscription":
            # Create User
    
            yield _db
    
            _db.drop_all()
    
        elif user_type == "with-subscription":
            pass
    
    

    test.py

    
    @pytest.mark.usefixtures("live_server", "client", "firefox_headless", "_db")
    @pytest.mark.parametrize("generate_random_user", ["no-subscription"], indirect=True)
    def test_checkout_new_customer(selenium, generate_random_user):
        # Check if user has been created successfully
        user5 = models.User.query.get(5)
        print(user5)
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-02
    相关资源
    最近更新 更多