How can I run a single test out of a set configured with parametrize? Let’s say I have the following test method:
JavaScript
x
11
11
1
@pytest.mark.parametrize(PARAMETERS_LIST, PARAMETERS_VALUES)
2
def test_my_feature(self, param1, param2, param3):
3
"""
4
test doc
5
"""
6
if param1 == 'value':
7
assert True
8
else:
9
print 'not value'
10
assert False
11
I have 3 parameters, and I generate a list of 15 different possible values for them, to test the function on.
How can I run just one of them? except for the obvious way – giving a single value instead of 15.
Advertisement
Answer
You can specify the tests to run by using the -k
flag for filtering tests that match a string expression. When using parametrize, pytest names each test case with the following convention:
test_name[‘-‘ separated test inputs]
for example
JavaScript
1
2
1
test_name[First_test_value-Second_test_value-N_test_value]
2
Selecting an specific test to run is a matter of putting all the above together for example
JavaScript
1
2
1
pytest -k 'my_test[value_1-value_2]'
2
or
JavaScript
1
2
1
pytest -k my_test[value_1-value_2]
2
You need to escape the square brackets.