pytest-dev / pytest-dev/pytest

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

Open
#8,741 16 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

topic: fixtures type: enhancement
Dominant language
Python
Stars
14.5k
Forks
3.4k
Avg merge
2d 9h
Merged PRs (30d)
35

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the CallInfo and TestReport duration reporting, then run the test_something reproduction using testdir.inline_run and the session-scoped slow_start fixture. Trace how setup, call, and teardown durations are assigned to tests. Done means setup-database-style startup time is not attributed to the first test while the reported phase durations remain correct.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.