py.test - pytest.mark.parametrize to read from list to generate tests dynamically -
i have function reads yaml file , generates test iterations list of dictionaries below:
iterations = lib_iterations() print iterations iterations = [{'mode':1,'format':5,'ip':'192.16.1.103'}, {'mode':2,'format':6,'ip':'192.16.1.104'}, {'mode':2,'format':8,'ip':'192.16.1.110'}, {'mode':6,'format':2,'ip':'192.16.1.105'}, {'mode':5,'format':7,'ip':'192.16.1.102'}, {'mode':4,'format':2,'ip':'192.16.1.101'}] i need able pass set pytest.mark.parametrize generate test each iteration/each row in list.
i not know dictionary keys before call function lib_iterations generates list of dictionaries.
any ideas on how this?
thanks ab
if problem how pass iterations list pytest parametrize decorator, can this:
@pytest.mark.parametrize('testdata', [i in iterations]) if content of iterations unknown in test module can use pytest_generate_tests hook. in hook function can load yaml file , pass content metafunc.parametrize:
import yaml def pytest_generate_tests(metafunc): if 'testdata' in metafunc.fixturenames: open("testdata.yml", 'r') f: iterations = yaml.load(f) metafunc.parametrize('testdata', [i in iterations]) the test function using fixture:
def test_it(testdata): print testdata
Comments
Post a Comment