[TIP] Tox and UserWarnings
Albert-Jan Roskam
fomcl at yahoo.com
Sat Jun 6 14:22:14 PDT 2015
Hi,
I am using a custom warning and I wanted to test whether it works as expected.
I wanted to suppress all warnings (PYTHONWARNINGS = ignore), except my own custom
warnings (action="default"). When I run test below in Tox, it passes in Python
2.7, 3.4 and pypy, but not Python 3.3. When I run the test directly (not with Tox)
it works with all versions. If I set PYTHONWARNINGS = default, all tests pass in Tox.
Not a huge deal, but I thought it would be nice to mention it here. I have no idea why this
happens, but it seems that there are subtle cross-version differences wrt the warnings module.
#!/usr/bin/env python#
-*- coding: utf-8 -*-
# test_booh.py
import unittest
import warnings
class MyCustomWarning(UserWarning): pass
warnings.filterwarnings("default", category=MyCustomWarning)
def panic():
warnings.warn("Booh", MyCustomWarning, stacklevel=2)
class test_MyCustomWarning(unittest.TestCase):
def test_raises_SPSSIOWarning(self):
with warnings.catch_warnings(record=True) as warn:
panic()
mywarnings = [item.message for item in warn]
self.assertTrue("Booh" in str(mywarnings)) # [1]
# [1] Python 3.3.4, in Tox:
# if setenv = PYTHONWARNINGS = ignore:
# --> AssertionError: False is not true
# if setenv = PYTHONWARNINGS = default:
# OK
if __name__ == "__main__":
unittest.main()
Regards,
Albert-Jan
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a
fresh water system, and public health, what have the Romans ever done for us?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
More information about the testing-in-python
mailing list