Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clear caches after other teardowns are run instead of before #46

Open
DetachHead opened this issue Jan 23, 2025 · 1 comment
Open

clear caches after other teardowns are run instead of before #46

DetachHead opened this issue Jan 23, 2025 · 1 comment

Comments

@DetachHead
Copy link

currently the teardown for clearing the caches happens first:

@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_teardown(item, nextitem): # pylint: disable=unused-argument
"""Call cache_clear on every cache_function, after every test run."""
for function in CACHED_FUNCTIONS:
function.cache_clear()
yield

this is an issue for me because i have teardown code for my tests that rely on the caches. it would be nice if the teardown was a trylast hook instead:

@pytest.hookimpl(trylast=True)
def pytest_runtest_teardown():
    for function in CACHED_FUNCTIONS:
        function.cache_clear()

or perhaps it could run during setup, to guarantee that the cache is present during all other teardowns from previous tests:

@pytest.hookimpl(tryfirst=True)
def pytest_runtest_setup():
    for function in CACHED_FUNCTIONS:
        function.cache_clear()

(thanks for this plugin btw, it uncovered so many issues with our tests that previously went completely undetected. this should be built into pytest)

@ipwnponies
Copy link
Owner

I'm open to either implementation, as they both fulfill the spirit. I'm leaning towards pytest_runtest_setup, since it'd be closer scoped to when we care to bust caching (actual test).

Huh, I think hookwrapper=True is actually unintentional, since there's no wrapping going on. TIL

Please put up a PR, the existing unit tests should correctly test this refactor.

(thanks for this plugin btw, it uncovered so many issues with our tests that previously went completely undetected. this should be built into pytest)

Thanks for the kind words! Credit should go to my colleague for their pytest wizardry.
Once upon a time I was hurt by a stupid flaky test that I wrote (like you), complained about it while doing absolutely nothing, was gifted a solution from above, and now I repay what I can by maintaining this project 😆

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants