Unittesting with mock objects fails in tox with @patch decorator

Issue #329 on hold
Arthur Lutz created an issue

I have some unittests that work fine when directly run with python (python -R -m coverage run -m pytest <FOLDER>/test),

The code looks like this

    @patch('elasticsearch.client.Elasticsearch.index')
    def test_es_hooks_modify(self, index):

and generates the following error when run in tox

/usr/lib/python2.7/dist-packages/mock/mock.py:721: AttributeError
___________________________________________________________ ExportElasticSearchTC.test_es_hooks_modify _____________________________________________________

args = (<test_elastic_search.ExportElasticSearchTC testMethod=test_es_hooks_modify>, <MagicMock name='index' id='140487100643792'>), keywargs = {}
extra_args = [<MagicMock name='index' id='140487100643792'>], entered_patchers = [<mock.mock._patch object at 0x7fc5b6522bd0>]
exc_info = (<type 'exceptions.AttributeError'>, AttributeError('assert_called',), <traceback object at 0x7fc5b39af638>)
patching = <mock.mock._patch object at 0x7fc5b6522bd0>, arg = <MagicMock name='index' id='140487100643792'>

    @wraps(func)
    def patched(*args, **keywargs):
        extra_args = []
        entered_patchers = []

        exc_info = tuple()
        try:
            for patching in patched.patchings:
                arg = patching.__enter__()
                entered_patchers.append(patching)
                if patching.attribute_name is not None:
                    keywargs.update(arg)
                elif patching.new is DEFAULT:
                    extra_args.append(arg)

            args += tuple(extra_args)
>           return func(*args, **keywargs)

Reading the mock code, I try out adding unsafe

    @patch('elasticsearch.client.Elasticsearch.index', unsafe=True)
    def test_es_hooks_modify(self, index):

And this works in both tox and directly from python.

Is this a mock bug ? a pytest bug ? is tox doing something that might affect the way mock works ?

Do you need extra context ?

Comments (3)

  1. Log in to comment