Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

parametrize and running a single test in pytest

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.

like image 836
Avishay Cohen Avatar asked Jul 06 '17 08:07

Avishay Cohen


People also ask

How do I run a particular test in pytest?

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.

How does pytest Parametrize work?

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.

How do you run the same test multiple times in pytest?

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.

What is the syntax to run a parameterized test in pytest?

The @pytest. mark. parametrize() decorator lets you parameterize arguments of the testing function independent of fixtures you created.


2 Answers

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.

like image 59
Enrique Saez Avatar answered Sep 20 '22 13:09

Enrique Saez


I can think of two possible solutions.

  1. Use the name of the test you want to run, and execute it
  2. Use the -k parameter to run tests that match a given substring expression

Solution 1

Use 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.

Solution 2

This solution has been provided by Enrique Saez, and it basically consists of passing part of the name of the test:

pytest -k -value3] 
like image 35
lmiguelvargasf Avatar answered Sep 22 '22 13:09

lmiguelvargasf