You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
3.7.1 unittest.mock decorators won't work for pytest fixture #3774
Hi, some of my unittests failed after I upgrade to pytest 3.7.1. I checked them and found it has something to do with using mock decorator with pytest fixtures.
Example:
import unittest.mock as mock
import pytest
config = {
'mykey': '1'
}
@pytest.fixture(scope='function')
@mock.patch.dict(config, {'mykey': 0})
def my_fixture():
print("value of mykey now: {}".format(config['mykey']))
def test_foobar(my_fixture):
print('bla bla bla')
With pytest 3.6.3, it works fine and output is like:
============================= test session starts ==============================
platform darwin -- Python 3.5.0, pytest-3.6.3, py-1.5.4, pluggy-0.6.0
rootdir: /Users/zhangyan/foobar/tests, inifile:
plugins: falcon-0.4.0collected 1 item
test_fixture.py value of mykey now: 0
.bla bla bla
Can see that the value of mykey has been mocked.
But with pytest 3.7.1, the output is:
============================= test session starts ==============================
platform darwin -- Python 3.5.0, pytest-3.7.1, py-1.5.4, pluggy-0.7.1
rootdir: /Users/zhangyan/foobar/tests, inifile:
plugins: falcon-0.4.0collected 1 item
test_fixture.py value of mykey now: 1
.bla bla bla
Can see from the output that, the mock didn't make any effect.
Please correct me if my usage is incorrect, thanks!
Any chance of a point release soon? This is also breaking pytest-trio's test suite (which tries to combine @pytest.fixture with @async_generator): python-trio/pytest-trio#50 (comment)
(And I've confirmed that using pytest master does resolve the issue we're seeing with 3.7.1.)
Activity
RonnyPfannschmidt commentedon Aug 3, 2018
@fabregaszy this is a regression, i believe it relates to #3754
RonnyPfannschmidt commentedon Aug 3, 2018
my suspicion is that getting the real function at that place is just unwrapping the mock decorator as well
a suggestion for a quick fix is to use monkeypatch in the fixture
nicoddemus commentedon Aug 3, 2018
Yes I think that's the problem,
get_real_funcunwraps everything until it gets to the original function. I will take a look ASAP.Fix issue where fixtures would lose the decorated functionality
Add CHANGELOG for issue pytest-dev#3774, missing from PR pytest-dev#3780
Merge pull request #3795 from nicoddemus/changelog-3774
njsmith commentedon Aug 15, 2018
Any chance of a point release soon? This is also breaking pytest-trio's test suite (which tries to combine
@pytest.fixturewith@async_generator): python-trio/pytest-trio#50 (comment)(And I've confirmed that using pytest master does resolve the issue we're seeing with 3.7.1.)
nicoddemus commentedon Aug 16, 2018
Hi @njsmith,
Yeah we already have quite a number of bug fixes on
master, so I was planning to start a release later today. 👍Update pytest to 3.7.2 (#1755)
Update pytest to 3.7.2 (#25)
Update pytest to 3.7.2 (#8)
Merge #28
Scheduled monthly dependency update for October (#389)