Skip to content

Surprising test run order when test function reused #5163

@samueljsb

Description

@samueljsb

issue

When a class is composed form another class, the tests do not run in the order they are defined.

e.g:

import pytest


class TestFoo:
    def test_foo(self, request):
        pass

    def test_bar(self, request):
        pass


class TestBar:
    def test_foo(self, request):
        pass

    test_bar = TestFoo.test_bar
$ pytest --verbose
…
test_test.py::TestFoo::test_foo PASSED
test_test.py::TestFoo::test_bar PASSED
test_test.py::TestBar::test_bar PASSED
test_test.py::TestBar::test_foo PASSED
…

I would expect the last 2 tests to be executed in the opposite order (i.e. TestBar::test_foo then TestBar::test_bar) since this is the order they appear in the file. I'm not sure this is a bug per se, but it's something I'd like to see documented if it's decided it should stay this way.

venv

$ pip list
Package        Version
-------------- -------
appdirs        1.4.3
atomicwrites   1.3.0
attrs          19.1.0
black          19.3b0
Click          7.0
more-itertools 7.0.0
pip            19.1
pluggy         0.9.0
py             1.8.0
pytest         4.4.1
setuptools     40.6.2
six            1.12.0
toml           0.10.0
platform darwin -- Python 3.7.2, pytest-4.4.1, py-1.8.0, pluggy-0.9.0

MacOS 10.14.4 (Mojave)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions