-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pytest 3.3: cannot mock root logger #2974
Comments
Thanks @CaselIT I will take a look later when I have more time, but just wanted to mention that you can disable the builtin logging plugin by passing |
Thanks @nicoddemus for the quick answer. Indeed |
* Workaround for py.test logging plugin bug in version 3.3: pytest-dev/pytest#2974 Change-Id: I2bdb8dacaaefa58c26d4c1c7cb7cbfc9e7f78fe6
I can no longer reproduce the original bug with pytest > 3.4.0, Thanks for fixing it! The logging module continues to use the mocked value though. This: import pytest
from unittest.mock import MagicMock
@pytest.fixture
def setup(monkeypatch):
mock = MagicMock()
monkeypatch.setattr('logging.getLogger', lambda: mock)
return mock
def test_x(setup):
assert setup.mock_calls == [] fails since the mock has the calls: Resetting the mock with Let me know if you prefer if I open a new issue for this |
Thanks @CaselIT for the feedback, I'm closing this one then.
Yeah by the time your fixture executes, It is not possible to use a fixture to monkeypatch |
Thanks for the clarification. |
It is not really related to |
Something like:
And maybe a reference to this issue |
Unfortunately this issue is not isolated to It is probably sufficient to have this discussion here so others can find. But really, we do appreciate the willingness to contribute a patch! 👍 |
I was not thinking about them. I agree that this is a bit of an edge case. Out of curiosity, premising that I'm not familiar with the internasl of pytest, wouldn't it worth considering making the logging module cache the root logger at the startup of pytest so it's not affected by patching and other modifications? And/or replace it with a child of the original root logger? |
Good question, but we probably don't want to make any logging calls until the plugin is actually "activated" (users can disable the plugin by passing |
Yes, of course, it should take into consideration the flag |
BTW, you can still patch
(Oh, and I'm using |
Pytest 3.3.0 uses the root logger in each test so it can no longer be mocked with
monkeypatch
raires
changing the
lambda
tolambda _: mock
raisesMoving the
monkeypatch
call in the function does not help since the second error is always raised.The error is raised in
<python-path>\lib\site-packages\_pytest\logging.py
on line 102 or 110Using the
--no-print-logs
flag does not change the outputPytest 3.2.5 does not have this problem
Also if there are subsequent tests after the one that caused the previous error, pytest crashes completely: appending
after
test_x
in the example above results in the following error when running pytest without filtersSelecting only
test_x
does not raises theAttributeError
I'm on windows 10 with python 3.6.3, but I've confirmed the same bug on linux under python 3.5 and 3.6
The text was updated successfully, but these errors were encountered: