user3435903
user3435903

Reputation: 133

How to pass fixture when parametrizing a test

I am trying to parametrize my test. In the setup method which returns a list, I am calling a fixture (app_config).

Now, i want to call the setup so that the list can be used as a parameter values inside the test.

The problem i am running into is that i cannot pass app_config fixture when calling setup in the parametrize decorator.

def setup(app_config):
    member = app_config.membership
    output = app_config.plan_data
    ls = list(zip(member, output))
    return ls

@pytest.mark.parametrize('member, output', setup(app_config))
def test_concentric(app_config, member, output):
    ....
    ....

Is there an elegant way to pass setup method in the parametrize decorator or any other way to approach this?

Upvotes: 1

Views: 530

Answers (1)

Andrey Glazkov
Andrey Glazkov

Reputation: 2592

Unfortunately, starting with pytest version 4, it has become impossible to call fixtures like regular functions.


In your case I can recommend not using fixtures and switch to normal functions. For example, it might look like this:

import pytest


def app_config():
    membership = ['a', 'b', 'c']
    plan_data = [1, 2, 3]
    return {'membership': membership,
            'plan_data': plan_data}

def setup_func(config_func):
    data = config_func()
    member = data['membership']
    output = data['plan_data']
    ls = list(zip(member, output))
    return ls


@pytest.mark.parametrize('member, output', setup_func(app_config))
def test_concentric(member, output):
    print(member, output)
    ....

NB! Avoid the setup() function/fixture name because it will conflict with pytest.runner's internals.

Upvotes: 1

Related Questions