Sakthi Sakthi - 1 month ago 9
Python Question

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

I have a test written in python using pytest.
which has conftest.py for setup, which parameterize 5 account ids for each test.
The Test Class has total 5 tests, out of which 4 needs the tests to be parameterized using metafunc (Which i have done in conftest.py), the remaining 1 test need not be parameterized. please tell how to run all these tests in one go with avoiding parameterization for the last test (test5).

my conftest.py has the following;

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


my test file is named test_accid.py; which has the following;

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>

Answer

Resolved this by adding the following check in conftest.py;

if 'accid' in metafunc.fixturenames:
    metafunc.parametrize("accid", accid)

So this will check if the accid is a parameter defined in test function, then only it will parameterize the test.

for my last test i removed the accid parameter, and its working now.