Skip to content

Setup times of long-running fixtures are attributed to first test #8741

Description

@sondrelg

Description

The duration reporting in CallInfos (and consequently in TestReports) does not seem to work as expected.

From what I can tell, the setup times for fixtures that run before the test suite starts are attributed to the first test in the test suite. This means that if you have a long-running setup in a fixture, your first test is incorrectly attributed a longer duration just because it's the first test.

My actual issue stems from the setup-database fixture in pytest-django, which runs for 4-5 minutes before tests start. This setup time is attributed to my first test, which in turn prevents pytest-split from splitting my test suite evenly (based on stored durations).

If you point me in the right direction I'd be happy to contribute to resolving this 🙂

Reproducible example

This code sample

def test_something(testdir):
    testdir.makepyfile(
        """
        import pytest
        import time

        @pytest.fixture(autouse=True, scope="session")
        def slow_start():
            time.sleep(5)

        def test_something():
            time.sleep(1)

        def test_something_else():
            time.sleep(1)
        """
    )
    testdir.inline_run("--durations=6", "-vvv")

Outputs

============================= slowest 6 durations ==============================
5.01s setup    test_something.py::test_something
1.01s call     test_something.py::test_something
1.00s call     test_something.py::test_something_else
0.00s teardown test_something.py::test_something_else
0.00s teardown test_something.py::test_something
0.00s setup    test_something.py::test_something_else
============================== 2 passed in 7.03s ===============================

Environment

Don't think it's relevant here, but just in case: I'm using Pytest 6.2.4, python 3.9, with macos

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: fixturesanything involving fixtures directly or indirectlytype: enhancementnew feature or API change, should be merged into features branch

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions