2010-10-11 05:45:45 +08:00
|
|
|
|
2011-12-05 18:10:48 +08:00
|
|
|
Asserting deprecation and other warnings
|
2010-10-11 05:45:45 +08:00
|
|
|
=====================================================
|
|
|
|
|
2011-09-06 17:43:42 +08:00
|
|
|
The recwarn function argument
|
2010-10-11 05:45:45 +08:00
|
|
|
------------------------------------
|
|
|
|
|
|
|
|
You can use the ``recwarn`` funcarg to assert that code triggers
|
|
|
|
warnings through the Python warnings system. Here is a simple
|
|
|
|
self-contained test::
|
|
|
|
|
|
|
|
# content of test_recwarn.py
|
|
|
|
def test_hello(recwarn):
|
|
|
|
from warnings import warn
|
|
|
|
warn("hello", DeprecationWarning)
|
|
|
|
w = recwarn.pop(DeprecationWarning)
|
|
|
|
assert issubclass(w.category, DeprecationWarning)
|
|
|
|
assert 'hello' in str(w.message)
|
|
|
|
assert w.filename
|
|
|
|
assert w.lineno
|
|
|
|
|
|
|
|
The ``recwarn`` function argument provides these methods:
|
|
|
|
|
|
|
|
* ``pop(category=None)``: return last warning matching the category.
|
|
|
|
* ``clear()``: clear list of warnings
|
|
|
|
|
2011-09-06 17:43:42 +08:00
|
|
|
Ensuring a function triggers a deprecation warning
|
2010-10-11 05:45:45 +08:00
|
|
|
-------------------------------------------------------
|
|
|
|
|
|
|
|
You can also call a global helper for checking
|
|
|
|
that a certain function call triggers a Deprecation
|
|
|
|
warning::
|
|
|
|
|
2010-11-18 05:12:16 +08:00
|
|
|
import pytest
|
2010-10-11 05:45:45 +08:00
|
|
|
|
|
|
|
def test_global():
|
2010-11-18 05:12:16 +08:00
|
|
|
pytest.deprecated_call(myfunction, 17)
|