If you cannot remove the test dependencies (which is always the best solution), the easiest way to do this is probably to just call the tests on the command line in the order you need, e.g. in your example something like:
python -m pytest test2.py test1.py test3.py
If that is not an option, you can use test markers with pytest-order
, as mentioned. In case you want to order only the test modules, but not the tests inside the modules, you can add a mark to the first test in each module, and use the --order-group-scope="module"
argument in the pytest call. Given you have the test modules as given in the question, you would have to do something like:
test_2.py
@pytest.mark.order(0)
def test_something():
...
def test_something_else():
...
... # more tests
test_1.py
class TestSomething:
@pytest.mark.order(1)
def test_1(self):
...
... # more tests
test_3.py
@pytest.mark.order(2)
def test_something():
...
... # more tests
and then run pytest using for example:
python -m pytest -vv --order-group-scope="module"
This will first order the tests inside the modules (e.g. in this case do nothing), and than sort the modules themselves based on the markers.
Disclaimer:
I'm the maintainer of pytest-order.