本文整理汇总了Python中unittest.installHandler函数的典型用法代码示例。如果您正苦于以下问题:Python installHandler函数的具体用法?Python installHandler怎么用?Python installHandler使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了installHandler函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testSecondInterrupt
def testSecondInterrupt(self):
if due_to_ironpython_bug("http://ironpython.codeplex.com/workitem/28171"):
return
result = unittest.TestResult()
unittest.installHandler()
unittest.registerResult(result)
def test(result):
pid = os.getpid()
os.kill(pid, signal.SIGINT)
result.breakCaught = True
if not due_to_ironpython_bug("http://ironpython.codeplex.com/workitem/28171"):
self.assertTrue(result.shouldStop)
os.kill(pid, signal.SIGINT)
if not due_to_ironpython_bug("http://ironpython.codeplex.com/workitem/28171"):
self.fail("Second KeyboardInterrupt not raised")
try:
test(result)
except KeyboardInterrupt:
pass
else:
if not due_to_ironpython_bug("http://ironpython.codeplex.com/workitem/28171"):
self.fail("Second KeyboardInterrupt not raised")
self.assertTrue(result.breakCaught)
示例2: testTwoResults
def testTwoResults(self):
if due_to_ironpython_bug("http://ironpython.codeplex.com/workitem/28171"):
return
unittest.installHandler()
result = unittest.TestResult()
unittest.registerResult(result)
new_handler = signal.getsignal(signal.SIGINT)
result2 = unittest.TestResult()
unittest.registerResult(result2)
self.assertEqual(signal.getsignal(signal.SIGINT), new_handler)
result3 = unittest.TestResult()
def test(result):
pid = os.getpid()
os.kill(pid, signal.SIGINT)
try:
test(result)
except KeyboardInterrupt:
self.fail("KeyboardInterrupt not handled")
if not due_to_ironpython_bug("http://ironpython.codeplex.com/workitem/28171"):
self.assertTrue(result.shouldStop)
self.assertTrue(result2.shouldStop)
self.assertFalse(result3.shouldStop)
示例3: __init__
def __init__(self, tests=[], from_gui=False, categories=['system', 'unit', 'gui', 'verification'], timing=False):
"""Store the list of tests to preform.
The test list should be something like ['N_state_model.test_stereochem_analysis']. The first part is the imported test case class, the second is the specific test.
@keyword tests: The list of tests to preform. If left at [], then all tests will be run.
@type tests: list of str
@keyword from_gui: A flag which indicates if the tests are being run from the GUI or not.
@type from_gui: bool
@keyword categories: The list of test categories to run, for example ['system', 'unit', 'gui', 'verification'] for all tests.
@type categories: list of str
@keyword timing: A flag which if True will enable timing of individual tests.
@type timing: bool
"""
# Store the args.
self.tests = tests
self.from_gui = from_gui
self.categories = categories
# Set up the test runner.
if from_gui:
self.runner = GuiTestRunner(stream=sys.stdout, timing=timing)
else:
self.runner = RelaxTestRunner(stream=sys.stdout, timing=timing)
# Let the tests handle the keyboard interrupt (for Python 2.7 and higher).
if hasattr(unittest, 'installHandler'):
unittest.installHandler()
示例4: testTwoResults
def testTwoResults(self):
unittest.installHandler()
result = unittest.TestResult()
unittest.registerResult(result)
new_handler = signal.getsignal(signal.SIGINT)
result2 = unittest.TestResult()
unittest.registerResult(result2)
self.assertEqual(signal.getsignal(signal.SIGINT), new_handler)
result3 = unittest.TestResult()
def test(result):
pid = os.getpid()
os.kill(pid, signal.SIGINT)
try:
test(result)
except KeyboardInterrupt:
self.fail("KeyboardInterrupt not handled")
self.assertTrue(result.shouldStop)
self.assertTrue(result2.shouldStop)
self.assertFalse(result3.shouldStop)
示例5: testRemoveHandler
def testRemoveHandler(self):
default_handler = signal.getsignal(signal.SIGINT)
unittest.installHandler()
unittest.removeHandler()
self.assertEqual(signal.getsignal(signal.SIGINT), default_handler)
unittest.removeHandler()
self.assertEqual(signal.getsignal(signal.SIGINT), default_handler)
示例6: run
def run(self):
build = self.get_finalized_command("build")
build.run()
if self.catch:
unittest.installHandler()
sys.path.insert(0, str(Path(build.build_lib).resolve()))
test_loader = unittest.defaultTestLoader
if self.test_name:
tests = test_loader.loadTestsFromName(self.test_name, None)
else:
tests = test_loader.discover(self.start_dir, self.pattern)
test_runner = unittest.TextTestRunner(
verbosity = self.verbose,
failfast = self.failfast,
buffer = self.buffer
)
test_results = test_runner.run(tests)
del sys.path[0]
if self.exit and not test_results.wasSuccessful():
raise SystemExit()
示例7: run
def run(self):
try:
import coverage
use_cov = True
except:
use_cov = False
cov = None
if use_cov:
# The latter is required to not give errors on f23, probably
# a temporary bug.
omit = ["/usr/*", "/*/tests/*", "/builddir/*"]
cov = coverage.coverage(omit=omit)
cov.erase()
cov.start()
import tests as testsmodule
testsmodule.cov = cov
testsmodule.utils.REGENERATE_OUTPUT = bool(self.regenerate_output)
if hasattr(unittest, "installHandler"):
try:
unittest.installHandler()
except:
print "installHandler hack failed"
tests = unittest.TestLoader().loadTestsFromNames(self._testfiles)
if self.only:
newtests = []
for suite1 in tests:
for suite2 in suite1:
for testcase in suite2:
if self.only in str(testcase):
newtests.append(testcase)
if not newtests:
print "--only didn't find any tests"
sys.exit(1)
tests = unittest.TestSuite(newtests)
print "Running only:"
for test in newtests:
print "%s" % test
print
t = unittest.TextTestRunner(verbosity=1)
try:
result = t.run(tests)
except KeyboardInterrupt:
sys.exit(1)
if use_cov:
cov.stop()
cov.save()
err = int(bool(len(result.failures) > 0 or
len(result.errors) > 0))
if not err and use_cov and self.coverage:
cov.report(show_missing=False)
sys.exit(err)
示例8: run
def run(self):
try:
# Use system 'coverage' if available
import coverage
use_coverage = True
except:
use_coverage = False
tests = unittest.TestLoader().loadTestsFromNames(self._testfiles)
t = unittest.TextTestRunner(verbosity=1)
if use_coverage:
coverage.erase()
coverage.start()
if hasattr(unittest, "installHandler"):
try:
unittest.installHandler()
except:
print "installHandler hack failed"
try:
result = t.run(tests)
except KeyboardInterrupt:
sys.exit(1)
if use_coverage:
coverage.stop()
sys.exit(int(bool(len(result.failures) > 0 or
len(result.errors) > 0)))
示例9: runTests
def runTests(self):
if (self.catchbreak
and getattr(unittest, 'installHandler', None) is not None):
unittest.installHandler()
self.result = self.testRunner.run(self.test)
if self.exit:
sys.exit(not self.result.wasSuccessful())
示例10: run_tests
def run_tests(file):
suite = get_tests_suite(file)
unittest.installHandler()
logging.disable(logging.INFO)
results = unittest.TextTestRunner(verbosity=2).run(suite)
logging.disable(logging.NOTSET)
unittest.removeHandler()
return results
示例11: run
def run(self,
test_modules=__all__,
pretend=False,
logfile='test/test.log',
loglevel=forcebalance.output.INFO,
**kwargs):
self.check()
self.logger.setLevel(loglevel)
# first install unittest interrupt handler which gracefully finishes current test on Ctrl+C
unittest.installHandler()
# create blank test suite and fill it with test suites loaded from each test module
tests = unittest.TestSuite()
systemTests = unittest.TestSuite()
for suite in unittest.defaultTestLoader.discover('test'):
for module in suite:
for test in module:
modName,caseName,testName = test.id().split('.')
if modName in test_modules:
if modName=="test_system": systemTests.addTest(test)
else: tests.addTest(test)
tests.addTests(systemTests) # integration tests should be run after other tests
result = ForceBalanceTestResult()
forcebalance.output.getLogger("forcebalance").addHandler(forcebalance.output.NullHandler())
### START TESTING ###
# run any pretest tasks before first test
result.startTestRun(tests)
# if pretend option is enabled, skip all tests instead of running them
if pretend:
for test in tests:
result.addSkip(test)
# otherwise do a normal test run
else:
unittest.registerResult(result)
try:
tests.run(result)
except KeyboardInterrupt:
# Adding this allows us to determine
# what is causing tests to hang indefinitely.
import traceback
traceback.print_exc()
self.logger.exception(msg="Test run cancelled by user")
except:
self.logger.exception(msg="An unexpected exception occurred while running tests\n")
result.stopTestRun(tests)
### STOP TESTING ###
return result
示例12: run_tests
def run_tests(options, testsuite, runner=None):
if runner is None:
runner = unittest.TextTestRunner()
runner.verbosity = options.verbose
runner.failfast = options.failfast
if options.catchbreak:
unittest.installHandler()
result = runner.run(testsuite)
return result.wasSuccessful()
示例13: main
def main():
installHandler()
option_parser = optparse.OptionParser(
usage="%prog [options] [filenames]",
description="pyjaco unittests script."
)
option_parser.add_option(
"-a",
"--run-all",
action="store_true",
dest="run_all",
default=False,
help="run all tests (including the known-to-fail)"
)
option_parser.add_option(
"-x",
"--no-error",
action="store_true",
dest="no_error",
default=False,
help="ignores error (don't display them after tests)"
)
option_parser.add_option(
"-f",
"--only-failing",
action="store_true",
dest="only_failing",
default=False,
help="run only failing tests (to check for improvements)"
)
options, args = option_parser.parse_args()
with open("py-builtins.js", "w") as f:
builtins = BuiltinGenerator().generate_builtins()
f.write(builtins)
runner = testtools.runner.Py2JsTestRunner(verbosity=2)
results = None
try:
if options.run_all:
results = runner.run(testtools.tests.ALL)
elif options.only_failing:
results = runner.run(testtools.tests.KNOWN_TO_FAIL)
elif args:
results = runner.run(testtools.tests.get_tests(args))
else:
results = runner.run(testtools.tests.NOT_KNOWN_TO_FAIL)
except KeyboardInterrupt:
pass
if not options.no_error and results and results.errors:
print
print "errors:"
print " (use -x to skip this part)"
for test, error in results.errors:
print
print "*", str(test), "*"
print error
示例14: testRemoveHandler
def testRemoveHandler(self):
default_handler = signal.getsignal(signal.SIGINT)
unittest.installHandler()
unittest.removeHandler()
self.assertEqual(signal.getsignal(signal.SIGINT), default_handler)
# check that calling removeHandler multiple times has no ill-effect
unittest.removeHandler()
self.assertEqual(signal.getsignal(signal.SIGINT), default_handler)
示例15: testRemoveHandlerAsDecorator
def testRemoveHandlerAsDecorator(self):
default_handler = signal.getsignal(signal.SIGINT)
unittest.installHandler()
@unittest.removeHandler
def test():
self.assertEqual(signal.getsignal(signal.SIGINT), default_handler)
test()
self.assertNotEqual(signal.getsignal(signal.SIGINT), default_handler)