From 158f251ac89b5e37c7267390caecf37c8797f0e0 Mon Sep 17 00:00:00 2001 From: Stefaan Lippens Date: Wed, 8 Mar 2023 18:07:19 +0100 Subject: [PATCH] Quick fix for teardown issue with caplog and custom formatter see https://github.com/pytest-dev/pytest/issues/2987#issuecomment-1460509126 --- tests/test_jobregistry.py | 4 ++-- tests/util/test_logging.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/test_jobregistry.py b/tests/test_jobregistry.py index ce5564ef..616e592f 100644 --- a/tests/test_jobregistry.py +++ b/tests/test_jobregistry.py @@ -545,7 +545,7 @@ def test_just_log_errors(self, caplog): ) ] - def test_job_id_logging(self, requests_mock, oidc_mock, ejr, caplog): + def test_job_id_logging(self, requests_mock, oidc_mock, ejr, caplog, monkeypatch): """Check that job_id logging is passed through as logging extra in appropriate places""" caplog.set_level(logging.DEBUG) @@ -554,7 +554,7 @@ def format(self, record: logging.LogRecord): job_id = getattr(record, "job_id", None) return f"{record.name}:{job_id}:{record.message}" - caplog.handler.setFormatter(Formatter()) + monkeypatch.setattr(caplog.handler, "formatter", Formatter()) job_id = "j-123" diff --git a/tests/util/test_logging.py b/tests/util/test_logging.py index e582bb1c..2957ae6c 100644 --- a/tests/util/test_logging.py +++ b/tests/util/test_logging.py @@ -346,13 +346,13 @@ def test_just_log_exceptions_invalid_logger(caplog): assert caplog.record_tuples == [expected] -def test_just_log_exceptions_extra(caplog): +def test_just_log_exceptions_extra(caplog, monkeypatch): class Formatter: def format(self, record: logging.LogRecord): foo = getattr(record, "foo", None) return f"[Foo:{foo}] {record.levelname} {record.message}" - caplog.handler.setFormatter(Formatter()) + monkeypatch.setattr(caplog.handler, "formatter", Formatter()) with just_log_exceptions(extra={"foo": "bar"}): raise RuntimeError("Nope")