8

I 'm writing test cases in the following manner.

# content of test_class.py
class TestClass(object):
    def test_one(self):
        x = "this"
        assert 'h' in x

    def test_two(self):
        x = "hello"
        assert hasattr(x, 'check')

test_two depends on test_one, so ordering of the execution is important, What's the convention to enforce test execution order when you group tests in a class?

eugene
  • 39,839
  • 68
  • 255
  • 489

2 Answers2

6

By default the tests will be executed in the order they are defined in the class/module. In your case:

test_class.py::TestClass::test_one PASSED
test_class.py::TestClass::test_two PASSED

Consider that in general it's a bad practise writing tests that are dependent on each other. If later tests are run in parallel, you will have flakiness, or if you install a plugin for random test execution e.g. https://pypi.python.org/pypi/pytest-randomly, or if you leave the project and someone else will have to debug tests that would start failing out of the blue.

I'd recommend combining two tests into one. All that matters is you have some test scenario. Does it matter if you have 2 tests or 1 if you still have same confidence in your code?

Dmitry Tokarev
  • 1,851
  • 15
  • 29
  • "Does it matter if you have 2 tests or 1 if you still have same confidence in your code?" - this is correct, but sometimes it's easier to split tests, just for easier output in case of errors. For example, in one test I test initialisation of my complex module, in another I test that it actually works. Then I would first check that all my modules are initialised properly, and if they are, I would check more details. – Yaroslav Nikitenko Aug 28 '23 at 09:53
  • 1
    @YaroslavNikitenko it was just a suggestion. Use abstraction in a way that is meaningful to you and your organization. Functions, classes - this is all more or less a personal choice of convenience and sometimes taste. – Dmitry Tokarev Aug 29 '23 at 06:05
3

You can use pytest_collection_modifyitems hook to change the order as you wish.

Den
  • 51
  • 2
  • A full example is available [here](https://stackoverflow.com/questions/70738211/run-pytest-classes-in-custom-order/70758938#70758938) – swimmer Jan 18 '22 at 17:10