fixes issue 156: monkeypatch class level descriptors

This commit is contained in:
Ronny Pfannschmidt 2012-09-25 18:15:13 +02:00
parent a18fd61a20
commit 35cbb5791d
3 changed files with 42 additions and 2 deletions

View File

@ -56,7 +56,8 @@ Changes between 2.2.4 and 2.3.0.dev
- fix issue 188: ensure sys.exc_info is clear on python2
before calling into a test
- fix issue 191: addd unittest TestCase runTest method support
- fix issue 191: add unittest TestCase runTest method support
- fix issue 156: monkeypatch correctly handles class level descriptors
- reporting refinements:

View File

@ -1,6 +1,6 @@
""" monkeypatching and mocking functionality. """
import os, sys
import os, sys, inspect
def pytest_funcarg__monkeypatch(request):
"""The returned ``monkeypatch`` funcarg provides these
@ -39,6 +39,10 @@ class monkeypatch:
oldval = getattr(obj, name, notset)
if raising and oldval is notset:
raise AttributeError("%r has no attribute %r" %(obj, name))
# avoid class descriptors like staticmethod/classmethod
if inspect.isclass(obj):
oldval = obj.__dict__.get(name, notset)
self._setattr.insert(0, (obj, name, oldval))
setattr(obj, name, value)

View File

@ -206,3 +206,38 @@ def test_issue185_time_breaks(testdir):
result.stdout.fnmatch_lines("""
*1 passed*
""")
class SampleNew(object):
@staticmethod
def hello():
return True
class SampleNewInherit(SampleNew):
pass
class SampleOld:
#oldstyle on python2
@staticmethod
def hello():
return True
class SampleOldInherit(SampleOld):
pass
@pytest.mark.parametrize('Sample', [
SampleNew, SampleNewInherit,
SampleOld, SampleOldInherit,
], ids=['new', 'new-inherit', 'old', 'old-inherit'])
def test_issue156_undo_staticmethod(Sample):
monkeypatch = MonkeyPatch()
monkeypatch.setattr(Sample, 'hello', None)
assert Sample.hello is None
monkeypatch.undo()
assert Sample.hello()