Exemplo n.º 1
0
    def testAssertWarnsRegexCallable(self):
        def _runtime_warn(msg):
            warnings.warn(msg, RuntimeWarning)

        self.assertWarnsRegex(RuntimeWarning, "o+", _runtime_warn, "foox")
        # Failure when no warning is triggered
        with self.assertRaises(self.failureException):
            self.assertWarnsRegex(RuntimeWarning, "o+", lambda: 0)
        # Failure when another warning is triggered
        with catch_warnings():
            # Force default filter (in user_case tests are run with -We)
            warnings.simplefilter("default", RuntimeWarning)
            with self.assertRaises(self.failureException):
                self.assertWarnsRegex(DeprecationWarning, "o+", _runtime_warn,
                                      "foox")
        # Failure when message doesn't match
        with self.assertRaises(self.failureException):
            self.assertWarnsRegex(RuntimeWarning, "o+", _runtime_warn, "barz")
        # A little trickier: we ask RuntimeWarnings to be raised, and then
        # check for some of them.  It is implementation-defined whether
        # non-matching RuntimeWarnings are simply re-raised, or produce a
        # failureException.
        with catch_warnings():
            warnings.simplefilter("error", RuntimeWarning)
            with self.assertRaises((RuntimeWarning, self.failureException)):
                self.assertWarnsRegex(RuntimeWarning, "o+", _runtime_warn,
                                      "barz")
Exemplo n.º 2
0
    def testAssertWarnsCallable(self):
        def _runtime_warn():
            warnings.warn("foo", RuntimeWarning)

        # Success when the right warning is triggered, even several times
        self.assertWarns(RuntimeWarning, _runtime_warn)
        self.assertWarns(RuntimeWarning, _runtime_warn)
        # A tuple of warning classes is accepted
        self.assertWarns((DeprecationWarning, RuntimeWarning), _runtime_warn)
        # *args and **kwargs also work
        self.assertWarns(RuntimeWarning,
                         warnings.warn,
                         "foo",
                         category=RuntimeWarning)
        # Failure when no warning is triggered
        with self.assertRaises(self.failureException):
            self.assertWarns(RuntimeWarning, lambda: 0)
        # Failure when another warning is triggered
        with catch_warnings():
            # Force default filter (in user_case tests are run with -We)
            warnings.simplefilter("default", RuntimeWarning)
            with self.assertRaises(self.failureException):
                self.assertWarns(DeprecationWarning, _runtime_warn)
        # Filters for other warnings are not modified
        with catch_warnings():
            warnings.simplefilter("error", RuntimeWarning)
            with self.assertRaises(RuntimeWarning):
                self.assertWarns(DeprecationWarning, _runtime_warn)
 def testAssertWarnsRegexCallable(self):
     def _runtime_warn(msg):
         warnings.warn(msg, RuntimeWarning)
     self.assertWarnsRegex(RuntimeWarning, "o+",
                           _runtime_warn, "foox")
     # Failure when no warning is triggered
     with self.assertRaises(self.failureException):
         self.assertWarnsRegex(RuntimeWarning, "o+",
                               lambda: 0)
     # Failure when another warning is triggered
     with catch_warnings():
         # Force default filter (in case tests are run with -We)
         warnings.simplefilter("default", RuntimeWarning)
         with self.assertRaises(self.failureException):
             self.assertWarnsRegex(DeprecationWarning, "o+",
                                   _runtime_warn, "foox")
     # Failure when message doesn't match
     with self.assertRaises(self.failureException):
         self.assertWarnsRegex(RuntimeWarning, "o+",
                               _runtime_warn, "barz")
     # A little trickier: we ask RuntimeWarnings to be raised, and then
     # check for some of them.  It is implementation-defined whether
     # non-matching RuntimeWarnings are simply re-raised, or produce a
     # failureException.
     with catch_warnings():
         warnings.simplefilter("error", RuntimeWarning)
         with self.assertRaises((RuntimeWarning, self.failureException)):
             self.assertWarnsRegex(RuntimeWarning, "o+",
                                   _runtime_warn, "barz")
 def testAssertWarnsRegexContext(self):
     # Same as above, but with assertWarnsRegex as a context manager
     def _runtime_warn(msg):
         warnings.warn(msg, RuntimeWarning)
     _runtime_warn_lineno = inspect.getsourcelines(_runtime_warn)[1]
     with self.assertWarnsRegex(RuntimeWarning, "o+") as cm:
         _runtime_warn("foox")
     self.assertIsInstance(cm.warning, RuntimeWarning)
     self.assertEqual(cm.warning.args[0], "foox")
     self.assertIn("_test_unittest2_with.py", cm.filename)
     self.assertEqual(cm.lineno, _runtime_warn_lineno + 1)
     # Failure when no warning is triggered
     with self.assertRaises(self.failureException):
         with self.assertWarnsRegex(RuntimeWarning, "o+"):
             pass
     # Failure when another warning is triggered
     with catch_warnings():
         # Force default filter (in case tests are run with -We)
         warnings.simplefilter("default", RuntimeWarning)
         with self.assertRaises(self.failureException):
             with self.assertWarnsRegex(DeprecationWarning, "o+"):
                 _runtime_warn("foox")
     # Failure when message doesn't match
     with self.assertRaises(self.failureException):
         with self.assertWarnsRegex(RuntimeWarning, "o+"):
             _runtime_warn("barz")
     # A little trickier: we ask RuntimeWarnings to be raised, and then
     # check for some of them.  It is implementation-defined whether
     # non-matching RuntimeWarnings are simply re-raised, or produce a
     # failureException.
     with catch_warnings():
         warnings.simplefilter("error", RuntimeWarning)
         with self.assertRaises((RuntimeWarning, self.failureException)):
             with self.assertWarnsRegex(RuntimeWarning, "o+"):
                 _runtime_warn("barz")
 def testAssertWarnsCallable(self):
     def _runtime_warn():
         warnings.warn("foo", RuntimeWarning)
     # Success when the right warning is triggered, even several times
     self.assertWarns(RuntimeWarning, _runtime_warn)
     self.assertWarns(RuntimeWarning, _runtime_warn)
     # A tuple of warning classes is accepted
     self.assertWarns((DeprecationWarning, RuntimeWarning), _runtime_warn)
     # *args and **kwargs also work
     self.assertWarns(RuntimeWarning,
                      warnings.warn, "foo", category=RuntimeWarning)
     # Failure when no warning is triggered
     with self.assertRaises(self.failureException):
         self.assertWarns(RuntimeWarning, lambda: 0)
     # Failure when another warning is triggered
     with catch_warnings():
         # Force default filter (in case tests are run with -We)
         warnings.simplefilter("default", RuntimeWarning)
         with self.assertRaises(self.failureException):
             self.assertWarns(DeprecationWarning, _runtime_warn)
     # Filters for other warnings are not modified
     with catch_warnings():
         warnings.simplefilter("error", RuntimeWarning)
         with self.assertRaises(RuntimeWarning):
             self.assertWarns(DeprecationWarning, _runtime_warn)
 def test_assert_dict_unicode_error(self):
     with catch_warnings(record=True):
         # This causes a UnicodeWarning due to its craziness
         one = ''.join(chr(i) for i in range(255))
         # this used to cause a UnicodeDecodeError constructing the failure msg
         with self.assertRaises(self.failureException):
             self.assertDictContainsSubset({'foo': one}, {'foo': u('\uFFFD')})
 def test_assert_dict_unicode_error(self):
     with catch_warnings(record=True):
         # This causes a UnicodeWarning due to its craziness
         one = ''.join(chr(i) for i in range(255))
         # this used to cause a UnicodeDecodeError constructing the failure msg
         with self.assertRaises(self.failureException):
             self.assertDictContainsSubset({'foo': one}, {'foo': u'\uFFFD'})
