Skip to content

3.7.1 unittest.mock decorators won't work for pytest fixture #3774

Closed
@fabregaszy

Description

@fabregaszy

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!

Metadata

Metadata

Assignees

Labels

type: bugproblem that needs to be addressedtype: regressionindicates a problem that was introduced in a release which was working previously

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions