pytest:如何忽略 class 中少数测试的 metafunc 参数化值

pytest: how to ignore metafunc parameterize values for few tests in a class

我有一个使用 pytest 在 python 中编写的测试。 其中有 conftest.py 用于设置,它为每个测试参数化 5 个帐户 ID。 测试 Class 共有 5 个测试,其中 4 个需要使用 metafunc 对测试进行参数化(我在 conftest.py 中完成),其余 1 个测试不需要参数化。请告诉我们如何 运行 一次性完成所有这些测试,同时避免最后一个测试 (test5) 的参数化。

我的 conftest.py 有以下内容;

def pytest_generate_tests(metafunc):
        accid = ['string1','string2','string3','string4','string5']
        metafunc.parametrize("accid", accid)

我的测试文件名为test_accid.py;其中有以下内容;

    class TestAccount:
        def test_base1(self, accid):
            <test code>
        def test_base2(self, accid):
            <test code>
        def test_base3(self, accid):
            <test code>
        def test_base4(self, accid):
            <test code>

        #The following test should not have accid
        def test_no_accid(self):
            <test code>

通过在 conftest.py;

中添加以下检查解决了这个问题
if 'accid' in metafunc.fixturenames:
    metafunc.parametrize("accid", accid)

所以这将检查accid是否是测试函数中定义的参数,然后只有它会参数化测试。

在上次测试中,我删除了 accid 参数,现在它可以正常工作了。