-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Ensure logging tests always cleanup after themselves #11531
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
Ensure logging tests always cleanup after themselves #11531
Conversation
2nd commits adds type annotations for all tests in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
testing/logging/test_fixture.py
Outdated
@@ -290,25 +294,47 @@ def test_caplog_captures_for_all_stages(caplog, logging_during_setup_and_teardow | |||
assert [x.message for x in caplog.get_records("setup")] == ["a_setup_log"] | |||
|
|||
# This reaches into private API, don't use this type of thing in real tests! | |||
assert set(caplog._item.stash[caplog_records_key]) == {"setup", "call"} | |||
# error: Invalid index type "StashKey[dict[str, list[LogRecord]]]" for "Stash"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried playing a bit with the StashKey
's TypeVar
variance to fix this, but I think it needs to be invariant because it's used in both setters and getters. Maybe the new Python 3.12 type parameter syntax fixes this, didn't check.
Anyway I think it can be worked around by doing it in two lines:
caplog_records = caplog._item.stash[caplog_records_key]
assert set(caplog_records) == {"setup", "call"}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed that fixes it, however I do not get why... could you explain it, please?
Logging has many global states, and we did foresee this by creating a ``cleanup_disabled_logging`` fixture, however one might still forget to use it and failures leak later -- sometimes not even in the same PR, because the order of the tests might change in the future, specially when running under xdist. This problem surfaced during pytest-dev#11530, where tests unrelated to the change started to fail.
29fe9c4
to
7e69ce7
Compare
Logging has many global states, and we did foresee this by creating a
cleanup_disabled_logging
fixture, however one might still forget to use it and failures leak later -- sometimes not even in the same PR, because the order of the tests might change in the future, specially when running under xdist.This problem surfaced during #11530, where tests unrelated to the change started to fail.