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
Ark Medes
  • 233
  • 1
  • 2
  • 9

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)
Ark Medes
  • 233
  • 1
  • 2
  • 9
  • You should also take a look at that: https://stackoverflow.com/a/44701916/9515831 Do not forget that fixtures are just decorators. – Victor Jun 19 '19 at 04:34
  • @Reese Woah! That looks more pythonic. Thank you for mentioning about that! – Ark Medes Jun 19 '19 at 08:46