def testCatchBreakInstallsHandler(self):
module = sys.modules['unittest2.main']
original = module.installHandler
def restore():
module.installHandler = original
self.addCleanup(restore)
self.installed = False
def fakeInstallHandler():
self.installed = True
module.installHandler = fakeInstallHandler
program = self.program
program.catchbreak = True
program.testRunner = FakeRunner
program.runTests()
self.assertTrue(self.installed)
python类main()的实例源码
def no_test_main_is_shared(self):
self.assertTrue(macho_dump.main is _cmdline.main)
self.assertTrue(macho_find.main is _cmdline.main)
def test_NonExit(self):
program = unittest2.main(exit=False,
argv=["foobar"],
testRunner=unittest2.TextTestRunner(stream=StringIO()),
testLoader=self.FooBarLoader())
self.assertTrue(hasattr(program, 'result'))
def test_Exit(self):
self.assertRaises(
SystemExit,
unittest2.main,
argv=["foobar"],
testRunner=unittest2.TextTestRunner(stream=StringIO()),
exit=True,
testLoader=self.FooBarLoader())
def test_ExitAsDefault(self):
self.assertRaises(
SystemExit,
unittest2.main,
argv=["foobar"],
testRunner=unittest2.TextTestRunner(stream=StringIO()),
testLoader=self.FooBarLoader())
def test_async_produce_thread_exception(self):
"""Ensure that an exception on a worker thread is raised to the main thread"""
consumer = self._get_consumer()
with self.assertRaises(AttributeError):
with self._get_producer(min_queued_messages=1) as producer:
# get some dummy data into the queue that will cause a crash
# when flushed:
msg = Message("stuff", partition_id=0)
del msg.value
producer._produce(msg)
while consumer.consume() is not None:
time.sleep(.05)
def main():
unittest.main()
def test_wildcard(self):
"""Wildcard input"""
self.compile(prefix='wildcard', args='../sample/*.h', main=True, output="wildcard.out")
def test_namespace1(self):
"""Nested namespace declarations"""
if self.fog == '':
self.compile(prefix='namespace1', args='Namespace1.h', main=True, failBuild=True)
else:
self.compile(prefix='namespace1', args='Namespace1.h', main=True, output="namespace.out")
def test_namespace2(self):
"""Explicit namespace declarations"""
self.compile(prefix='namespace2', args='Namespace2.h', main=True, output="namespace.out")
def test_shared_main(self):
saved_stderr = sys.stderr
saved_argv = sys.argv
try:
sys.stderr = StringIO()
sys.argv = ['macho_tool']
self.assertEqual(_cmdline.main(lambda *args: None), 1)
self.assertEqual(sys.stderr.getvalue(), 'Usage: macho_tool filename...\n')
names = []
def record_names(fp, name):
self.assertEqual(fp, sys.stdout)
names.append(name)
sys.stderr = StringIO()
sys.argv = ['macho_tool', '/bin/sh']
self.assertEqual(_cmdline.main(record_names), 0)
self.assertEqual(sys.stderr.getvalue(), '')
self.assertEqual(names, ['/bin/sh'])
names = []
sys.stderr = StringIO()
sys.argv = ['macho_tool', '/bin/sh', '/bin/ls']
self.assertEqual(_cmdline.main(record_names), 0)
self.assertEqual(sys.stderr.getvalue(), '')
self.assertEqual(names, ['/bin/sh', '/bin/ls'])
names = []
sys.stderr = StringIO()
sys.argv = ['macho_tool', '/bin']
self.assertEqual(_cmdline.main(record_names), 0)
self.assertEqual(sys.stderr.getvalue(), '')
names.sort()
dn = '/bin'
real_names = [
os.path.join(dn, fn) for fn in os.listdir(dn)
if util.is_platform_file(os.path.join(dn, fn)) ]
real_names.sort()
self.assertEqual(names, real_names)
finally:
sys.stderr = saved_stderr
sys.argv = saved_argv
def check_root(self, prefix='', output=None):
self.init(prefix)
args = "--have-eh --abort-on-fail --root --error-printer"
if self.cxxtest_import:
os.chdir(currdir)
cxxtest.cxxtestgen.main(['cxxtestgen', self.fog, '-o', self.py_cpp]+re.split('[ ]+',args), True)
else:
cmd = join_commands("cd %s" % currdir,
"%s %s../bin/cxxtestgen %s -o %s %s > %s 2>&1" % (sys.executable, currdir, self.fog, self.py_cpp, args, self.py_out))
status = subprocess.call(cmd, shell=True)
self.assertEqual(status, 0, 'Error executing cxxtestgen')
#
files = [self.py_cpp]
for i in [1,2]:
args = "--have-eh --abort-on-fail --part Part%s.h" % str(i)
file = currdir+self.prefix+'_py%s.cpp' % str(i)
files.append(file)
if self.cxxtest_import:
os.chdir(currdir)
cxxtest.cxxtestgen.main(['cxxtestgen', self.fog, '-o', file]+re.split('[ ]+',args), True)
else:
cmd = join_commands("cd %s" % currdir,
"%s %s../bin/cxxtestgen %s -o %s %s > %s 2>&1" % (sys.executable, currdir, self.fog, file, args, self.py_out))
status = subprocess.call(cmd, shell=True)
self.assertEqual(status, 0, 'Error executing cxxtestgen')
#
cmd = join_commands("cd %s" % currdir,
"%s %s %s %s. %s%s../ %s > %s 2>&1" % (self.compiler, self.exe_option, self.build_target, self.include_option, self.include_option, currdir, ' '.join(files), self.build_log))
status = subprocess.call(cmd, shell=True)
for file in files:
if os.path.exists(file):
os.remove(file)
self.assertEqual(status, 0, 'Error executing command: '+cmd)
#
cmd = join_commands("cd %s" % currdir,
"%s %s -v > %s 2>&1" % (self.valgrind, self.build_target, self.px_pre))
status = subprocess.call(cmd, shell=True)
OUTPUT = open(self.px_pre,'a')
OUTPUT.write('Error level = '+str(status)+'\n')
OUTPUT.close()
diffstr = file_diff(self.px_pre, currdir+output, self.file_filter)
if not diffstr == '':
self.fail("Unexpected differences in output:\n"+diffstr)
if self.valgrind != '':
self.parse_valgrind(self.px_pre)
#
self.passed=True