Exemplo n.º 8
0
 def __enter__(self):
     # The __warningregistry__'s need to be in a pristine state for tests
     # to work properly.
     for v in sys.modules.values():
         if getattr(v, '__warningregistry__', None):
             v.__warningregistry__ = {}
     self.warnings_manager = catch_warnings(record=True)
     self.warnings = self.warnings_manager.__enter__()
     warnings.simplefilter("always", self.expected)
     return self
Exemplo n.º 9
0
 def __enter__(self):
     # The __warningregistry__'s need to be in a pristine state for tests
     # to work properly.
     for v in sys.modules.values():
         if getattr(v, '__warningregistry__', None):
             v.__warningregistry__ = {}
     self.warnings_manager = catch_warnings(record=True)
     self.warnings = self.warnings_manager.__enter__()
     warnings.simplefilter("always", self.expected)
     return self
Exemplo n.º 10
0
    def testAssertWarnsContext(self):
        # Believe it or not, it is preferrable to duplicate all tests above,
        # to make sure the __warningregistry__ $@ is circumvented correctly.
        def _runtime_warn():
            warnings.warn("foo", RuntimeWarning)

        _runtime_warn_lineno = inspect.getsourcelines(_runtime_warn)[1]
        with self.assertWarns(RuntimeWarning) as cm:
            _runtime_warn()
        # A tuple of warning classes is accepted
        with self.assertWarns((DeprecationWarning, RuntimeWarning)) as cm:
            _runtime_warn()
        # The context manager exposes various useful attributes
        self.assertIsInstance(cm.warning, RuntimeWarning)
        self.assertEqual(cm.warning.args[0], "foo")
        self.assertIn("_test_unittest2_with.py", cm.filename)
        self.assertEqual(cm.lineno, _runtime_warn_lineno + 1)
        # Same with several warnings
        with self.assertWarns(RuntimeWarning):
            _runtime_warn()
            _runtime_warn()
        with self.assertWarns(RuntimeWarning):
            warnings.warn("foo", category=RuntimeWarning)
        # Failure when no warning is triggered
        with self.assertRaises(self.failureException):
            with self.assertWarns(RuntimeWarning):
                pass
        # Failure when another warning is triggered
        with catch_warnings():
            # Force default filter (in user_case tests are run with -We)
            warnings.simplefilter("default", RuntimeWarning)
            with self.assertRaises(self.failureException):
                with self.assertWarns(DeprecationWarning):
                    _runtime_warn()
        # Filters for other warnings are not modified
        with catch_warnings():
            warnings.simplefilter("error", RuntimeWarning)
            with self.assertRaises(RuntimeWarning):
                with self.assertWarns(DeprecationWarning):
                    _runtime_warn()
    def testAssertWarnsContext(self):
        # Believe it or not, it is preferrable to duplicate all tests above,
        # to make sure the __warningregistry__ $@ is circumvented correctly.
        def _runtime_warn():
            warnings.warn("foo", RuntimeWarning)

        _runtime_warn_lineno = inspect.getsourcelines(_runtime_warn)[1]
        with self.assertWarns(RuntimeWarning) as cm:
            _runtime_warn()
        # A tuple of warning classes is accepted
        with self.assertWarns((DeprecationWarning, RuntimeWarning)) as cm:
            _runtime_warn()
        # The context manager exposes various useful attributes
        self.assertIsInstance(cm.warning, RuntimeWarning)
        self.assertEqual(cm.warning.args[0], "foo")
        self.assertIn("_test_unittest2_with.py", cm.filename)
        self.assertEqual(cm.lineno, _runtime_warn_lineno + 1)
        # Same with several warnings
        with self.assertWarns(RuntimeWarning):
            _runtime_warn()
            _runtime_warn()
        with self.assertWarns(RuntimeWarning):
            warnings.warn("foo", category=RuntimeWarning)
        # Failure when no warning is triggered
        with self.assertRaises(self.failureException):
            with self.assertWarns(RuntimeWarning):
                pass
        # Failure when another warning is triggered
        with catch_warnings():
            # Force default filter (in case tests are run with -We)
            warnings.simplefilter("default", RuntimeWarning)
            with self.assertRaises(self.failureException):
                with self.assertWarns(DeprecationWarning):
                    _runtime_warn()
        # Filters for other warnings are not modified
        with catch_warnings():
            warnings.simplefilter("error", RuntimeWarning)
            with self.assertRaises(RuntimeWarning):
                with self.assertWarns(DeprecationWarning):
                    _runtime_warn()
