(self)
| 1660 | self.assertWarns(DeprecationWarning, _runtime_warn) |
| 1661 | |
| 1662 | def testAssertWarnsContext(self): |
| 1663 | # Believe it or not, it is preferable to duplicate all tests above, |
| 1664 | # to make sure the __warningregistry__ $@ is circumvented correctly. |
| 1665 | def _runtime_warn(): |
| 1666 | warnings.warn("foo", RuntimeWarning) |
| 1667 | _runtime_warn_lineno = inspect.getsourcelines(_runtime_warn)[1] |
| 1668 | with self.assertWarns(RuntimeWarning) as cm: |
| 1669 | _runtime_warn() |
| 1670 | # A tuple of warning classes is accepted |
| 1671 | with self.assertWarns((DeprecationWarning, RuntimeWarning)) as cm: |
| 1672 | _runtime_warn() |
| 1673 | # The context manager exposes various useful attributes |
| 1674 | self.assertIsInstance(cm.warning, RuntimeWarning) |
| 1675 | self.assertEqual(cm.warning.args[0], "foo") |
| 1676 | self.assertIn("test_case.py", cm.filename) |
| 1677 | self.assertEqual(cm.lineno, _runtime_warn_lineno + 1) |
| 1678 | # Same with several warnings |
| 1679 | with self.assertWarns(RuntimeWarning): |
| 1680 | _runtime_warn() |
| 1681 | _runtime_warn() |
| 1682 | with self.assertWarns(RuntimeWarning): |
| 1683 | warnings.warn("foo", category=RuntimeWarning) |
| 1684 | # Failure when no warning is triggered |
| 1685 | with self.assertRaises(self.failureException): |
| 1686 | with self.assertWarns(RuntimeWarning): |
| 1687 | pass |
| 1688 | # Custom message |
| 1689 | with self.assertRaisesRegex(self.failureException, 'foobar'): |
| 1690 | with self.assertWarns(RuntimeWarning, msg='foobar'): |
| 1691 | pass |
| 1692 | # Invalid keyword argument |
| 1693 | with self.assertRaisesRegex(TypeError, 'foobar'): |
| 1694 | with self.assertWarns(RuntimeWarning, foobar=42): |
| 1695 | pass |
| 1696 | # Failure when another warning is triggered |
| 1697 | with warnings.catch_warnings(): |
| 1698 | # Force default filter (in case tests are run with -We) |
| 1699 | warnings.simplefilter("default", RuntimeWarning) |
| 1700 | with self.assertRaises(self.failureException): |
| 1701 | with self.assertWarns(DeprecationWarning): |
| 1702 | _runtime_warn() |
| 1703 | # Filters for other warnings are not modified |
| 1704 | with warnings.catch_warnings(): |
| 1705 | warnings.simplefilter("error", RuntimeWarning) |
| 1706 | with self.assertRaises(RuntimeWarning): |
| 1707 | with self.assertWarns(DeprecationWarning): |
| 1708 | _runtime_warn() |
| 1709 | |
| 1710 | def testAssertWarnsNoExceptionType(self): |
| 1711 | with self.assertRaises(TypeError): |
nothing calls this directly
no test coverage detected