How to make a “factory” to create dependent fixtures for multiple paramtrizations?

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?

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật