user3586164
user3586164

Reputation: 195

When pytest run all tests in a directory, how can it decide which test to run last?

I am calling all tests under a directory using pytest. How can I run one-specific test case last?

python -m pytest ../testdir  
../testdir/test_1.py.... test_n.py

Upvotes: 7

Views: 15476

Answers (1)

hoefling
hoefling

Reputation: 66251

You can easily change the default test execution order by implementing your own pytest_collection_modifyitems hook. Create a file conftest.py with the following contents:

def pytest_collection_modifyitems(items):
    test_name = 'test_1'
    test_index = next((i for i, item in enumerate(items) if item.name == test_name), -1)
    test_item = items.pop(test_index)
    items.append(test_item)

In this example, if a test function named test_1 is collected, it will be shifted to the end of the items list. Replace test_1 with your own function name, or even make it configurable via command line arguments.

Upvotes: 5

Related Questions