def testTwoResults(self): unittest2.installHandler() result = unittest2.TestResult() unittest2.registerResult(result) new_handler = signal.getsignal(signal.SIGINT) result2 = unittest2.TestResult() unittest2.registerResult(result2) self.assertEqual(signal.getsignal(signal.SIGINT), new_handler) result3 = unittest2.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)
def runTests(self): if (self.catchbreak and getattr(unittest, 'installHandler', None) is not None): unittest.installHandler() testRunner = self._get_runner() self.result = testRunner.run(self.test) if self.exit: sys.exit(not self.result.wasSuccessful())
def testRemoveHandler(self): default_handler = signal.getsignal(signal.SIGINT) unittest2.installHandler() unittest2.removeHandler() self.assertEqual(signal.getsignal(signal.SIGINT), default_handler) # check that calling removeHandler multiple times has no ill-effect unittest2.removeHandler() self.assertEqual(signal.getsignal(signal.SIGINT), default_handler)
def testRemoveHandlerAsDecorator(self): default_handler = signal.getsignal(signal.SIGINT) unittest2.installHandler() @unittest2.removeHandler def test(): self.assertEqual(signal.getsignal(signal.SIGINT), default_handler) test() self.assertNotEqual(signal.getsignal(signal.SIGINT), default_handler)
def testInstallHandler(self): default_handler = signal.getsignal(signal.SIGINT) unittest2.installHandler() self.assertNotEqual(signal.getsignal(signal.SIGINT), default_handler) try: pid = os.getpid() os.kill(pid, signal.SIGINT) except KeyboardInterrupt: self.fail("KeyboardInterrupt not handled") self.assertTrue(unittest2.signals._interrupt_handler.called)
def testRemoveResult(self): result = unittest2.TestResult() unittest2.registerResult(result) unittest2.installHandler() self.assertTrue(unittest2.removeResult(result)) # Should this raise an error instead? self.assertFalse(unittest2.removeResult(unittest2.TestResult())) try: pid = os.getpid() os.kill(pid, signal.SIGINT) except KeyboardInterrupt: pass self.assertFalse(result.shouldStop)
def testHandlerReplacedButCalled(self): # If our handler has been replaced (is no longer installed) but is # called by the *new* handler, then it isn't safe to delay the # SIGINT and we should immediately delegate to the default handler unittest2.installHandler() handler = signal.getsignal(signal.SIGINT) def new_handler(frame, signum): handler(frame, signum) signal.signal(signal.SIGINT, new_handler) try: pid = os.getpid() os.kill(pid, signal.SIGINT) except KeyboardInterrupt: pass else: self.fail("replaced but delegated handler doesn't raise interrupt")
def run_parallel(self, test_suites, test_runner, json_result=False): exit_code = 0 proc = None unittest.installHandler() processes = [] manager = Manager() results = manager.list() start = time.time() for test_suite in test_suites: proc = Process(target=execute_test, args=( test_runner, test_suite, results)) processes.append(proc) proc.start() for proc in processes: proc.join() finish = time.time() errors, failures, _ = dump_results(start, finish, results) if json_result: all_results = [] for tests, result in zip(test_suites, results): result_parser = SummarizeResults( vars(result), tests, (finish - start)) all_results += result_parser.gather_results() # Convert Result objects to dicts for serialization json_results = [] for r in all_results: json_results.append(r.__dict__) with open(os.getcwd() + "/results.json", 'wb') as result_file: json.dump(json_results, result_file) if failures or errors: exit_code = 1 return exit_code
def run_parallel(self, test_suites, test_runner, result_type=None, results_path=None): exit_code = 0 proc = None unittest.installHandler() processes = [] manager = Manager() results = manager.list() start = time.time() for test_suite in test_suites: proc = Process(target=execute_test, args=( test_runner, test_suite, results)) processes.append(proc) proc.start() for proc in processes: proc.join() finish = time.time() errors, failures, _ = dump_results(start, finish, results) if result_type is not None: all_results = [] for tests, result in zip(test_suites, results): result_parser = SummarizeResults( vars(result), tests, (finish - start)) all_results += result_parser.gather_results() reporter = Reporter(result_parser=result_parser, all_results=all_results) reporter.generate_report(result_type=result_type, path=results_path) if failures or errors: exit_code = 1 return exit_code
def testInterruptCaught(self): default_handler = signal.getsignal(signal.SIGINT) result = unittest2.TestResult() unittest2.installHandler() unittest2.registerResult(result) self.assertNotEqual(signal.getsignal(signal.SIGINT), default_handler) def test(result): pid = os.getpid() os.kill(pid, signal.SIGINT) result.breakCaught = True self.assertTrue(result.shouldStop) try: test(result) except KeyboardInterrupt: self.fail("KeyboardInterrupt not handled") self.assertTrue(result.breakCaught)
def testSecondInterrupt(self): result = unittest2.TestResult() unittest2.installHandler() unittest2.registerResult(result) def test(result): pid = os.getpid() os.kill(pid, signal.SIGINT) result.breakCaught = True self.assertTrue(result.shouldStop) os.kill(pid, signal.SIGINT) self.fail("Second KeyboardInterrupt not raised") try: test(result) except KeyboardInterrupt: pass else: self.fail("Second KeyboardInterrupt not raised") self.assertTrue(result.breakCaught)
def run_parallel(self, test_suites, test_runner, json_result=False): exit_code = 0 proc = None unittest.installHandler() processes = [] manager = Manager() results = manager.list() start = time.time() for test_suite in test_suites: proc = Process(target=execute_test, args=(test_runner, test_suite, results)) processes.append(proc) proc.start() for proc in processes: proc.join() finish = time.time() errors, failures, _ = dump_results(start, finish, results) if json_result: all_results = [] for tests, result in zip(test_suites, results): result_parser = SummarizeResults(vars(result), tests, (finish - start)) all_results += result_parser.gather_results() # Convert Result objects to dicts for serialization json_results = [] for r in all_results: json_results.append(r.__dict__) with open(os.getcwd() + "/results.json", 'wb') as result_file: json.dump(json_results, result_file) if failures or errors: exit_code = 1 return exit_code
def testHandlerReplacedButCalled(self): # Can't use skipIf decorator because the signal handler may have # been changed after defining this method. if signal.getsignal(signal.SIGINT) == signal.SIG_IGN: self.skipTest("test requires SIGINT to not be ignored") # If our handler has been replaced (is no longer installed) but is # called by the *new* handler, then it isn't safe to delay the # SIGINT and we should immediately delegate to the default handler unittest2.installHandler() handler = signal.getsignal(signal.SIGINT) def new_handler(frame, signum): handler(frame, signum) signal.signal(signal.SIGINT, new_handler) try: pid = os.getpid() os.kill(pid, signal.SIGINT) except KeyboardInterrupt: pass else: self.fail("replaced but delegated handler doesn't raise interrupt")
def run_serialized(self, master_suite, test_runner, result_type=None, results_path=None): exit_code = 0 unittest.installHandler() start_time = time.time() result = test_runner.run(master_suite) total_execution_time = time.time() - start_time if result_type is not None: result_parser = SummarizeResults(vars(result), master_suite, total_execution_time) all_results = result_parser.gather_results() reporter = Reporter(result_parser=result_parser, all_results=all_results) reporter.generate_report(result_type=result_type, path=results_path) log_results(result) if not result.wasSuccessful(): exit_code = 1 return exit_code
def testSecondInterrupt(self): # Can't use skipIf decorator because the signal handler may have # been changed after defining this method. if signal.getsignal(signal.SIGINT) == signal.SIG_IGN: self.skipTest("test requires SIGINT to not be ignored") result = unittest2.TestResult() unittest2.installHandler() unittest2.registerResult(result) def test(result): pid = os.getpid() os.kill(pid, signal.SIGINT) result.breakCaught = True self.assertTrue(result.shouldStop) os.kill(pid, signal.SIGINT) self.fail("Second KeyboardInterrupt not raised") try: test(result) except KeyboardInterrupt: pass else: self.fail("Second KeyboardInterrupt not raised") self.assertTrue(result.breakCaught)
def run_serialized(self, master_suite, test_runner, json_result=False): exit_code = 0 unittest.installHandler() start_time = time.time() result = test_runner.run(master_suite) total_execution_time = time.time() - start_time if json_result: result_parser = SummarizeResults(vars(result), master_suite, total_execution_time) all_results = result_parser.gather_results() # Convert Result objects to dicts for serialization json_results = [] for r in all_results: json_results.append(r.__dict__) with open(os.getcwd() + "/results.json", 'wb') as result_file: json.dump(json_results, result_file) log_results(result) if not result.wasSuccessful(): exit_code = 1 return exit_code
def run(self): ''' loops through all the packages, modules, and methods sent in from the command line and runs them ''' test_classes = [] cl_args = self.get_cl_args() module_regex = None if os.path.exists(BASE_DIR) is False: err_msg = self.error_msg( "{0} does not exist - Exiting".format(BASE_DIR)) print self.colorize(err_msg, HIGHLIGHTED_RED) exit(1) if cl_args.module is None: module_regex = '*.py' else: if cl_args.module.find('.py') != -1: module_regex = cl_args.module else: module_regex = '.'.join([cl_args.module, 'py']) if cl_args.method_regex is None: method_regex = 'test_*' else: if cl_args.method_regex.find('test_') != -1: method_regex = cl_args.method_regex else: method_regex = ''.join(['test_', cl_args.method_regex]) parent_path = BASE_DIR config_path = self.get_config_path(parent_path, cl_args.product, cl_args.config) repo_path = self.get_repo_path(cl_args.product) if os.path.exists(repo_path) is False: err_msg = self.error_msg( 'Repo', ' '.join( [cl_args.product, repo_path, 'does not exist - Exiting'])) print self.colorize(err_msg, HIGHLIGHTED_RED) exit(1) if cl_args.list is not None: for arg in cl_args.list: if arg == 'tests': banner = ''.join(['\n', '<[TEST REPO]>', '\n']) path = repo_path else: banner = ''.join(['\n', '<[CONFIGS]>', '\n']) path = os.path.join(parent_path, 'configs', cl_args.product) print self.colorize(banner, WHITE) self.tree(path, ' ', print_files=True) else: suite = unittest.TestSuite() master_suite = unittest.TestSuite() #Use the parallel runner if needed so the console logs look correct if cl_args.parallel: test_runner = CCParallelTextTestRunner(verbosity=cl_args.quiet) else: test_runner = unittest.TextTestRunner(verbosity=cl_args.quiet) test_runner.failfast = cl_args.fail_fast #-----------------------Debug Logger----------------------------- #this is for the debug logger. its breaking right now #test_runner = LogCaptureRunner(verbosity=cl_args.quiet) #-----------------------Debug Logger----------------------------- try: stats_log_path = '/'.join([ LOG_BASE_PATH, cl_args.product, cl_args.config, "statistics" ]) product_log_path = '/'.join([ LOG_BASE_PATH, cl_args.product, cl_args.config, self.get_safe_file_date() ]) except TypeError: print 'Config was not set on command line - Exiting' exit(1) if os.path.isdir(stats_log_path) is not True: os.makedirs(stats_log_path) if os.path.isdir(product_log_path) is not True: os.makedirs(product_log_path) #Get and then ensure the existence of the cc data directory data_dir = None user_data_dir = getattr(cl_args, 'data_directory', None) if user_data_dir is not None: #Quit if the data directory is user-defined and doesn't exist, #otherwise it uses the user defined data dir user_data_dir = os.path.expanduser(user_data_dir) if os.path.isdir(user_data_dir): data_dir = user_data_dir else: print "Data directory '{0}' does not exist. Exiting."\ .format(user_data_dir) exit(1) else: #Make and use the default directory if it doesn't exist ''' @TODO: Make this create a sub-directory based on the product name like the log_dir does (minus timestamps and config file name) ''' data_dir = DATA_DIR if not os.path.isdir(data_dir): os.makedirs(data_dir) self.set_env(config_path, product_log_path, data_dir) if cl_args.packages is None: for module in self.get_modules(repo_path, module_regex): try: loaded_module = self.load_module(module) except ImportError: self.print_traceback() continue try: suite = self.build_suite(loaded_module, method_regex, cl_args.tags, cl_args.supress_flag) master_suite.addTests(suite) test_classes.append(suite) except ImportError: self.print_traceback() continue except AttributeError: self.print_traceback() continue except Exception: self.print_traceback() continue else: for package_name in cl_args.packages: test_path = self.find_subdir(repo_path, package_name) if test_path is None: err_msg = self.error_msg('Package', package_name) print self.colorize(err_msg, HIGHLIGHTED_RED) continue for module_path in self.get_modules( test_path, module_regex): try: loaded_module = self.load_module(module_path) except ImportError: self.print_traceback() continue try: suite = self.build_suite(loaded_module, method_regex, cl_args.tags, cl_args.supress_flag) master_suite.addTests(suite) test_classes.append(suite) except ImportError: self.print_traceback() continue except AttributeError: self.print_traceback() continue except Exception: self.print_traceback() continue if cl_args.parallel: unittest.installHandler() threads = [] results = [] start = time.time() for test in test_classes: t = ThreadedRunner(test_runner, test, results) t.start() threads.append(t) for t in threads: t.join() finish = time.time() print '=' * 71 print 'Tests Complete.' print '=' * 71 run = 0 errors = 0 failures = 0 for result in results: run += result.testsRun errors += len(result.errors) failures += len(result.failures) print("Ran %d test%s in %.3fs" % (run, run != 1 and "s" or "", finish - start)) if failures: print("Failures=%d" % failures) if errors: print("Errors=%d" % errors) if failures or errors: exit(1) else: unittest.installHandler() start_time = time.time() result = test_runner.run(master_suite) total_execution_time = time.time() - start_time if cl_args.generateXML is not None: xml_path = ''.join([parent_path, cl_args.generateXML]) parse_res = ParseResult(vars(result), master_suite, xml_path, total_execution_time) parse_res.generate_xml_report() self.log_results(result) if not result.wasSuccessful(): exit(1)
def run(self): """ loops through all the packages, modules, and methods sent in from the command line and runs them """ test_classes = [] cl_args = self.get_cl_args() module_regex = None if os.path.exists(BASE_DIR) is False: err_msg = self.error_msg("{0} does not exist - Exiting". format(BASE_DIR)) print err_msg exit(1) if cl_args.module is None: module_regex = '*.py' else: if cl_args.module.find('.py') != -1: module_regex = cl_args.module else: module_regex = '.'.join([cl_args.module, 'py']) if cl_args.method_regex is None: method_regex = 'test_*' else: if cl_args.method_regex.find('test_') != -1: method_regex = cl_args.method_regex else: method_regex = ''.join(['test_', cl_args.method_regex]) parent_path = BASE_DIR config_path = self.get_config_path(parent_path, cl_args.product, cl_args.config) repo_path = self.get_repo_path(cl_args.product) if os.path.exists(repo_path) is False: err_msg = self.error_msg('Repo', ' '.join([cl_args.product, repo_path, 'does not exist - Exiting'])) print err_msg exit(1) if cl_args.list is not None: arg_list = [] if not cl_args.list: arg_list = ['tests', 'configs'] else: arg_list = cl_args.list for arg in arg_list: if arg == 'tests': banner = '\n<[TEST REPO]>\n' path = repo_path else: banner = '\n<[CONFIGS]>\n' path = os.path.join( parent_path, 'configs', cl_args.product) print banner self.tree(path, ' ', print_files=True) else: suite = unittest.TestSuite() master_suite = unittest.TestSuite() # Use the parallel text runner so the console logs look correct if cl_args.parallel: test_runner = CCParallelTextTestRunner(verbosity=cl_args.quiet) else: test_runner = unittest.TextTestRunner(verbosity=cl_args.quiet) test_runner.failfast = cl_args.fail_fast try: stats_log_path = '{}/{}/{}/statistics'.format( LOG_BASE_PATH, cl_args.product, cl_args.config) product_log_path = '{}/{}/{}/{}'.format( LOG_BASE_PATH, cl_args.product, cl_args.config, self.get_safe_file_date()) except TypeError: print 'Config was not set on command line - Exiting' exit(1) if os.path.isdir(stats_log_path) is not True: os.makedirs(stats_log_path) if os.path.isdir(product_log_path) is not True: os.makedirs(product_log_path) #Get and then ensure the existence of the cc data directory data_dir = None user_data_dir = getattr(cl_args, 'data_directory', None) if user_data_dir is not None: #Quit if the data directory is user-defined and doesn't exist, #otherwise it uses the user defined data dir user_data_dir = os.path.expanduser(user_data_dir) if os.path.isdir(user_data_dir): data_dir = user_data_dir else: print "Data directory '{0}' does not exist. Exiting." \ .format(user_data_dir) exit(1) else: #Make and use the default directory if it doesn't exist """ @TODO: Make this create a sub-directory based on the product name like the log_dir does (minus timestamps and config file name) """ data_dir = DATA_DIR if not os.path.isdir(data_dir): os.makedirs(data_dir) self.set_env(config_path, product_log_path, data_dir) if cl_args.packages is None: for module in self.get_modules(repo_path, module_regex): try: loaded_module = self.load_module(module) except ImportError: self.print_traceback() continue try: suite = self.build_suite(loaded_module, method_regex, cl_args.tags, cl_args.supress_flag) master_suite.addTests(suite) test_classes.append(suite) except ImportError: self.print_traceback() continue except AttributeError: self.print_traceback() continue except Exception: self.print_traceback() continue else: for package_name in cl_args.packages: test_path = self.find_subdir(repo_path, package_name) if test_path is None: err_msg = self.error_msg('Package', package_name) print err_msg continue for module_path in self.get_modules(test_path, module_regex): try: loaded_module = self.load_module(module_path) except ImportError: self.print_traceback() continue try: suite = self.build_suite(loaded_module, method_regex, cl_args.tags, cl_args.supress_flag) master_suite.addTests(suite) test_classes.append(suite) except ImportError: self.print_traceback() continue except AttributeError: self.print_traceback() continue except Exception: self.print_traceback() continue if cl_args.parallel: unittest.installHandler() processes = [] manager = Manager() results = manager.list() start = time.time() for test in test_classes: proc = Process(target=execute_test, args=(test_runner, test, results)) processes.append(proc) proc.start() for proc in processes: proc.join() finish = time.time() print '-' * 71 run = 0 errors = 0 failures = 0 for result in results: run += result.testsRun errors += len(result.errors) failures += len(result.failures) print ("Ran %d test%s in %.3fs" % (run, run != 1 and "s" or "", finish - start)) fail = '' error = '' space = '' if failures: fail = "Failures=%d" % failures if errors: error = "Errors=%d" % errors if failures or errors: if failures and errors: space = ' ' print print 'FAILED ({}{}{})'.format(fail, space, error) exit(1) else: unittest.installHandler() start_time = time.time() result = test_runner.run(master_suite) total_execution_time = time.time() - start_time if cl_args.generateXML is not None: xml_path = ''.join([parent_path, cl_args.generateXML]) parse_res = ParseResult(vars(result), master_suite, xml_path, total_execution_time) parse_res.generate_xml_report() log_results(result) if not result.wasSuccessful(): exit(1)
def run(self): """ loops through all the packages, modules, and methods sent in from the command line and runs them """ test_classes = [] cl_args = self.get_cl_args() module_regex = None if os.path.exists(BASE_DIR) is False: err_msg = self.error_msg("{0} does not exist - Exiting".format(BASE_DIR)) print self.colorize(err_msg, HIGHLIGHTED_RED) exit(1) if cl_args.module is None: module_regex = "*.py" else: if cl_args.module.find(".py") != -1: module_regex = cl_args.module else: module_regex = ".".join([cl_args.module, "py"]) if cl_args.method_regex is None: method_regex = "test_*" else: if cl_args.method_regex.find("test_") != -1: method_regex = cl_args.method_regex else: method_regex = "".join(["test_", cl_args.method_regex]) parent_path = BASE_DIR config_path = self.get_config_path(parent_path, cl_args.product, cl_args.config) repo_path = self.get_repo_path(cl_args.product) if os.path.exists(repo_path) is False: err_msg = self.error_msg("Repo", " ".join([cl_args.product, repo_path, "does not exist - Exiting"])) print self.colorize(err_msg, HIGHLIGHTED_RED) exit(1) if cl_args.list is not None: for arg in cl_args.list: if arg == "tests": banner = "".join(["\n", "<[TEST REPO]>", "\n"]) path = repo_path else: banner = "".join(["\n", "<[CONFIGS]>", "\n"]) path = os.path.join(parent_path, "configs", cl_args.product) print self.colorize(banner, WHITE) self.tree(path, " ", print_files=True) else: suite = unittest.TestSuite() master_suite = unittest.TestSuite() # Use the parallel runner if needed so the console logs look correct if cl_args.parallel: test_runner = CCParallelTextTestRunner(verbosity=cl_args.quiet) else: test_runner = unittest.TextTestRunner(verbosity=cl_args.quiet) test_runner.failfast = cl_args.fail_fast # -----------------------Debug Logger----------------------------- # this is for the debug logger. its breaking right now # test_runner = LogCaptureRunner(verbosity=cl_args.quiet) # -----------------------Debug Logger----------------------------- try: stats_log_path = "/".join([LOG_BASE_PATH, cl_args.product, cl_args.config, "statistics"]) product_log_path = "/".join([LOG_BASE_PATH, cl_args.product, cl_args.config, self.get_safe_file_date()]) except TypeError: print "Config was not set on command line - Exiting" exit(1) if os.path.isdir(stats_log_path) is not True: os.makedirs(stats_log_path) if os.path.isdir(product_log_path) is not True: os.makedirs(product_log_path) # Get and then ensure the existence of the cc data directory data_dir = None user_data_dir = getattr(cl_args, "data_directory", None) if user_data_dir is not None: # Quit if the data directory is user-defined and doesn't exist, # otherwise it uses the user defined data dir user_data_dir = os.path.expanduser(user_data_dir) if os.path.isdir(user_data_dir): data_dir = user_data_dir else: print "Data directory '{0}' does not exist. Exiting.".format(user_data_dir) exit(1) else: # Make and use the default directory if it doesn't exist """ @TODO: Make this create a sub-directory based on the product name like the log_dir does (minus timestamps and config file name) """ data_dir = DATA_DIR if not os.path.isdir(data_dir): os.makedirs(data_dir) self.set_env(config_path, product_log_path, data_dir) if cl_args.packages is None: for module in self.get_modules(repo_path, module_regex): try: loaded_module = self.load_module(module) except ImportError: self.print_traceback() continue try: suite = self.build_suite(loaded_module, method_regex, cl_args.tags, cl_args.supress_flag) master_suite.addTests(suite) test_classes.append(suite) except ImportError: self.print_traceback() continue except AttributeError: self.print_traceback() continue except Exception: self.print_traceback() continue else: for package_name in cl_args.packages: test_path = self.find_subdir(repo_path, package_name) if test_path is None: err_msg = self.error_msg("Package", package_name) print self.colorize(err_msg, HIGHLIGHTED_RED) continue for module_path in self.get_modules(test_path, module_regex): try: loaded_module = self.load_module(module_path) except ImportError: self.print_traceback() continue try: suite = self.build_suite(loaded_module, method_regex, cl_args.tags, cl_args.supress_flag) master_suite.addTests(suite) test_classes.append(suite) except ImportError: self.print_traceback() continue except AttributeError: self.print_traceback() continue except Exception: self.print_traceback() continue if cl_args.parallel: unittest.installHandler() processes = [] manager = Manager() results = manager.list() start = time.time() for test in test_classes: p = Process(target=execute_test, args=(test_runner, test, results)) processes.append(p) p.start() for p in processes: p.join() finish = time.time() print "=" * 71 print "Tests Complete." print "=" * 71 run = 0 errors = 0 failures = 0 for result in results: run += result.testsRun errors += len(result.errors) failures += len(result.failures) print ("Ran %d test%s in %.3fs" % (run, run != 1 and "s" or "", finish - start)) if failures: print ("Failures=%d" % failures) if errors: print ("Errors=%d" % errors) if failures or errors: exit(1) else: unittest.installHandler() start_time = time.time() result = test_runner.run(master_suite) total_execution_time = time.time() - start_time if cl_args.generateXML is not None: xml_path = "".join([parent_path, cl_args.generateXML]) parse_res = ParseResult(vars(result), master_suite, xml_path, total_execution_time) parse_res.generate_xml_report() self.log_results(result) if not result.wasSuccessful(): exit(1)
def optparse_callback_catch_keyboard_interrupt(*args, **kwargs): unittest.installHandler()