Search code examples
pythonpytestpytest-dependency

How to make a "factory" to create dependent fixtures for multiple parametrizations?


I have a test setup wherein I have multiple parametrizations, and I have secondary tests that depend on primary tests for each parametrization. I have got the following dependency setup working (see my SO question here, although for this question I'm doing everything in one file for simplicity). However, now that I'm applying it, I'm running into the issue that I have to create two new fixtures for each parametrization, resulting in a lot of duplicated code.

My setup:

tests/
  - common.py
  - test_0.py

common.py:

import numpy as np

ints = [1, 2, 3]
strs = ['a', 'b']
pars1 = list(zip(np.repeat(ints, 2), np.tile(strs, 3)))
pars2 = list(zip(np.repeat(ints[:2], 2), np.tile(strs, 2)))

test_0.py:

import numpy as np
import pytest
from pytest_dependency import depends

from common import pars1, pars2


def idfnc_mark(val):
    if isinstance(val, (int, np.int32, np.int64)):
        return f"n{val}"

def idfnc_fix(val):
    return "n{}-{}".format(*val)

# I use markers here because I have a lot of code parametrized this way
perm_mk1 = pytest.mark.parametrize('num, lbl', pars1, ids=idfnc_mark)
perm_mk2 = pytest.mark.parametrize('num, lbl', pars2, ids=idfnc_mark)

# 2 of these parametrized tests should fail
@perm_mk1
@pytest.mark.dependency()
def test_a(num, lbl):
    if num == 2:
        assert False
    else:
        assert True

# 2 of these parametrized tests should fail
@perm_mk2
@pytest.mark.dependency()
def test_b(num, lbl):
    if lbl == 'b':
        assert False
    else:
        assert True

# Following the example in the documentation for parametrized dependency
@pytest.fixture(params=pars1, ids=idfnc_fix)
def perm_fixt1(request):
    return request.param

@pytest.fixture()
def dep_perms1(request, perm_fixt1):
    depends(request, ["test_a[n{}-{}]".format(*perm_fixt1)])
    return perm_fixt1

@pytest.mark.dependency()
def test_c(dep_perms1):
    pass

This all works as expected. But, now I want to define a test_d that depends on test_b, which is parametrized by pars2 not pars1. I really don't want to make two new fixtures - my real code has 5 parametrizations, which would be 10 fixtures all following the exact same pattern. Is there a way to generate the dependent fixture in a function or factory of some sort? I tried something like this:

def make_dependency(name, params, ref_function, perm_format, id_func):

    @pytest.fixture(params=params, ids=id_func)
    def orig(request):
        return request.param

    @pytest.fixture(name=name)
    def dep_test(request, orig):
        depends(request, [ref_function+"["+perm_format.format(*orig)+"]"])
        return orig

    return dep_test

dep_pars1 = make_dependency("dep_pars1", pars1, "test_0.py", "test_a", "n{}-{}", idfnc_fix)

But, that complains that it can't find the orig fixture. And that makes sort of sense, maybe; I did know that calling it multiple times would probably cause naming / scope issues. Does pytest-dependency support doing something like this?


Solution

  • Yes indeed, pytest-dependency is able to do this pretty easily, you just need to use a slightly different setup for the dependencies. An example of this alternative setup can be found in the Advanced Usage section of the pytest-dependency documentation here, specifically in "Dynamic compilation of marked parameters".

    Instead of relying on the params keyword to a fixture, we'll explicitly form the dependency for each parameter within the parametrization by using a list comprehension. Then the result can be sent to parametrize and the dependencies will be included. You can write each one individually like:

    plist1 = [
        pytest.param(x, id="n{}-{}".format(*x),
                     marks=pytest.mark.dependency(depends=["test_a[n{}-{}]".format(*x)]))
        for x in pars1
    ]
    plist2 = [
        pytest.param(x, id="n{}-{}".format(*x),
                     marks=pytest.mark.dependency(depends=["test_b[n{}-{}]".format(*x)]))
        for x in pars2
    ]
    

    Or, as might be better suited if you have a lot of them, use a normal function to execute the list comprehension with similar parameters as you tried in your make_dependency attempt.

    def param_factory(params, ref_func, id_formatter):
        param_list = [
            pytest.param(x, id=id_formatter.format(*x),
                         marks=pytest.mark.dependency(depends=[ref_func+"["+id_formatter.format(*x)+"]"]))
            for x in params
        ]
        return param_list
    
    plist1 = param_factory(pars1, "test_a", "n{}-{}")
    plist2 = param_factory(pars2, "test_b", "n{}-{}")
    
    @pytest.mark.parametrize("p2", plist2)
    def test_d(p2):
        pass
    

    Now, test_d should reflect its dependencies appropriately, and adding a new dependent parameterization is as easy as one more call to param_factory. Depending on what the parametrizations are you may want to / need to customize the formation of the IDs differently, but this will work for the code in the question.