New issue 329: Unittesting with mock objects fails in tox with @patch decorator
https://bitbucket.org/hpk42/tox/issues/329/unittesting-with-mock-objects-fails-in-tox

Arthur Lutz:

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 ? 



_______________________________________________
pytest-commit mailing list
pytest-commit@python.org
https://mail.python.org/mailman/listinfo/pytest-commit

Reply via email to