本文整理汇总了Python中test.test_support.captured_stderr方法的典型用法代码示例。如果您正苦于以下问题:Python test_support.captured_stderr方法的具体用法?Python test_support.captured_stderr怎么用?Python test_support.captured_stderr使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类test.test_support
的用法示例。
在下文中一共展示了test_support.captured_stderr方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_issue31285
# 需要导入模块: from test import test_support [as 别名]
# 或者: from test.test_support import captured_stderr [as 别名]
def test_issue31285(self):
# warn_explicit() shouldn't raise a SystemError in case the return
# value of get_source() has a bad splitlines() method.
class BadLoader:
def get_source(self, fullname):
class BadSource(str):
def splitlines(self):
return 42
return BadSource('spam')
wmod = self.module
with original_warnings.catch_warnings(module=wmod):
wmod.filterwarnings('default', category=UserWarning)
with test_support.captured_stderr() as stderr:
wmod.warn_explicit(
'foo', UserWarning, 'bar', 1,
module_globals={'__loader__': BadLoader(),
'__name__': 'foobar'})
self.assertIn('UserWarning: foo', stderr.getvalue())
示例2: test_directory
# 需要导入模块: from test import test_support [as 别名]
# 或者: from test.test_support import captured_stderr [as 别名]
def test_directory(self):
os.mkdir(test_support.TESTFN)
self.addCleanup(test_support.rmtree, test_support.TESTFN)
c_filename = os.path.join(test_support.TESTFN, "file.c")
with open(c_filename, "w") as file:
file.write("int xx;\n")
with open(os.path.join(test_support.TESTFN, "file.py"), "w") as file:
file.write("xx = 'unaltered'\n")
script = os.path.join(scriptsdir, "fixcid.py")
# ignore dbg() messages
with test_support.captured_stderr() as stderr:
output = self.run_script(args=(test_support.TESTFN,))
self.assertMultiLineEqual(output,
"{}:\n"
"1\n"
'< int xx;\n'
'> int yy;\n'.format(c_filename),
"stderr: %s" % stderr.getvalue()
)
示例3: test_sni_callback_wrong_return_type
# 需要导入模块: from test import test_support [as 别名]
# 或者: from test.test_support import captured_stderr [as 别名]
def test_sni_callback_wrong_return_type(self):
# Returning the wrong return type terminates the TLS connection
# with an internal error alert.
server_context, other_context, client_context = self.sni_contexts()
def cb_wrong_return_type(ssl_sock, server_name, initial_context):
return "foo"
server_context.set_servername_callback(cb_wrong_return_type)
with self.assertRaises(ssl.SSLError) as cm, \
support.captured_stderr() as stderr:
stats = server_params_test(client_context, server_context,
chatty=False,
sni_name='supermessage')
self.assertEqual(cm.exception.reason, 'TLSV1_ALERT_INTERNAL_ERROR')
self.assertIn("TypeError", stderr.getvalue())
示例4: test_unicode_args
# 需要导入模块: from test import test_support [as 别名]
# 或者: from test.test_support import captured_stderr [as 别名]
def test_unicode_args(self):
e = RuntimeError(u"Drink \u2615") # coffee emoji
# Can take the repr of any object
self.assertEqual(repr(e), "RuntimeError(u'Drink \u2615',)")
# Cannot of course turn a non-ascii Unicode object into a str, even if it's an exception object
with self.assertRaises(UnicodeEncodeError) as cm:
str(e)
self.assertEqual(
str(cm.exception),
"'ascii' codec can't encode character u'\u2615' in position 6: ordinal not in range(128)")
# But the exception hook, via Py#displayException, does not fail when attempting to __str__ the exception args
with test_support.captured_stderr() as s:
sys.excepthook(RuntimeError, u"Drink \u2615", None)
# At minimum, it tells us what kind of exception it was
self.assertEqual(s.getvalue()[:12], "RuntimeError")
# It is fine with ascii values, of course
with test_support.captured_stderr() as s:
sys.excepthook(RuntimeError, u"Drink java", None)
self.assertEqual(s.getvalue(), "RuntimeError: Drink java\n")
示例5: test_main_exception
# 需要导入模块: from test import test_support [as 别名]
# 或者: from test.test_support import captured_stderr [as 别名]
def test_main_exception(self):
with captured_stderr() as error_stringio:
s = self.run_main(switches=['1.0/0.0'])
self.assert_exc_string(error_stringio.getvalue(), 'ZeroDivisionError')
示例6: test_main_exception_fixed_reps
# 需要导入模块: from test import test_support [as 别名]
# 或者: from test.test_support import captured_stderr [as 别名]
def test_main_exception_fixed_reps(self):
with captured_stderr() as error_stringio:
s = self.run_main(switches=['-n1', '1.0/0.0'])
self.assert_exc_string(error_stringio.getvalue(), 'ZeroDivisionError')
示例7: test_sni_callback_raising
# 需要导入模块: from test import test_support [as 别名]
# 或者: from test.test_support import captured_stderr [as 别名]
def test_sni_callback_raising(self):
# Raising fails the connection with a TLS handshake failure alert.
server_context, other_context, client_context = self.sni_contexts()
def cb_raising(ssl_sock, server_name, initial_context):
1.0/0.0
server_context.set_servername_callback(cb_raising)
with self.assertRaises(ssl.SSLError) as cm, \
support.captured_stderr() as stderr:
stats = server_params_test(client_context, server_context,
chatty=False,
sni_name='supermessage')
self.assertEqual(cm.exception.reason, 'SSLV3_ALERT_HANDSHAKE_FAILURE')
self.assertIn("ZeroDivisionError", stderr.getvalue())
示例8: test_invalid_args
# 需要导入模块: from test import test_support [as 别名]
# 或者: from test.test_support import captured_stderr [as 别名]
def test_invalid_args(self):
# Popen() called with invalid arguments should raise TypeError
# but Popen.__del__ should not complain (issue #12085)
with test_support.captured_stderr() as s:
self.assertRaises(TypeError, subprocess.Popen, invalid_arg_name=1)
argcount = subprocess.Popen.__init__.__code__.co_argcount
too_many_args = [0] * (argcount + 1)
self.assertRaises(TypeError, subprocess.Popen, *too_many_args)
self.assertEqual(s.getvalue(), '')
示例9: test_sni_callback_raising
# 需要导入模块: from test import test_support [as 别名]
# 或者: from test.test_support import captured_stderr [as 别名]
def test_sni_callback_raising(self):
# Raising fails the connection with a TLS handshake failure alert.
server_context, other_context, client_context = self.sni_contexts()
def cb_raising(ssl_sock, server_name, initial_context):
1/0
server_context.set_servername_callback(cb_raising)
with self.assertRaises(ssl.SSLError) as cm, \
support.captured_stderr() as stderr:
stats = server_params_test(client_context, server_context,
chatty=False,
sni_name='supermessage')
self.assertEqual(cm.exception.reason, 'SSLV3_ALERT_HANDSHAKE_FAILURE')
self.assertIn("ZeroDivisionError", stderr.getvalue())
示例10: test_run_show
# 需要导入模块: from test import test_support [as 别名]
# 或者: from test.test_support import captured_stderr [as 别名]
def test_run_show(self):
with captured_stderr() as f:
run.idle_showwarning_subproc(
'Test', UserWarning, 'test_warning.py', 99, f, 'Line of code')
# The following uses .splitlines to erase line-ending differences
self.assertEqual(idlemsg.splitlines(), f.getvalue().splitlines())
示例11: test_shell_show
# 需要导入模块: from test import test_support [as 别名]
# 或者: from test.test_support import captured_stderr [as 别名]
def test_shell_show(self):
with captured_stderr() as f:
shell.idle_showwarning(
'Test', UserWarning, 'test_warning.py', 99, f, 'Line of code')
self.assertEqual(shellmsg.splitlines(), f.getvalue().splitlines())