How to give dynamic values to parametrize @pytest.mark.parametrize in pytest

864 views Asked by At
tests_arr = ['test1','test2']
logs_arr = ['log1','log2']
def List_of_Tests():     
    tests = tests_arr    
    return tests

def List_of_Logs():     
    logs = logs_arr    
    return logs



@pytest.mark.parametrize("test, log",
                         [
                             ([i for i in List_of_Tests()], [i for i in List_of_Logs()])
                         ]
                         )
def methodname(test,log):
    print(test+ " " + log)

When I try to pas the list items dynamically, I'm getting the below error

TypeError: expected str, bytes or os.PathLike object, not list

1

There are 1 answers

0
dosas On

I am not sure what parametrization you want but I guess:

test_foo/test_stuff.py::test_stuff[test1-log1] PASSED                                                                                                                                                       [ 50%]
test_foo/test_stuff.py::test_stuff[test2-log2] PASSED                                                                                                                                                       [100%]

then you should use zip.

import pytest

tests_arr = ['test1','test2']
logs_arr = ['log1','log2']

def List_of_Tests():
    return tests_arr

def List_of_Logs():
    return logs_arr

@pytest.mark.parametrize("test, log", zip(List_of_Tests(), List_of_Logs()))
def test_stuff(test, log):
    pass

But what you are doing looks strange, did you consider using pytest_generate_tests?