Carla
Carla

Reputation: 3390

Passing parameters to fixture


I'm just learning Pythos fixtures in Pytest. So far I couldn't find an example of how to pass function's parameters in a fixture. Something like this:

@pytest.mark.usefixtures('checkname')
class TestFixtures:
    @pytest.mark.parametrize('name', ['Gloria', 'Haley'])
    def test_one(self):
        print("Test one executed")

And in conftest.py:

@pytest.fixture()
def checkname():
    print(name) #Undefined!

Should I make the 'name' parameter visible in a class, or is it possible to pass the parameter to the fixture somehow? Thanks

Upvotes: 0

Views: 100

Answers (1)

sanyassh
sanyassh

Reputation: 8550

To pass parameters to fixture you can use indirect parametrization:

import pytest

@pytest.fixture
def checkname(request):
    print(request.param)

@pytest.mark.usefixtures('checkname')
class TestFixtures:
    @pytest.mark.parametrize('checkname', ['Gloria', 'Haley'], indirect=True)
    def test_one(self):
        print("Test one executed")

Upvotes: 2

Related Questions