Skip to content

repeating a test spams pytest cache #84

Open

Description

@dpinol

When running many repeatitions of a test with --count, .pytest_cache/v/cache/nodeids may get up to millions of entries. Eg:

"test/utils/test_types.py::test_if_none[1-100]",
  "test/utils/test_types.py::test_if_none[10-100]",
  "test/utils/test_types.py::test_if_none[100-100]",
  "test/utils/test_types.py::test_if_none[11-100]",
  "test/utils/test_types.py::test_if_none[12-100]",
...

This may cause a lag of several seconds even after executing a single method with -k specifying a single test file.

The culprit is pytest_sessionfinish in cacheprovider.py, when calling config.cache.set("cache/nodeids", sorted(self.cached_nodeids)). When cached_nodeids contains milions of entries, this function always generates a json with the whole contents of .pytest_cache/v/cache/nodeids.

Activity

  1. RonnyPfannschmidt commented on Apr 14, 2024

    @RonnyPfannschmidt
    Member

    Please elaborate on the actual numbers

  2. dpinol commented on Apr 14, 2024

    @dpinol
    Author

    The length of ".pytest_cache/v/cache/nodeids" depends on the usage. I guess I executed at some time with millions of executions and got the file with so many lines. In that case, on my intel i9 a trivial test took 4s, while after removing the file the whole execution now takes 0.3s

  3. dpinol commented on Apr 14, 2024

    @dpinol
    Author

    I tried to reproduce it. I executed 1M times a file with 2 tests. It created a cache file of 143M with 2M entries.
    Executing the tests a single time passed from 0.4s to 2.4s

  4. RonnyPfannschmidt commented on Apr 14, 2024

    @RonnyPfannschmidt
    Member

    as repeat uses parameterize - i suspect a breaking change is needed to make this more usable

  5. okken commented on Apr 30, 2024

    @okken
    Contributor

    You can disable caching entirely while running large counts with -p no:cacheprovider.
    Would that work?

  6. okken commented on Feb 16, 2025

    @okken
    Contributor

    Recommend closing this issue, as there's not much this plugin can do about it.

  7. dpinol commented on Feb 17, 2025

    @dpinol
    Author

    Sorry for the late answer. -p no:cacheprovider works like a charm, thanks!

    I guess users would appreciate a note in the documentation about this issue, probably mentioning the -p no:cacheprovider trick. I added it to my pytest wrapper script when it detects --count since I guess the cache make not much sense when repeating a tests.

  8. dmakhno commented on Mar 26, 2025

    @dmakhno

    Is it related to when pytest_generate_tests is executed?
    Lmk if my case is another one, and I'll create separate issue.

    I have a suite, let's say with 10000 tests.
    And I have -k or -m which selects among them only 10.
    And e.g. I'm using --count 10
    What I'd expected:
    "Run 100, Deselected 9990"
    What I have:
    "Run 100, Deselected 99900" - tests were multiplied before filtering

    Is there way to postpone generate hook or "multiplication" call later somehow?

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions