Skip to content
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

Cannot use @mark and multiple @patch class decorators #473

Closed
pytestbot opened this issue Mar 4, 2014 · 5 comments
Closed

Cannot use @mark and multiple @patch class decorators #473

pytestbot opened this issue Mar 4, 2014 · 5 comments
Labels
type: bug problem that needs to be addressed

Comments

@pytestbot
Copy link
Contributor

Originally reported by: Dan Kinder (BitBucket: dankinder, GitHub: dankinder)


Forgive me if this isn't intended to work, but I imagine it should.

This small test works:

#!python
from pytest import mark
from mock import patch

@patch('os.path')
@mark.slow
class TestSimple:
    def test_simple_thing(self, mock_path):
        pass

As well as this one:

#!python
from mock import patch

@patch('os.getcwd')
@patch('os.path')
class TestSimple:
    def test_simple_thing(self, mock_path, mock_getcwd):
        pass

But this one fails:

#!python
from pytest import mark
from mock import patch

# Note: the decorator order does not matter
@patch('os.getcwd')
@patch('os.path')
@mark.slow
class TestSimple:
    def test_simple_thing(self, mock_path, mock_getcwd):
        pass

(On python 2.6.6, pytest 2.5.2, mock 1.0.1)

The failure:

$ py.test
=========================================================== test session starts ===========================================================
platform linux2 -- Python 2.6.6 -- py-1.4.20 -- pytest-2.5.2
collected 0 items / 1 errors 

================================================================= ERRORS ==================================================================
_______________________________________________________ ERROR collecting test_a.py ________________________________________________________
../venv/lib/python2.6/site-packages/_pytest/runner.py:139: in __init__
>               self.result = func()
../venv/lib/python2.6/site-packages/_pytest/main.py:419: in _memocollect
>       return self._memoizedcall('_collected', lambda: list(self.collect()))
../venv/lib/python2.6/site-packages/_pytest/main.py:296: in _memoizedcall
>           res = function()
../venv/lib/python2.6/site-packages/_pytest/main.py:419: in <lambda>
>   return self._memoizedcall('_collected', lambda: list(self.collect()))
../venv/lib/python2.6/site-packages/_pytest/python.py:527: in collect
>       return super(Instance, self).collect()
../venv/lib/python2.6/site-packages/_pytest/python.py:321: in collect
>               res = self.makeitem(name, obj)
../venv/lib/python2.6/site-packages/_pytest/python.py:333: in makeitem
>           collector=self, name=name, obj=obj)
../venv/lib/python2.6/site-packages/_pytest/main.py:162: in call_matching_hooks
>       return hookmethod.pcall(plugins, **kwargs)
../venv/lib/python2.6/site-packages/_pytest/core.py:381: in pcall
>       return self._docall(methods, kwargs)
../venv/lib/python2.6/site-packages/_pytest/core.py:388: in _docall
>           res = mc.execute()
../venv/lib/python2.6/site-packages/_pytest/core.py:289: in execute
>           res = method(**kwargs)
../venv/lib/python2.6/site-packages/_pytest/python.py:227: in pytest_pycollect_makeitem
>               return list(collector._genfunctions(name, obj))
../venv/lib/python2.6/site-packages/_pytest/python.py:339: in _genfunctions
>       transfer_markers(funcobj, cls, module)
../venv/lib/python2.6/site-packages/_pytest/python.py:435: in transfer_markers
>                   mark(funcobj)
../venv/lib/python2.6/site-packages/_pytest/mark.py:264: in __call__
>                       setattr(func, self.name, holder)
E                       AttributeError: 'instancemethod' object has no attribute 'slow'
========================================================= 1 error in 0.07 seconds =========================================================

If this should work and is a pytest problem (not the mock library or something else), let me know and I may be able to dive in and try to patch it up.


@pytestbot
Copy link
Contributor Author

Original comment by holger krekel (BitBucket: hpk42, GitHub: hpk42):


I actually wasn't aware that you can use mock.patch on classes as well. I guess there is some interaction problem with transfering markers.

@pytestbot
Copy link
Contributor Author

Original comment by Ronny Pfannschmidt (BitBucket: RonnyPfannschmidt, GitHub: RonnyPfannschmidt):


Merged in hpk42/pytest-hpk/issue473 (pull request #152)

fix issue473: work around mock putting an unbound method into a class

1 similar comment
@pytestbot
Copy link
Contributor Author

Original comment by Ronny Pfannschmidt (BitBucket: RonnyPfannschmidt, GitHub: RonnyPfannschmidt):


Merged in hpk42/pytest-hpk/issue473 (pull request #152)

fix issue473: work around mock putting an unbound method into a class

@pytestbot
Copy link
Contributor Author

Original comment by Ronny Pfannschmidt (BitBucket: RonnyPfannschmidt, GitHub: RonnyPfannschmidt):


fix issue473: work around mock putting an unbound method into a class
dict when double-patching.

@pytestbot
Copy link
Contributor Author

Original comment by Dan Kinder (BitBucket: dankinder, GitHub: dankinder):


Awesome, thanks for the help folks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug problem that needs to be addressed
Projects
None yet
Development

No branches or pull requests

1 participant