Closed codeananda closed 2 days ago
Hey @codeananda.
Could you create a fixture that automatically removes the handlers possible added by your test? Basically, what would happen if you make sure logger.remove()
is called at the end of your test?
Hi @Delgan finally figured it out.
The key is to add caplog
to patch_logger_add
so that the patch is applied after caplog
is created and teardown happens before caplog
teardown.
@pytest.fixture(autouse=True)
def patch_logger_add(caplog):
"""Patch logger.add to ensure no files are written to disk.
Note: we pass caplog to ensure the patch is applied after caplog setup and removed before
caplog teardown. Otherwise, there are teardown issues when trying to call logger.remove.
"""
with patch("my_module.logger.add"):
yield
I am testing a function which calls
loguru.logger.add("file.log")
at the start. This causes issues during pytest execution. The file is written to a temp dir and thus is being used by another process (good ol' Windows) when clean-up happens.One solution is to patch
loguru.logger.add
on each test. But this results in much repeated (boilerplate?) code. For many tests, I don't need to refer tologger.add
, just need patch it so the test runs.How can I reduce this duplication?
Things I've tried:
autouse=True
fixture toconftest.py
or in the test file/classe.g.
or
These don't work. Perhaps because, in order for loguru to work with pytest, we have to redefine
caplog
and that involves callinglogger.add
.This works but only when I do not pass
caplog
to my tests.Note: I do not want to turn loguru off completely because I check the logs for errors in my tests.
I posted this also on SO but didn't get much response so wondered if I'd get better results here.