GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — pytest can report negative duration due to usage of `time.time()` · Issue #13384 · pytest-dev/pytest

pytest can report negative duration due to usage of time.time() #13384

New issue
Copy link
New issue
Copy link
Closed
Bug
#13394
Closed
pytest can report negative duration due to usage of time.time()#13384
Bug
#13394
Copy link
Labels
good first issueeasy issue that is friendly to new contributorneeds backportapplied to PRs, indicates that it should be ported to the current bug-fix branchtopic: reportingrelated to terminal output and user-facing messages and errorstype: bugproblem that needs to be addressedtype: performanceperformance or memory problem/improvementtype: proposalproposal for a new feature, often to gather opinions or design the API around the new featuretype: refactoringinternal improvements to the code

Description

@asottile
asottile
opened on Apr 19, 2025
Issue body actions

cutting most of the details here -- I'm using pytest 8.3.5 and just got this surprising result:

$ pytest tests/check_merge_conflict_test.py 
============================= test session starts ==============================
platform linux -- Python 3.12.3, pytest-8.3.5, pluggy-1.5.0
rootdir: /home/asottile/workspace/pre-commit-hooks
collected 12 items                                                             

tests/check_merge_conflict_test.py ............                          [100%]

============================= 12 passed in -0.94s ==============================

my tests are pretty fast but definitely not time-travelling fast.

it seems the root cause is usage of time.time() which is subject to smear seconds, etc. and should instead be using time.monotonic() or perf_counter()

seems there's a bunch of places we should adjust:

$ git grep '\btime\(\)' -- '*.py'
src/_pytest/junitxml.py:        self.suite_start_time = timing.time()
src/_pytest/junitxml.py:            suite_stop_time = timing.time()
src/_pytest/pytester.py:        now = timing.time()
src/_pytest/pytester.py:            reprec.ret, out.splitlines(), err.splitlines(), timing.time() - now
src/_pytest/pytester.py:            now = timing.time()
src/_pytest/pytester.py:        return RunResult(ret, out, err, timing.time() - now)
src/_pytest/runner.py:        start = timing.time()
src/_pytest/runner.py:        stop = timing.time()
src/_pytest/terminal.py:                self._collect_report_last_write = timing.time()
src/_pytest/terminal.py:            t = timing.time()
src/_pytest/terminal.py:        self._sessionstarttime = timing.time()
src/_pytest/terminal.py:        session_duration = timing.time() - self._sessionstarttime
src/_pytest/timing.py:    numbers and obtain accurate time() calls at the end, making tests reliable and instant."""
testing/_py/test_local.py:            mtime = int(time.time()) - 100
testing/_py/test_local.py:        now = time.time()
testing/_py/test_local.py:        duration = time.time() - now
testing/conftest.py:    numbers and obtain accurate time() calls at the end, making tests reliable and instant.
testing/test_pytester.py:    start = time.time()
testing/test_pytester.py:    end = time.time()

Activity

  1. webknjaz
    added the
    Bug
    issue type on Apr 19, 2025
  2. webknjaz
    added
    type: bugproblem that needs to be addressed
    type: proposalproposal for a new feature, often to gather opinions or design the API around the new feature
    topic: reportingrelated to terminal output and user-facing messages and errors
    good first issueeasy issue that is friendly to new contributor
    type: performanceperformance or memory problem/improvement
    type: refactoringinternal improvements to the code
    needs backportapplied to PRs, indicates that it should be ported to the current bug-fix branch
    on Apr 19, 2025
  3. nicoddemus commented on Apr 19, 2025

    @nicoddemus
    nicoddemus
    on Apr 19, 2025
    Member
    More actions

    Thanks @asottile

    Indeed seems safe to replace them with time.perf_counter.

  4. deyshift commented on Apr 22, 2025

    @deyshift
    deyshift
    on Apr 22, 2025
    Contributor
    More actions

    I would be interested in taking this on if it is unclaimed!

  5. RonnyPfannschmidt commented on Apr 22, 2025

    @RonnyPfannschmidt
    RonnyPfannschmidt
    on Apr 22, 2025
    Member
    More actions

    We need to evaluate if we should use this as chance to make more use o _pytest.timing

  6. nicoddemus commented on Apr 22, 2025

    @nicoddemus
    nicoddemus
    on Apr 22, 2025
    Member
    More actions

    @rivtechprojects please go ahead!

    We need to evaluate if we should use this as chance to make more use o _pytest.timing

    Good point, we should replace those usages with _pytest.timing.perf_counter. 👍

    Reacted by Deysha Rivera
  7. deyshift commented on Apr 23, 2025

    @deyshift
    deyshift
    on Apr 23, 2025
    Contributor
    More actions

    @rivtechprojects please go ahead!

    Thanks, will update!

  8. deyshift
    added a commit that references this issue on Apr 25, 2025

    Add changelog entry for issue pytest-dev#13384

    a61de97
  9. deyshift commented on Apr 25, 2025

    @deyshift
    deyshift
    on Apr 25, 2025
    Contributor
    More actions

    The pull request (#13394) is now up to address this issue. It replaces time.time() with _pytest.timing.perf_counter() for more reliable timing. Let me know if there’s any feedback or changes needed!

  10. nicoddemus
    closed this as completedin #13394on Apr 27, 2025
  11. nicoddemus
    closed this as completedin 8173aa8on Apr 27, 2025
  12. nicoddemus
    added a commit that references this issue on Apr 27, 2025

    Replace explicit time() calls with _pytest.timing (pytest-dev#13394)

    This issue will close once commit 8173aa8 is merged into the 'main' branch.
    8173aa8
  13. luketainton
    added a commit that references this issue on Jan 5, 2026

    chore(deps): update dependency pytest to v9 (#305)

    4b75154
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    good first issueeasy issue that is friendly to new contributorneeds backportapplied to PRs, indicates that it should be ported to the current bug-fix branchtopic: reportingrelated to terminal output and user-facing messages and errorstype: bugproblem that needs to be addressedtype: performanceperformance or memory problem/improvementtype: proposalproposal for a new feature, often to gather opinions or design the API around the new featuretype: refactoringinternal improvements to the code

    Type

    Bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.