classification
Title: assertLogs to optionally not disable existing handlers
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.11
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: dandiez, vinay.sajip
Priority: normal Keywords:

Created on 2021-11-06 12:08 by dandiez, last changed 2022-01-14 19:02 by iritkatriel.

Messages (1)
msg405858 - (view) Author: Daniel (dandiez) * Date: 2021-11-06 12:08
At the moment, assertLogs removes the handlers attached to the logger.
In general this is good, because it reduces message spamming in the test logs.
However, if the code being tested is relying on a handler to do something, then the test fails because the handler is being removed.
This leads to the situation that the same exact test must be run twice:
- first time within the context manager, to assert that specific messages were logged (using `with self.assertLogs()`)
- second time, without the assertLogs to ensure the code that uses a handler does the right thing

The proposal is to have `self.assertLogs()` accept a key word argument such as `keep_handlers=False`, which can be set to True, whenever the handlers should be preserved.
It would probably be also useful to add a note in the documentation that makes users aware that the existing handlers will be removed.
History
Date User Action Args
2022-01-14 19:02:39iritkatrielsetversions: - Python 3.7, Python 3.8, Python 3.9, Python 3.10
2021-11-07 03:51:55ned.deilysetnosy: + vinay.sajip
2021-11-06 13:34:41dandiezsettype: enhancement
2021-11-06 13:02:50dandiezsetcomponents: + Library (Lib)
versions: + Python 3.11
2021-11-06 12:08:52dandiezcreate