Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call the same fixture multiple time in py.test

I'm using Pytest to write some tests in Python.

I'm using the following fixture in order to create a user and user it in my test, then delete it after the test (finalizer):

@pytest.fixture(scope='function')
def test_user(request):
    def finalizer():
        delete_the_user()

    request.addfinalizer(finalizer)
    return user()

with delete_the_user() and user() two functions not detailed here.

The problem is: I'd like to use 2 users for some of my tests.

I've tried to call the fixture like that:

def test_function(test_user_1 = test_user, test_user_2 = test_user):
    # Test here

without success.

How can I use the same fixture more than once in a test?

like image 998
superzouzou Avatar asked Feb 07 '23 02:02

superzouzou


1 Answers

The only ways to do this I can think of are:

  • Having a second fixture
  • Making the fixture return a function instead (but then you'd have to refactor other functions using the fixture too).
like image 140
The Compiler Avatar answered Feb 19 '23 18:02

The Compiler