2009-09-06 22:59:39 +08:00
|
|
|
import py
|
2010-01-14 00:15:54 +08:00
|
|
|
from py._plugin.pytest_recwarn import WarningsRecorder
|
2009-09-06 22:59:39 +08:00
|
|
|
|
2010-07-05 04:13:12 +08:00
|
|
|
def test_WarningRecorder(recwarn):
|
2009-09-06 22:59:39 +08:00
|
|
|
showwarning = py.std.warnings.showwarning
|
|
|
|
rec = WarningsRecorder()
|
|
|
|
assert py.std.warnings.showwarning != showwarning
|
|
|
|
assert not rec.list
|
|
|
|
py.std.warnings.warn_explicit("hello", UserWarning, "xyz", 13)
|
|
|
|
assert len(rec.list) == 1
|
|
|
|
py.std.warnings.warn(DeprecationWarning("hello"))
|
|
|
|
assert len(rec.list) == 2
|
|
|
|
warn = rec.pop()
|
|
|
|
assert str(warn.message) == "hello"
|
|
|
|
l = rec.list
|
|
|
|
rec.clear()
|
|
|
|
assert len(rec.list) == 0
|
|
|
|
assert l is rec.list
|
|
|
|
py.test.raises(AssertionError, "rec.pop()")
|
|
|
|
rec.finalize()
|
|
|
|
assert showwarning == py.std.warnings.showwarning
|
|
|
|
|
|
|
|
def test_recwarn_functional(testdir):
|
|
|
|
reprec = testdir.inline_runsource("""
|
2010-07-27 03:15:15 +08:00
|
|
|
pytest_plugins = 'pytest_recwarn',
|
2009-09-06 22:59:39 +08:00
|
|
|
import warnings
|
|
|
|
oldwarn = warnings.showwarning
|
|
|
|
def test_method(recwarn):
|
|
|
|
assert warnings.showwarning != oldwarn
|
|
|
|
warnings.warn("hello")
|
|
|
|
warn = recwarn.pop()
|
|
|
|
assert isinstance(warn.message, UserWarning)
|
|
|
|
def test_finalized():
|
|
|
|
assert warnings.showwarning == oldwarn
|
|
|
|
""")
|
|
|
|
res = reprec.countoutcomes()
|
|
|
|
assert tuple(res) == (2, 0, 0), res
|
2010-07-27 03:15:15 +08:00
|
|
|
|
2009-09-06 22:59:39 +08:00
|
|
|
#
|
|
|
|
# ============ 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:
|
2010-07-27 03:15:15 +08:00
|
|
|
py.std.warnings.warn_explicit("dep_explicit", category=DeprecationWarning,
|
2009-09-06 22:59:39 +08:00
|
|
|
filename="hello", lineno=3)
|
|
|
|
|
|
|
|
def test_deprecated_call_raises():
|
2010-07-27 03:15:15 +08:00
|
|
|
excinfo = py.test.raises(AssertionError,
|
2009-09-06 22:59:39 +08:00
|
|
|
"py.test.deprecated_call(dep, 3)")
|
2010-07-27 03:15:15 +08:00
|
|
|
assert str(excinfo).find("did not produce") != -1
|
2009-09-06 22:59:39 +08:00
|
|
|
|
|
|
|
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():
|
2010-07-27 03:15:15 +08:00
|
|
|
py.test.raises(AssertionError,
|
2009-09-06 22:59:39 +08:00
|
|
|
"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)
|
|
|
|
|