Mayil-AI-Sandbox / loguru-Jan2023

MIT License
0 stars 0 forks source link

Question: How to remove duplication of loguru logger patching in pytest (hashtag1153) #157

Open vikramsubramanian opened 2 weeks ago

vikramsubramanian commented 2 weeks ago

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.

PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'path/to/tmp_dir/file.log'

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 to logger.add, just need patch it so the test runs.


def test_one(mock_logger_add):
    ...
    hashtag Useful to check, but don't want to call this in EVERY test
    mock_logger_add.assert_called_once()

def test_two(mock_logger_add):
    ...
    hashtag No need to check mock_logger_add, just want my code to run

How can I reduce this duplication?

Things I've tried:

e.g.


def patch_logger_add(monkeypatch):
    monkeypatch.setattr("loguru.logger.add", lambda *args, **kwargs: None)
    hashtag or
    hashtag monkeypatch.setattr("loguru.logger.add", MagicMock())

or


def no_logger_add(monkeypatch):
    monkeypatch.delattr("loguru.logger.add")

These don't work. Perhaps because, in order for loguru to work with pytest, we have to [redefine caplog]( and that involves calling logger.add.

This works but only when I do not pass caplog to my tests.


def patch_logger():
    with patch("loguru.logger.add"):
        yield

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. )

mayil-ai[bot] commented 2 weeks ago

Some past issues to consider:

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:

  1. 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
  2. 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)
  3. 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!