How can I run a single test out of a set configured with parametrize? Let's say I have the following test method:
@pytest.mark.parametrize(PARAMETERS_LIST, PARAMETERS_VALUES) def test_my_feature(self, param1, param2, param3): """ test doc """ if param1 == 'value': assert True else: print 'not value' assert False
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.
To execute the test methods having a particular string within its name, we need to execute the command, pytest -k <substring> -v. Here -k <substring> is the substring to look for in the test methods and v means verbose. For our case, the command should be pytest -k Calculate –v.
pytest will build a string that is the test ID for each set of values in a parametrized test. These IDs can be used with -k to select specific cases to run, and they will also identify the specific case when one is failing. Running pytest with --collect-only will show the generated IDs.
Repeating a test Each test collected by pytest will be run count times. If you want to override default tests executions order, you can use --repeat-scope command line option with one of the next values: session , module , class or function (default). It behaves like a scope of the pytest fixture.
The @pytest. mark. parametrize() decorator lets you parameterize arguments of the testing function independent of fixtures you created.
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
test_name[First_test_value-Second_test_value-N_test_value]
Selecting an specific test to run is a matter of putting all the above together for example
pytest -k 'my_test[value_1-value_2]'
or
pytest -k my_test\[value_1-value_2\]
You need to escape the square brackets.
I can think of two possible solutions.
-k
parameter to run tests that match a given substring expressionUse the following command to see the name of the tests without running them:
pytest --collect-only -q # use --co if pytest 5.3.0+ instead of --collect-only
Use the name of the test you want to run, let's say the test is called test_file_name.py::test_name[value1-value2-value3]
, so use the following command to run it:
pytest test_file_name.py::test_name[value1-value2-value3]
Note: Be sure to use quotes if there are spaces in the identifier.
This solution has been provided by Enrique Saez, and it basically consists of passing part of the name of the test:
pytest -k -value3]
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