Open vikramsubramanian opened 2 weeks ago
To address the issue of avoiding repetitive patching of loguru.logger.add
in each pytest test and preventing PermissionError
due to file access conflicts on Windows, follow these actionable steps:
Create an autouse=True
Fixture in conftest.py
:
This fixture will automatically patch loguru.logger.add
for all tests, eliminating the need to patch it in each individual test.
import pytest
from unittest.mock import patch
@pytest.fixture(autouse=True)
def patch_logger_add():
with patch("loguru.logger.add", lambda *args, **kwargs: None):
yield
Ensure Compatibility with caplog
:
To capture logs correctly using caplog
, ensure that enqueue=True
is used when adding the log handler.
import pytest
from loguru import logger
@pytest.fixture
def caplog(_caplog):
handler_id = logger.add(_caplog.handler, format="{message}", enqueue=True)
yield _caplog
logger.remove(handler_id)
Use the Fixtures in Your Tests:
With the patch_logger_add
fixture set to autouse=True
, it will automatically apply to all tests. You can now write your tests without worrying about patching loguru.logger.add
in each one.
def test_one(caplog):
# Your test code here
pass
def test_two(caplog):
# Your test code here
pass
By following these steps, you can reduce the duplication of patching loguru.logger.add
in your tests and ensure smooth pytest execution without file access conflicts.
💡 To rerun Mayil, comment mayil-ai rerun
. Mayil will incorporate any new context added to the ticket. Include details in your rerun comment to guide Mayil!
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. )