fixes issue 156: monkeypatch class level descriptors
This commit is contained in:
parent
a18fd61a20
commit
35cbb5791d
|
@ -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
|
- fix issue 188: ensure sys.exc_info is clear on python2
|
||||||
before calling into a test
|
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:
|
- reporting refinements:
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
""" monkeypatching and mocking functionality. """
|
""" monkeypatching and mocking functionality. """
|
||||||
|
|
||||||
import os, sys
|
import os, sys, inspect
|
||||||
|
|
||||||
def pytest_funcarg__monkeypatch(request):
|
def pytest_funcarg__monkeypatch(request):
|
||||||
"""The returned ``monkeypatch`` funcarg provides these
|
"""The returned ``monkeypatch`` funcarg provides these
|
||||||
|
@ -39,6 +39,10 @@ class monkeypatch:
|
||||||
oldval = getattr(obj, name, notset)
|
oldval = getattr(obj, name, notset)
|
||||||
if raising and oldval is notset:
|
if raising and oldval is notset:
|
||||||
raise AttributeError("%r has no attribute %r" %(obj, name))
|
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))
|
self._setattr.insert(0, (obj, name, oldval))
|
||||||
setattr(obj, name, value)
|
setattr(obj, name, value)
|
||||||
|
|
||||||
|
|
|
@ -206,3 +206,38 @@ def test_issue185_time_breaks(testdir):
|
||||||
result.stdout.fnmatch_lines("""
|
result.stdout.fnmatch_lines("""
|
||||||
*1 passed*
|
*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()
|
||||||
|
|
Loading…
Reference in New Issue