Exemplo n.º 12
0
    def testAssertWarnsRegexContext(self):
        # Same as above, but with assertWarnsRegex as a context manager
        def _runtime_warn(msg):
            warnings.warn(msg, RuntimeWarning)

        _runtime_warn_lineno = inspect.getsourcelines(_runtime_warn)[1]
        with self.assertWarnsRegex(RuntimeWarning, "o+") as cm:
            _runtime_warn("foox")
        self.assertIsInstance(cm.warning, RuntimeWarning)
        self.assertEqual(cm.warning.args[0], "foox")
        self.assertIn("_test_unittest2_with.py", cm.filename)
        self.assertEqual(cm.lineno, _runtime_warn_lineno + 1)
        # Failure when no warning is triggered
        with self.assertRaises(self.failureException):
            with self.assertWarnsRegex(RuntimeWarning, "o+"):
                pass
        # Failure when another warning is triggered
        with catch_warnings():
            # Force default filter (in user_case tests are run with -We)
            warnings.simplefilter("default", RuntimeWarning)
            with self.assertRaises(self.failureException):
                with self.assertWarnsRegex(DeprecationWarning, "o+"):
                    _runtime_warn("foox")
        # Failure when message doesn't match
        with self.assertRaises(self.failureException):
            with self.assertWarnsRegex(RuntimeWarning, "o+"):
                _runtime_warn("barz")
        # A little trickier: we ask RuntimeWarnings to be raised, and then
        # check for some of them.  It is implementation-defined whether
        # non-matching RuntimeWarnings are simply re-raised, or produce a
        # failureException.
        with catch_warnings():
            warnings.simplefilter("error", RuntimeWarning)
            with self.assertRaises((RuntimeWarning, self.failureException)):
                with self.assertWarnsRegex(RuntimeWarning, "o+"):
                    _runtime_warn("barz")
Exemplo n.º 13
0
 def test_formatMessage_unicode_error(self):
     with catch_warnings(record=True):
         # This causes a UnicodeWarning due to its craziness
         one = ''.join(chr(i) for i in range(255))
         # this used to cause a UnicodeDecodeError constructing msg
         self._formatMessage(one, u('\uFFFD'))
 def test_formatMessage_unicode_error(self):
     with catch_warnings(record=True):
         # This causes a UnicodeWarning due to its craziness
         one = ''.join(chr(i) for i in range(255))
         # this used to cause a UnicodeDecodeError constructing msg
         self._formatMessage(one, u('\uFFFD'))