执行 select 组合并防止 pytest 运行 所有参数化组合

Execute select combinations and prevent pytest from running all parameterise combinations

有3个用户和5个代码

@pytest.mark.fns
@pytest.mark.parametrize("USER, PWD", [(user1, user1pwd), (user2, user2pwd), (user3, user3pwd)])
@pytest.mark.parametrize("CODE", ['code1', 'code2', 'code3', 'code 4', 'code 5'])

def fn1(CODE, USER, PWD):
    ...
    ...
    do something
    ...
    ...

现在,如果我 运行 pytest -m "fns",函数 fn1 将 运行 15 次所有代码和用户、密码组合。但是我只希望它像下面这样 运行 5 次:

fn1(code1, user1, user1pwd)
fn1(code2, user2, user2pwd)
fn1(code3, user3, user3pwd)
fn1(code4, user1, user1pwd)
fn1(code5, user2, user2pwd)

即代码1,2和3传给user1,2和3,然后剩下的代码4和5传给user1和2

Edit1:代码的数量可以从 1 到 1000 不等,而只有 3 个用户。而且每个code只需要1个用户传递一次。

我看过固定装置,但对我来说太难理解了。有人可以向我解释我如何才能做到这一点吗?

import itertools

def fn1_parameters():
    for code, (user, pwd) in zip(
        ["code1", "code2", "code3", "code4", "code5"],
        itertools.cycle([
            ("user1", "user1pwd"),
            ("user2", "user2pwd"),
            ("user3", "user3pwd"),
        ])
    ):
        yield code, user, pwd

然后:

def test_fn1():
    for code, user, pwd in fn1_parameters():
        fn1(code, user, pwd)

# Not sure if list() is needed
@pytest.mark.parametrize("code", "user", "pwd", list(fn1_parameters()))
def fn1(code, user, pwd):