本文整理汇总了Python中test.test_support.check_warnings函数的典型用法代码示例。如果您正苦于以下问题:Python check_warnings函数的具体用法?Python check_warnings怎么用?Python check_warnings使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了check_warnings函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_check_warnings
def test_check_warnings(self):
# Explicit tests for the test_support convenience wrapper
wmod = self.module
if wmod is not sys.modules['warnings']:
self.skipTest('module to test is not loaded warnings module')
with test_support.check_warnings(quiet=False) as w:
self.assertEqual(w.warnings, [])
wmod.simplefilter("always")
wmod.warn("foo")
self.assertEqual(str(w.message), "foo")
wmod.warn("bar")
self.assertEqual(str(w.message), "bar")
self.assertEqual(str(w.warnings[0].message), "foo")
self.assertEqual(str(w.warnings[1].message), "bar")
w.reset()
self.assertEqual(w.warnings, [])
with test_support.check_warnings():
# defaults to quiet=True without argument
pass
with test_support.check_warnings(('foo', UserWarning)):
wmod.warn("foo")
with self.assertRaises(AssertionError):
with test_support.check_warnings(('', RuntimeWarning)):
# defaults to quiet=False with argument
pass
with self.assertRaises(AssertionError):
with test_support.check_warnings(('foo', RuntimeWarning)):
wmod.warn("foo")
示例2: test_issue3221
def test_issue3221(self):
# Regression test for http://bugs.python.org/issue3221.
def check_absolute():
exec "from os import path" in ns
def check_relative():
exec "from . import relimport" in ns
# Check both OK with __package__ and __name__ correct
ns = dict(__package__='test', __name__='test.notarealmodule')
check_absolute()
check_relative()
# Check both OK with only __name__ wrong
ns = dict(__package__='test', __name__='notarealpkg.notarealmodule')
check_absolute()
check_relative()
# Check relative fails with only __package__ wrong
ns = dict(__package__='foo', __name__='test.notarealmodule')
with check_warnings(('.+foo', RuntimeWarning)):
check_absolute()
self.assertRaises(SystemError, check_relative)
# Check relative fails with __package__ and __name__ wrong
ns = dict(__package__='foo', __name__='notarealpkg.notarealmodule')
with check_warnings(('.+foo', RuntimeWarning)):
check_absolute()
self.assertRaises(SystemError, check_relative)
# Check both fail with package set to a non-string
ns = dict(__package__=object())
self.assertRaises(ValueError, check_absolute)
self.assertRaises(ValueError, check_relative)
示例3: test_compress_deprecated
def test_compress_deprecated(self):
tmpdir, tmpdir2, base_name = self._create_files()
# using compress and testing the PendingDeprecationWarning
old_dir = os.getcwd()
os.chdir(tmpdir)
try:
with check_warnings() as w:
warnings.simplefilter("always")
make_tarball(base_name, 'dist', compress='compress')
finally:
os.chdir(old_dir)
tarball = base_name + '.tar.Z'
self.assertTrue(os.path.exists(tarball))
self.assertEqual(len(w.warnings), 1)
# same test with dry_run
os.remove(tarball)
old_dir = os.getcwd()
os.chdir(tmpdir)
try:
with check_warnings() as w:
warnings.simplefilter("always")
make_tarball(base_name, 'dist', compress='compress',
dry_run=True)
finally:
os.chdir(old_dir)
self.assertFalse(os.path.exists(tarball))
self.assertEqual(len(w.warnings), 1)
示例4: test_issue3221
def test_issue3221(self):
def check_absolute():
exec "from os import path" in ns
def check_relative():
exec "from . import relimport" in ns
# Check both OK with __package__ and __name__ correct
ns = dict(__package__='test', __name__='test.notarealmodule')
check_absolute()
check_relative()
# Check both OK with only __name__ wrong
ns = dict(__package__='test', __name__='notarealpkg.notarealmodule')
check_absolute()
check_relative()
# Check relative fails with only __package__ wrong
ns = dict(__package__='foo', __name__='test.notarealmodule')
with check_warnings() as w:
check_absolute()
self.assert_('foo' in str(w.message))
self.assertEqual(w.category, RuntimeWarning)
self.assertRaises(SystemError, check_relative)
# Check relative fails with __package__ and __name__ wrong
ns = dict(__package__='foo', __name__='notarealpkg.notarealmodule')
with check_warnings() as w:
check_absolute()
self.assert_('foo' in str(w.message))
self.assertEqual(w.category, RuntimeWarning)
self.assertRaises(SystemError, check_relative)
# Check both fail with package set to a non-string
ns = dict(__package__=object())
self.assertRaises(ValueError, check_absolute)
self.assertRaises(ValueError, check_relative)
示例5: testAssertDictContainsSubset
def testAssertDictContainsSubset(self):
self.assertDictContainsSubset({}, {})
self.assertDictContainsSubset({}, {'a': 1})
self.assertDictContainsSubset({'a': 1}, {'a': 1})
self.assertDictContainsSubset({'a': 1}, {'a': 1,
'b': 2})
self.assertDictContainsSubset({'a': 1,
'b': 2}, {'a': 1,
'b': 2})
with self.assertRaises(self.failureException):
self.assertDictContainsSubset({1: 'one'}, {})
with self.assertRaises(self.failureException):
self.assertDictContainsSubset({'a': 2}, {'a': 1})
with self.assertRaises(self.failureException):
self.assertDictContainsSubset({'c': 1}, {'a': 1})
with self.assertRaises(self.failureException):
self.assertDictContainsSubset({'a': 1,
'c': 1}, {'a': 1})
with self.assertRaises(self.failureException):
self.assertDictContainsSubset({'a': 1,
'c': 1}, {'a': 1})
with test_support.check_warnings(('', UnicodeWarning)):
one = ''.join((chr(i) for i in range(255)))
with self.assertRaises(self.failureException):
self.assertDictContainsSubset({'foo': one}, {'foo': u'\ufffd'})
示例6: test_extension_init
def test_extension_init(self):
# the first argument, which is the name, must be a string
self.assertRaises(AssertionError, Extension, 1, [])
ext = Extension('name', [])
self.assertEquals(ext.name, 'name')
# the second argument, which is the list of files, must
# be a list of strings
self.assertRaises(AssertionError, Extension, 'name', 'file')
self.assertRaises(AssertionError, Extension, 'name', ['file', 1])
ext = Extension('name', ['file1', 'file2'])
self.assertEquals(ext.sources, ['file1', 'file2'])
# others arguments have defaults
for attr in ('include_dirs', 'define_macros', 'undef_macros',
'library_dirs', 'libraries', 'runtime_library_dirs',
'extra_objects', 'extra_compile_args', 'extra_link_args',
'export_symbols', 'swig_opts', 'depends'):
self.assertEquals(getattr(ext, attr), [])
self.assertEquals(ext.language, None)
self.assertEquals(ext.optional, None)
# if there are unknown keyword options, warn about them
with check_warnings() as w:
warnings.simplefilter('always')
ext = Extension('name', ['file1', 'file2'], chic=True)
self.assertEquals(len(w.warnings), 1)
self.assertEquals(str(w.warnings[0].message),
"Unknown Extension options: 'chic'")
示例7: testDeprecatedMessageAttribute
def testDeprecatedMessageAttribute(self):
# Accessing BaseException.message and relying on its value set by
# BaseException.__init__ triggers a deprecation warning.
exc = BaseException("foo")
with check_warnings(("BaseException.message has been deprecated " "as of Python 2.6", DeprecationWarning)) as w:
self.assertEqual(exc.message, "foo")
self.assertEqual(len(w.warnings), 1)
示例8: test_forbidden_names
def test_forbidden_names(self):
# So we don't screw up our globals
def safe_exec(expr):
def f(**kwargs): pass
exec expr in {'f' : f}
tests = [("True", "assignment to True or False is forbidden in 3.x"),
("False", "assignment to True or False is forbidden in 3.x"),
("nonlocal", "nonlocal is a keyword in 3.x")]
with check_warnings() as w:
for keyword, expected in tests:
safe_exec("{0} = False".format(keyword))
self.assertWarning(None, w, expected)
w.reset()
try:
safe_exec("obj.{0} = True".format(keyword))
except NameError:
pass
self.assertWarning(None, w, expected)
w.reset()
safe_exec("def {0}(): pass".format(keyword))
self.assertWarning(None, w, expected)
w.reset()
safe_exec("class {0}: pass".format(keyword))
self.assertWarning(None, w, expected)
w.reset()
safe_exec("def f({0}=43): pass".format(keyword))
self.assertWarning(None, w, expected)
w.reset()
示例9: check_all
def check_all(self, modname):
names = {}
with support.check_warnings((".* (module|package)",
DeprecationWarning), quiet=True):
try:
exec "import %s" % modname in names
except:
# Silent fail here seems the best route since some modules
# may not be available or not initialize properly in all
# environments.
raise FailedImport(modname)
if not hasattr(sys.modules[modname], "__all__"):
raise NoAll(modname)
names = {}
try:
exec "from %s import *" % modname in names
except Exception as e:
# Include the module name in the exception string
self.fail("__all__ failure in {}: {}: {}".format(
modname, e.__class__.__name__, e))
if "__builtins__" in names:
del names["__builtins__"]
keys = set(names)
all = set(sys.modules[modname].__all__)
self.assertEqual(keys, all)
示例10: test_main
def test_main():
with check_warnings(
("complex divmod.., // and % are deprecated", DeprecationWarning),
("classic (int|long) division", DeprecationWarning),
quiet=True,
):
run_unittest(CoercionTest)
示例11: testAssertDictContainsSubset
def testAssertDictContainsSubset(self):
self.assertDictContainsSubset({}, {})
self.assertDictContainsSubset({}, {'a': 1})
self.assertDictContainsSubset({'a': 1}, {'a': 1})
self.assertDictContainsSubset({'a': 1}, {'a': 1, 'b': 2})
self.assertDictContainsSubset({'a': 1, 'b': 2}, {'a': 1, 'b': 2})
with self.assertRaises(self.failureException):
self.assertDictContainsSubset({1: "one"}, {})
with self.assertRaises(self.failureException):
self.assertDictContainsSubset({'a': 2}, {'a': 1})
with self.assertRaises(self.failureException):
self.assertDictContainsSubset({'c': 1}, {'a': 1})
with self.assertRaises(self.failureException):
self.assertDictContainsSubset({'a': 1, 'c': 1}, {'a': 1})
with self.assertRaises(self.failureException):
self.assertDictContainsSubset({'a': 1, 'c': 1}, {'a': 1})
with test_support.check_warnings(("", UnicodeWarning)):
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'})
示例12: test_check_metadata_deprecated
def test_check_metadata_deprecated(self):
# makes sure make_metadata is deprecated
dist, cmd = self.get_cmd()
with check_warnings() as w:
warnings.simplefilter("always")
cmd.check_metadata()
self.assertEqual(len(w.warnings), 1)
示例13: testAssertItemsEqual
def testAssertItemsEqual(self):
a = object()
self.assertItemsEqual([1, 2, 3], [3, 2, 1])
self.assertItemsEqual(["foo", "bar", "baz"], ["bar", "baz", "foo"])
self.assertItemsEqual([a, a, 2, 2, 3], (a, 2, 3, a, 2))
self.assertItemsEqual([1, "2", "a", "a"], ["a", "2", True, "a"])
self.assertRaises(self.failureException, self.assertItemsEqual, [1, 2] + [3] * 100, [1] * 100 + [2, 3])
self.assertRaises(self.failureException, self.assertItemsEqual, [1, "2", "a", "a"], ["a", "2", True, 1])
self.assertRaises(self.failureException, self.assertItemsEqual, [10], [10, 11])
self.assertRaises(self.failureException, self.assertItemsEqual, [10, 11], [10])
self.assertRaises(self.failureException, self.assertItemsEqual, [10, 11, 10], [10, 11])
# Test that sequences of unhashable objects can be tested for sameness:
self.assertItemsEqual([[1, 2], [3, 4], 0], [False, [3, 4], [1, 2]])
with test_support.check_warnings(quiet=True) as w:
# hashable types, but not orderable
self.assertRaises(self.failureException, self.assertItemsEqual, [], [divmod, "x", 1, 5j, 2j, frozenset()])
# comparing dicts raises a py3k warning
self.assertItemsEqual([{"a": 1}, {"b": 2}], [{"b": 2}, {"a": 1}])
# comparing heterogenous non-hashable sequences raises a py3k warning
self.assertItemsEqual([1, "x", divmod, []], [divmod, [], "x", 1])
self.assertRaises(self.failureException, self.assertItemsEqual, [], [divmod, [], "x", 1, 5j, 2j, set()])
# fail the test if warnings are not silenced
if w.warnings:
self.fail("assertItemsEqual raised a warning: " + str(w.warnings[0]))
self.assertRaises(self.failureException, self.assertItemsEqual, [[1]], [[2]])
# Same elements, but not same sequence length
self.assertRaises(self.failureException, self.assertItemsEqual, [1, 1, 2], [2, 1])
self.assertRaises(self.failureException, self.assertItemsEqual, [1, 1, "2", "a", "a"], ["2", "2", True, "a"])
self.assertRaises(
self.failureException, self.assertItemsEqual, [1, {"b": 2}, None, True], [{"b": 2}, True, None]
)
示例14: testAssertDictContainsSubset
def testAssertDictContainsSubset(self):
self.assertDictContainsSubset({}, {})
self.assertDictContainsSubset({}, {"a": 1})
self.assertDictContainsSubset({"a": 1}, {"a": 1})
self.assertDictContainsSubset({"a": 1}, {"a": 1, "b": 2})
self.assertDictContainsSubset({"a": 1, "b": 2}, {"a": 1, "b": 2})
with self.assertRaises(self.failureException):
self.assertDictContainsSubset({1: "one"}, {})
with self.assertRaises(self.failureException):
self.assertDictContainsSubset({"a": 2}, {"a": 1})
with self.assertRaises(self.failureException):
self.assertDictContainsSubset({"c": 1}, {"a": 1})
with self.assertRaises(self.failureException):
self.assertDictContainsSubset({"a": 1, "c": 1}, {"a": 1})
with self.assertRaises(self.failureException):
self.assertDictContainsSubset({"a": 1, "c": 1}, {"a": 1})
if test_support.due_to_ironpython_bug("http://ironpython.codeplex.com/workitem/28171"):
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"})
else:
with test_support.check_warnings(("", UnicodeWarning)):
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"})
示例15: testAssertDictContainsSubset
def testAssertDictContainsSubset(self):
self.assertDictContainsSubset({}, {})
self.assertDictContainsSubset({}, {"a": 1})
self.assertDictContainsSubset({"a": 1}, {"a": 1})
self.assertDictContainsSubset({"a": 1}, {"a": 1, "b": 2})
self.assertDictContainsSubset({"a": 1, "b": 2}, {"a": 1, "b": 2})
with self.assertRaises(self.failureException):
self.assertDictContainsSubset({1: "one"}, {})
with self.assertRaises(self.failureException):
self.assertDictContainsSubset({"a": 2}, {"a": 1})
with self.assertRaises(self.failureException):
self.assertDictContainsSubset({"c": 1}, {"a": 1})
with self.assertRaises(self.failureException):
self.assertDictContainsSubset({"a": 1, "c": 1}, {"a": 1})
with self.assertRaises(self.failureException):
self.assertDictContainsSubset({"a": 1, "c": 1}, {"a": 1})
with test_support.check_warnings(("", UnicodeWarning)):
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"})