46

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.

0

3 Answers 3

58

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.

4
  • 3
    The -k option uses substring matching so you can also pass in enough of the test 'name' to be unique. So something like pytest -k mytest[value_1] or even pytest -k value_2] would also work.
    – durden2.0
    Jan 26, 2018 at 12:13
  • 1
    The -k option should be given in "mytest[value_1]". PyTest threw error Pattern Not Found when I tried running without this. Dec 6, 2019 at 22:43
  • This 'my_test[value_1-value_2]' form seems to be missing from the docs now.
    – bravmi
    Jul 12, 2021 at 7:03
  • Moreover it doesn't work for me, while 'my_test[value_1 or value_2]' does (pytest 6.2.4). Maybe it kinda follows from -k option help but I'm not really sure.)
    – bravmi
    Jul 12, 2021 at 7:18
18

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]
1
  • Be sure to escape [ character in example: pytest test_file_name.py::test_name\[value1-value2-value3\] Jun 10, 2022 at 10:16
0

I know this question is answered, but I wasn't satisfied with the answer for my use case.

I have some parametrised tests that take longer than I'd like if I am to run them frequently. It would be useful to be able to pass a parameter to pytest on the command line to set a maximum number of runs for each parametrised test. That way I get resassurance that my code works for some data sets, without having to wait for every data set to be checked, but still preserving the code for testing every data set (to be run less frequently).

I have achieved this by adding the following to my conftest.py

def pytest_addoption(parser):
    parser.addoption(
        "--limit",
        action="store",
        default=-1,
        type=int,
        help="Maximum number of permutations of parametrised tests to run",
    )


def pytest_collection_modifyitems(session, config, items):
    def get_base_name(test_name):
        """
        Get name of test without parameters

        Parametrised tests have the [ character after the base test name, followed by
        the parameter values. This removes the [ and all that follows from test names.
        """
        try:
            return test_name[: test_name.index("[")]
        except ValueError:
            return test_name

    limit = config.getoption("--limit")
    if limit >= 0:
        tests_by_name = {item.name: item for item in items}
        test_base_names = set(get_base_name(name) for name in tests_by_name.keys())

        tests_to_run = []
        for base_name in test_base_names:
            to_skip = [t for n, t in tests_by_name.items() if base_name in n][limit:]
            for t in to_skip:
                t.add_marker("skip")


2
  • This might be improved by marking the unrun tests as skipped instead of just removing them from the collection
    – lochsh
    Oct 28, 2021 at 17:27
  • I have updated the code so that the tests are skipped instead of removed from collection
    – lochsh
    Oct 28, 2021 at 17:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.