0

I'm trying to create a Pytest Fixture and have "user_type" argument within the fixture. And then evaluate the passed argument in if statement.

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
Paulo Sairel Don
  • 129
  • 1
  • 1
  • 7

1 Answers1

0

I solved it by using parametrize and indirect=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)
Paulo Sairel Don
  • 129
  • 1
  • 1
  • 7