Skip to content Skip to sidebar Skip to footer

Limit The Number Of Test Cases To Be Executed In Pytest

A little background I am executing my test cases with Jenkins, I am doing a little POC with Jenkins right now. And, in my case, there are 500+ test cases which takes an hour to ex

Solution 1:

You can limit the amount of tests in many ways. For example, you can execute a single test by passing its full name as parameter:

$ pytest tests/test_spam.py::TestEggs::test_bacon

will run only the test method test_bacon in class TestEggs in module tests/test_spam.py.

If you don't know the exact test name, you can find it out by executing

$ pytest --collect-only -q

You can combine both commands to execute a limited amount of tests:

$ pytest -q --collect-only 2>&1 | head -n N | xargs pytest -sv

will execute first N collected tests.

You can also implement the --limit argument yourself if you want to. Example:

defpytest_addoption(parser):
    parser.addoption('--limit', action='store', default=-1, type=int, help='tests limit')


defpytest_collection_modifyitems(session, config, items):
    limit = config.getoption('--limit')
    if limit >= 0:
        items[:] = items[:limit]

Now the above command becomes equal to

$ pytest --limit N

Post a Comment for "Limit The Number Of Test Cases To Be Executed In Pytest"