mrGreenBrown
mrGreenBrown

Reputation: 596

How to execute pytest fixture on each parameter for test function?

In my current setup of end-to-end tests I am using Selenium. I have a fixture that looks something like this:

@pytest.fixture(scope="session")
def browser(request):
    # Here I do a basic setup
    # Setting up accounts from configuration based on input from test function
    # Initializing webdriver wrapper with this data
    # yield driver
    # teardown

So far I was only using parameters for a fixture and typical test function would look like this:

@pytest.mark.parametrize('browser', [(SomeEnum, AnotherEnum1),
                                    (SomeEnum, AnotherEnum2)], indirect=True)
def some_test(browser):

This will result in two tests:

I am trying to combine parameters for a function and parameters for a fixture now, so test function looks like this:

@pytest.mark.parametrize('browser', [([SomeEnum1, SomeEnum2], AnotherEnum)], indirect=True)
@pytest.mark.parametrize('param1,param2', [(DifferentEnum, False), (DifferentEnum2, True)])
def some_test(browser, param1, param2):

This setup will result in 2 tests, which I want:

If I run tests individually, everything is fine. But if I run them together, first one will finish and pass and it seems that second one doesn't go through the fixture at all, but browser session just stays open.

What I need to change for fixture to be executed for each of the tests?

Upvotes: 1

Views: 1096

Answers (1)

hoefling
hoefling

Reputation: 66261

Narrow the scope of the browser fixture:

@pytest.fixture(scope="function")
def browser(request):
    ...

or just drop it completely since function is the default scope.

@pytest.fixture
def browser(request):
    ...

Upvotes: 1

Related Questions