I have a function which reads yaml file and generates test iterations as a list of dictionaries like 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 to be able to pass this set to pytest.mark.parametrize
to generate a test for each iteration/each row in the list.
I do not know the dictionary keys before I call the function lib_iterations
which generates this list of dictionaries.
Any ideas on how to do this?
If your problem is how to pass Iterations list to pytest parametrize decorator, then you can do this:
@pytest.mark.parametrize('testdata', [i for i in Iterations])
If content of Iterations is unknown in the test module then you can use pytest_generate_tests
hook. In that hook function you can load the yaml file and pass its content to metafunc.parametrize
:
import yaml
def pytest_generate_tests(metafunc):
if 'testdata' in metafunc.fixturenames:
with open("testdata.yml", 'r') as f:
Iterations = yaml.load(f)
metafunc.parametrize('testdata', [i for i in Iterations])
The test function using this fixture:
def test_it(testdata):
print testdata
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With