shift deprecated_call helper into pytest_recwarn plugin

--HG--
branch : trunk
This commit is contained in:
holger krekel 2009-06-18 17:26:40 +02:00
parent 4a48a50e3b
commit 964ecbdb19
3 changed files with 82 additions and 70 deletions

View File

@ -112,32 +112,6 @@ def raises(ExpectedException, *args, **kwargs):
raise ExceptionFailure(msg="DID NOT RAISE",
expr=args, expected=ExpectedException)
def deprecated_call(func, *args, **kwargs):
""" assert that calling func(*args, **kwargs)
triggers a DeprecationWarning.
"""
warningmodule = py.std.warnings
l = []
oldwarn_explicit = getattr(warningmodule, 'warn_explicit')
def warn_explicit(*args, **kwargs):
l.append(args)
oldwarn_explicit(*args, **kwargs)
oldwarn = getattr(warningmodule, 'warn')
def warn(*args, **kwargs):
l.append(args)
oldwarn(*args, **kwargs)
warningmodule.warn_explicit = warn_explicit
warningmodule.warn = warn
try:
ret = func(*args, **kwargs)
finally:
warningmodule.warn_explicit = warn_explicit
warningmodule.warn = warn
if not l:
#print warningmodule
raise AssertionError("%r did not produce DeprecationWarning" %(func,))
return ret
# exitcodes for the command line
EXIT_OK = 0

View File

@ -1,6 +1,13 @@
"""
"recwarn" funcarg for asserting that warnings are shown to a user.
help performing checks for deprecation and other warnings. Provides:
recwarn: function argument where one can call recwarn.pop() to get
the last warning that would have been shown.
py.test.deprecated_call(func, *args, **kwargs):
assert that a function call triggers a deprecation warning.
"""
import py
import os
@ -10,6 +17,37 @@ def pytest_funcarg__recwarn(request):
request.addfinalizer(warnings.finalize)
return warnings
def pytest_namespace(config):
return {'deprecated_call': deprecated_call}
def deprecated_call(func, *args, **kwargs):
""" assert that calling func(*args, **kwargs)
triggers a DeprecationWarning.
"""
warningmodule = py.std.warnings
l = []
oldwarn_explicit = getattr(warningmodule, 'warn_explicit')
def warn_explicit(*args, **kwargs):
l.append(args)
oldwarn_explicit(*args, **kwargs)
oldwarn = getattr(warningmodule, 'warn')
def warn(*args, **kwargs):
l.append(args)
oldwarn(*args, **kwargs)
warningmodule.warn_explicit = warn_explicit
warningmodule.warn = warn
try:
ret = func(*args, **kwargs)
finally:
warningmodule.warn_explicit = warn_explicit
warningmodule.warn = warn
if not l:
#print warningmodule
raise AssertionError("%r did not produce DeprecationWarning" %(func,))
return ret
class RecordedWarning:
def __init__(self, message, category, filename, lineno, line):
self.message = message
@ -88,3 +126,46 @@ def test_recwarn_functional(testdir):
res = reprec.countoutcomes()
assert tuple(res) == (2, 0, 0), res
#
# ============ test py.test.deprecated_call() ==============
#
def dep(i):
if i == 0:
py.std.warnings.warn("is deprecated", DeprecationWarning)
return 42
reg = {}
def dep_explicit(i):
if i == 0:
py.std.warnings.warn_explicit("dep_explicit", category=DeprecationWarning,
filename="hello", lineno=3)
def test_deprecated_call_raises():
excinfo = py.test.raises(AssertionError,
"py.test.deprecated_call(dep, 3)")
assert str(excinfo).find("did not produce") != -1
def test_deprecated_call():
py.test.deprecated_call(dep, 0)
def test_deprecated_call_ret():
ret = py.test.deprecated_call(dep, 0)
assert ret == 42
def test_deprecated_call_preserves():
r = py.std.warnings.onceregistry.copy()
f = py.std.warnings.filters[:]
test_deprecated_call_raises()
test_deprecated_call()
assert r == py.std.warnings.onceregistry
assert f == py.std.warnings.filters
def test_deprecated_explicit_call_raises():
py.test.raises(AssertionError,
"py.test.deprecated_call(dep_explicit, 3)")
def test_deprecated_explicit_call():
py.test.deprecated_call(dep_explicit, 0)
py.test.deprecated_call(dep_explicit, 0)

View File

@ -15,49 +15,6 @@ class TestRaises:
def test_raises_function(self):
py.test.raises(ValueError, int, 'hello')
#
# ============ test py.test.deprecated_call() ==============
#
def dep(i):
if i == 0:
py.std.warnings.warn("is deprecated", DeprecationWarning)
return 42
reg = {}
def dep_explicit(i):
if i == 0:
py.std.warnings.warn_explicit("dep_explicit", category=DeprecationWarning,
filename="hello", lineno=3)
def test_deprecated_call_raises():
excinfo = py.test.raises(AssertionError,
"py.test.deprecated_call(dep, 3)")
assert str(excinfo).find("did not produce") != -1
def test_deprecated_call():
py.test.deprecated_call(dep, 0)
def test_deprecated_call_ret():
ret = py.test.deprecated_call(dep, 0)
assert ret == 42
def test_deprecated_call_preserves():
r = py.std.warnings.onceregistry.copy()
f = py.std.warnings.filters[:]
test_deprecated_call_raises()
test_deprecated_call()
assert r == py.std.warnings.onceregistry
assert f == py.std.warnings.filters
def test_deprecated_explicit_call_raises():
py.test.raises(AssertionError,
"py.test.deprecated_call(dep_explicit, 3)")
def test_deprecated_explicit_call():
py.test.deprecated_call(dep_explicit, 0)
py.test.deprecated_call(dep_explicit, 0)
def test_importorskip():
from py.__.test.outcome import Skipped
try: