Fenixs
Fenixs

Reputation: 353

Group test execution by class in pytest

I have the testcases structured in the following way.

app
  test_login.py
    class1
      test_11
      test_12
  test_function1.py
    class2
      test_21
      test_22

When I run "pytest.exe app", pytest is able to identify all the testcases, but it executes in random order. For example, test11, test22, test12 and so on

Is there any way I can change this and execute all testcases in a file::class first and then move on to another file::class?

Upvotes: 1

Views: 1172

Answers (2)

Fenixs
Fenixs

Reputation: 353

Following code block solved my issue. Thanks @hoefling for your time and help.

# conftest.py
@pytest.hookimpl(hookwrapper=True)
def pytest_collection_modifyitems(items):
    yield
    items.sort(key=operator.attrgetter('cls.__name__'))

Upvotes: 0

hoefling
hoefling

Reputation: 66171

it executes in random order

By default, tests are sorted by modules; inside the modules, tests are executed in the order they are specified. So you should get the rough order like this:

$ pytest --collect-only -q
test_function1.py::class2::test_21
test_function1.py::class2::test_22
test_login.py::class1::test_11
test_login.py::class1::test_12

Is there any way I can change this and execute all testcases in a file::class first and then move on to another file::class?

If you want to change the default execution order, you can do it in the pytest_collection_modifyitems hook. Example that reorders the collected tests by class name, then by test name:

# conftest.py

import operator

def pytest_collection_modifyitems(items):
    items.sort(key=operator.attrgetter('cls.__name__', 'name'))

Now tests in test_login will be executed before those in test_function1 because the module names are not counted in the ordering anymore:

$ pytest --collect-only -q
test_login.py::class1::test_11
test_login.py::class1::test_12
test_function1.py::class2::test_21
test_function1.py::class2::test_22

Upvotes: 3

Related Questions