Skip to content

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

Merged
merged 2 commits into from
Oct 23, 2023

Conversation

nicoddemus
Copy link
Member

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.

@nicoddemus nicoddemus added the needs backport applied to PRs, indicates that it should be ported to the current bug-fix branch label Oct 21, 2023
@nicoddemus
Copy link
Member Author

2nd commits adds type annotations for all tests in logging/test_fixture.py.

Copy link
Contributor

@BenjaminSchubert BenjaminSchubert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@@ -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";
Copy link
Member

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"}

Copy link
Member Author

@nicoddemus nicoddemus Oct 23, 2023

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.
@nicoddemus nicoddemus force-pushed the ensure-logging-cleanup branch from 29fe9c4 to 7e69ce7 Compare October 23, 2023 13:47
@nicoddemus nicoddemus merged commit bcd9664 into pytest-dev:main Oct 23, 2023
@nicoddemus nicoddemus added backport 7.4.x and removed needs backport applied to PRs, indicates that it should be ported to the current bug-fix branch labels Oct 23, 2023
@nicoddemus nicoddemus deleted the ensure-logging-cleanup branch October 23, 2023 13:55
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

Successfully merging this pull request may close these issues.

3 participants