Skip to content

Commit

Permalink
fix(unit_test): prevent logging from raising exceptions after tests
Browse files Browse the repository at this point in the history
pytest's capsys just closes the stdout when done with tests. there are
issues tracking this problem, see pytest-dev/pytest#5577 .
but we adds a handler which redirect the logging messages to stdout. so,
once pytest finishes testing, exceptions are raised when writing logging
messages, like
```
22:37:20    File "/usr/local/lib/python3.10/logging/__init__.py", line 1101, in emit
22:37:20      stream.write(msg + self.terminator)
22:37:20  ValueError: I/O operation on closed file.
```
so, in this change, `logging.raiseExceptions` is disabled when pytest's
session finishes.

Fixes #6000

Signed-off-by: Kefu Chai <kefu.chai@scylladb.com>
  • Loading branch information
tchaikov authored and fruch committed Apr 10, 2023
1 parent 590b9ef commit 0435861
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions unit_tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,14 @@ def fixture_params(request: pytest.FixtureRequest):
@pytest.fixture(scope='function', autouse=True)
def fixture_cleanup_continuous_events_registry():
ContinuousEventsRegistry().cleanup_registry()


def pytest_sessionfinish():
# pytest's capsys is enabled by default, see
# https://docs.pytest.org/en/7.1.x/how-to/capture-stdout-stderr.html.
# but pytest closes its internal stream for capturing the stdout and
# stderr, so we are not able to write the logger anymore once the test
# session finishes. see https://github.com/pytest-dev/pytest/issues/5577,
# to silence the warnings, let's just prevent logging from raising
# exceptions.
logging.raiseExceptions = False

0 comments on commit 0435861

Please sign in to comment.