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 test_run_sql_test_template(self): test_case = MockSQLConcurrencyTestCaseTemplate('test_template_query02') test_case.__class__.__unittest_skip__ = False test_result = unittest.TestResult() # Cleanup if os.path.exists(test_case.get_out_dir()): for file in os.listdir(test_case.get_out_dir()): if fnmatch.fnmatch(file, 'template_query02**.out'): os.remove(os.path.join(test_case.get_out_dir(), file)) test_case.run(test_result) self.assertEqual(test_result.testsRun, 1) self.assertEqual(len(test_result.errors), 0) self.assertEqual(len(test_result.skipped), 0) self.assertEqual(len(test_result.failures), 0) count = 0 for file in os.listdir(test_case.get_out_dir()): if fnmatch.fnmatch(file, 'template_query02*.out'): count = count + 1 self.assertEqual(count, 12) # Cleanup if os.path.exists(test_case.get_out_dir()): for file in os.listdir(test_case.get_out_dir()): path = os.path.join(test_case.get_out_dir(), file) if fnmatch.fnmatch(file, 'template_query02*.*'): os.remove(os.path.join(test_case.get_out_dir(), file)) if fnmatch.fnmatch(file, 'regress_sql_concurrency_test_case'): shutil.rmtree(path)
def test_discover_with_init_module_that_raises_SkipTest_on_import(self): vfs = { abspath('/foo'): ['my_package'], abspath('/foo/my_package'): ['__init__.py', 'test_module.py'] } self.setup_import_issue_package_tests(vfs) import_calls = [] def _get_module_from_name(name): import_calls.append(name) raise unittest.SkipTest('skipperoo') loader = unittest.TestLoader() loader._get_module_from_name = _get_module_from_name suite = loader.discover(abspath('/foo')) self.assertIn(abspath('/foo'), sys.path) self.assertEqual(suite.countTestCases(), 1) result = unittest.TestResult() suite.run(result) self.assertEqual(len(result.skipped), 1) self.assertEqual(result.testsRun, 1) self.assertEqual(import_calls, ['my_package']) # Check picklability for proto in range(pickle.HIGHEST_PROTOCOL + 1): pickle.loads(pickle.dumps(suite, proto))
def _do_test(self): test = request.params.get('test', None) server = str(request.params.get('server', None)) success = False if test is None: abort(400) suite = unittest2.TestSuite() suite.addTest(inpho.tests.Autotest(test, server)) result = unittest2.TestResult() suite.run(result) current_fails = len(result.errors) + len(result.failures) if current_fails == 0: response.status = 200 return test else: message = 'We have issues. ' if result.errors: message += "ERRORS: " message += ' '.join( [" ::: " + error[1] for error in result.errors]) if result.failures: message += "FAILURES: " message += ' '.join( [" ::: " + fail[1] for fail in result.failures]) response.status = 500 return message
def test_run_sql_test_failure_with_gpdiff(self): test_case = MockSQLConcurrencyTestCase('test_query03') # As explained above, we want MockSQLTestCase to run if and only if # it's being invoked by our unit tests. So, it's skipped if discovered # directly by unit2. Here, bearing in mind that SQLConcurrencyTestCaseTests is itself # triggered by unit2, we override MockSQLTestCase's skip decorator to allow # this explicit construction of MockSQLTestCase to proceed. test_case.__class__.__unittest_skip__ = False test_result = unittest.TestResult() current_dir = os.path.dirname(inspect.getfile(test_case.__class__)) i = 1 for file in os.listdir(test_case.get_sql_dir()): if fnmatch.fnmatch(file, 'query03_part*.sql'): i += 1 self.assertTrue(test_case.gpdiff) test_case.run(test_result) self.assertEqual(test_result.testsRun, 1) self.assertEqual(len(test_result.errors), 0) self.assertEqual(len(test_result.skipped), 0) self.assertEqual(len(test_result.failures), 1) # Cleanup if os.path.exists(test_case.get_out_dir()): for file in os.listdir(test_case.get_out_dir()): if fnmatch.fnmatch(file, 'query03*.out'): os.remove(os.path.join(test_case.get_out_dir(), file))
def test_stopTest(self): class Foo(unittest2.TestCase): def test_1(self): pass test = Foo('test_1') result = unittest2.TestResult() result.startTest(test) self.assertTrue(result.wasSuccessful()) self.assertEqual(len(result.errors), 0) self.assertEqual(len(result.failures), 0) self.assertEqual(result.testsRun, 1) self.assertEqual(result.shouldStop, False) result.stopTest(test) # Same tests as above; make sure nothing has changed self.assertTrue(result.wasSuccessful()) self.assertEqual(len(result.errors), 0) self.assertEqual(len(result.failures), 0) self.assertEqual(result.testsRun, 1) self.assertEqual(result.shouldStop, False)
def test_addError(self): class Foo(unittest2.TestCase): def test_1(self): pass test = Foo('test_1') try: raise TypeError() except: exc_info_tuple = sys.exc_info() result = unittest2.TestResult() result.startTest(test) result.addError(test, exc_info_tuple) result.stopTest(test) self.assertFalse(result.wasSuccessful()) self.assertEqual(len(result.errors), 1) self.assertEqual(len(result.failures), 0) self.assertEqual(result.testsRun, 1) self.assertEqual(result.shouldStop, False) test_case, formatted_exc = result.errors[0] self.assertIs(test_case, test) self.assertIsInstance(formatted_exc, str)
def testSystemExit(self): def _raise(self=None): raise SystemExit def nothing(self): pass class Test1(unittest2.TestCase): test_something = _raise class Test2(unittest2.TestCase): setUp = _raise test_something = nothing class Test3(unittest2.TestCase): test_something = nothing tearDown = _raise class Test4(unittest2.TestCase): def test_something(self): self.addCleanup(_raise) for klass in (Test1, Test2, Test3, Test4): result = unittest2.TestResult() klass('test_something').run(result) self.assertEqual(len(result.errors), 1) self.assertEqual(result.testsRun, 1)
def testRunnerRegistersResult(self): class Test(unittest2.TestCase): def testFoo(self): pass originalRegisterResult = unittest2.runner.registerResult def cleanup(): unittest2.runner.registerResult = originalRegisterResult self.addCleanup(cleanup) result = unittest2.TestResult() runner = unittest2.TextTestRunner(stream=StringIO()) # Use our result object runner._makeResult = lambda: result self.wasRegistered = 0 def fakeRegisterResult(thisResult): self.wasRegistered += 1 self.assertEqual(thisResult, result) unittest2.runner.registerResult = fakeRegisterResult runner.run(unittest2.TestSuite()) self.assertEqual(self.wasRegistered, 1)
def test_run_sql_test_success(self): test_case = MockSQLPerformanceTestCase('test_query02') # As explained above, we want MockSQLTestCase to run if and only if # it's being invoked by our unit tests. So, it's skipped if discovered # directly by unit2. Here, bearing in mind that SQLTestCaseTests is itself # triggered by unit2, we override MockSQLTestCase's skip decorator to allow # this explicit construction of MockSQLTestCase to proceed. test_case.__class__.__unittest_skip__ = False self.assertEqual(test_case.name, "MockSQLPerformanceTestCase.test_query02") self.assertEqual(test_case.author, 'kumara64') self.assertEqual(test_case.description, 'test sql test case') self.assertEqual( test_case.created_datetime, datetime.strptime('2012-07-05 12:00:00', '%Y-%m-%d %H:%M:%S')) self.assertEqual( test_case.modified_datetime, datetime.strptime('2012-07-08 12:00:02', '%Y-%m-%d %H:%M:%S')) self.assertEqual(test_case.tags, set(['orca', 'hashagg', 'executor'])) self.assertEqual(test_case.repetitions, 3) self.assertEqual(test_case.threshold, 10) test_result = unittest.TestResult() test_case.run(test_result) self.assertEqual(test_result.testsRun, 1) self.assertEqual(len(test_result.errors), 0) self.assertEqual(len(test_result.skipped), 0) self.assertEqual(len(test_result.failures), 0)
def testSkippingEverywhere(self): def _skip(self=None): raise unittest2.SkipTest('some reason') def nothing(self): pass class Test1(unittest2.TestCase): test_something = _skip class Test2(unittest2.TestCase): setUp = _skip test_something = nothing class Test3(unittest2.TestCase): test_something = nothing tearDown = _skip class Test4(unittest2.TestCase): def test_something(self): self.addCleanup(_skip) for klass in (Test1, Test2, Test3, Test4): result = unittest2.TestResult() klass('test_something').run(result) self.assertEqual(len(result.skipped), 1) self.assertEqual(result.testsRun, 1)
def test_run_sql_force_generate_ans_permission_denied(self): test_loader = tinctest.TINCTestLoader() test_suite = test_loader.loadTestsFromTestCase( MockSQLTestCaseForceGenerateAns) # Find our desired test case in test_suite. # This code is a consequence of us only having implemented # loadTestsFromTestCase. An implementation of loadTestsFromNames # would likely have allowed us to insolate test_query04 directly. test_case = None for temp in test_suite._tests: if temp.name == "MockSQLTestCaseForceGenerateAns.test_query04": # query04.ans wouldn't be checked-out from perforce, so it would have no write operation allowed test_case = temp self.assertIsNotNone(test_case) # As explained above, we want MockSQLTestCase to run if and only if # it's being invoked by our unit tests. So, it's skipped if discovered # directly by unit2. Here, bearing in mind that SQLTestCaseTests is itself # triggered by unit2, we override MockSQLTestCase's skip decorator to allow # this explicit construction of MockSQLTestCase to proceed. test_case.__class__.__unittest_skip__ = False test_result = unittest.TestResult() test_case.run(test_result) self.assertEqual(test_result.testsRun, 1) self.assertEqual(len(test_result.errors), 1) self.assertEqual(len(test_result.skipped), 0) self.assertEqual(len(test_result.failures), 0)
def test_addSubTest(self): log = [] class Foo(unittest.TestCase): def test_1(self): with self.subTest(foo=1): subtest = self._subtest log.append(subtest) try: 1 / 0 except ZeroDivisionError: exc_info_tuple = sys.exc_info() # Register an error by hand (to check the API) result.addSubTest(test, subtest, exc_info_tuple) # Now trigger a failure self.fail("some recognizable failure") test = Foo('test_1') result = unittest.TestResult() test.run(result) self.assertFalse(result.wasSuccessful()) self.assertEqual(len(result.errors), 1) self.assertEqual(len(result.failures), 1) self.assertEqual(result.testsRun, 1) self.assertEqual(result.shouldStop, False) test_case, formatted_exc = result.errors[0] subtest = log[0] self.assertIs(test_case, subtest) self.assertIn("ZeroDivisionError", formatted_exc) test_case, formatted_exc = result.failures[0] self.assertIs(test_case, subtest) self.assertIn("some recognizable failure", formatted_exc)
def test_gpdiff_no_ans_file(self): """ Test whether we throw an excpetion when there is no ans file for a sql file and if gpdiff is set to True """ test_loader = tinctest.TINCTestLoader() test_suite = test_loader.loadTestsFromTestCase( MockSQLTestCaseGpdiffNoAnsFile) # Find our desired test case in test_suite. # This code is a consequence of us only having implemented # loadTestsFromTestCase. An implementation of loadTestsFromNames # would likely have allowed us to insolate test_query02 directly. test_case = None for temp in test_suite._tests: if temp.name == "MockSQLTestCaseGpdiffNoAnsFile.test_query_no_ans_file": test_case = temp self.assertIsNotNone(test_case) # As explained above, we want MockSQLTestCase to run if and only if # it's being invoked by our unit tests. So, it's skipped if discovered # directly by unit2. Here, bearing in mind that SQLTestCaseTests is itself # triggered by unit2, we override MockSQLTestCase's skip decorator to allow # this explicit construction of MockSQLTestCase to proceed. test_case.__class__.__unittest_skip__ = False test_result = unittest.TestResult() test_case.run(test_result) self.assertEqual(test_result.testsRun, 1) self.assertEqual(len(test_result.errors), 1) self.assertEqual(len(test_result.skipped), 0) self.assertEqual(len(test_result.failures), 0)
def test_run_sql_test_success(self): test_loader = tinctest.TINCTestLoader() test_suite = test_loader.loadTestsFromTestCase(MockSQLTestCase) # Find our desired test case in test_suite. # This code is a consequence of us only having implemented # loadTestsFromTestCase. An implementation of loadTestsFromNames # would likely have allowed us to insolate test_query02 directly. test_case = None for temp in test_suite._tests: if temp.name == "MockSQLTestCase.test_query03": test_case = temp self.assertIsNotNone(test_case) # As explained above, we want MockSQLTestCase to run if and only if # it's being invoked by our unit tests. So, it's skipped if discovered # directly by unit2. Here, bearing in mind that SQLTestCaseTests is itself # triggered by unit2, we override MockSQLTestCase's skip decorator to allow # this explicit construction of MockSQLTestCase to proceed. test_case.__class__.__unittest_skip__ = False test_result = unittest.TestResult() test_case.run(test_result) self.assertEqual(test_result.testsRun, 1) self.assertEqual(len(test_result.errors), 0) self.assertEqual(len(test_result.skipped), 0) self.assertEqual(len(test_result.failures), 0) shutil.rmtree(test_case.get_out_dir())
def test_init__empty_tests(self): suite = unittest2.TestSuite([]) self.assertEqual(suite.countTestCases(), 0) # countTestCases() still works after tests are run suite.run(unittest.TestResult()) self.assertEqual(suite.countTestCases(), 0)
def test_pre_defined_transform_files_exist(self): """ Test whether the default transformations are applied correctly to a sql template case """ test_loader = tinctest.TINCTestLoader() test_suite = test_loader.loadTestsFromTestCase(MockSQLTemplateTestCase) test_case = None for temp in test_suite._tests: if temp.name == "MockSQLTemplateTestCase.test_pre_defined_transform": test_case = temp self.assertIsNotNone(test_case) test_case.__class__.__unittest_skip__ = False test_result = unittest.TestResult() test_case.run(test_result) self.assertEqual(test_result.testsRun, 1) self.assertEqual(len(test_result.failures), 0) self.assertEqual(len(test_result.errors), 0) self.assertTrue( os.path.exists(local_path("pre_defined_transform.sql.t"))) self.assertTrue( os.path.exists(local_path("pre_defined_transform.ans.t"))) schema = "%schema%" with open(local_path("pre_defined_transform.sql.t"), 'r') as input: for line in input.readlines(): if schema in line: self.assertTrue(False) with open(local_path("pre_defined_transform.ans.t"), 'r') as input: for line in input.readlines(): if schema in line: self.assertTrue(False) os.remove(local_path("pre_defined_transform.sql.t")) os.remove(local_path("pre_defined_transform.ans.t"))
def test_generator_test_case_derived_count(self): suite = unittest.TestLoader().loadTestsFromTestCase( self.DerivedExample) result = unittest.TestResult() suite.run(result) self.assertEqual(result.testsRun, 2 * len(self.inputs)) self.assertEqual(self.spy.call_count, len(self.inputs)) self.assertEqual(self.other_spy.call_count, len(self.inputs))
def test_function_in_suite(self): def f(_): pass suite = unittest2.TestSuite() suite.addTest(f) # when the bug is fixed this line will not crash suite.run(unittest2.TestResult())
def defaultTestResult(self, stream=None, descriptions=None, verbosity=None): if stream and descriptions and verbosity: return CustomTestCaseResult(stream, descriptions, verbosity) else: return unittest.TestResult()
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 test_run_all_without_opt_mode(self): test_loader = tinctest.TINCTestLoader() test_suite = test_loader.loadTestsFromTestCase(MockTestDifferentAnswerFilesWithoutOptMode) for tc in test_suite._tests: tc.__class__.__unittest_skip__ = False test_result = unittest.TestResult() tc.run(test_result) self.assertEqual(len(test_result.errors), 0, 'test case failed: %s' %tc) self.assertEqual(len(test_result.failures), 0, 'test case failed: %s' %tc)
def test_explicit_test_method(self): test_case = MockSQLConcurrencyTestCase('test_00000') test_case.__class__.__unittest_skip__ = False test_result = unittest.TestResult() test_case.run(test_result) self.assertEqual(test_result.testsRun, 1) self.assertEqual(len(test_result.errors), 0) self.assertEqual(len(test_result.skipped), 0) self.assertEqual(len(test_result.failures), 0)
def testStackFrameTrimming(self): class Frame(object): class tb_frame(object): f_globals = {} result = unittest2.TestResult() self.assertFalse(result._is_relevant_tb_level(Frame)) Frame.tb_frame.f_globals['__unittest'] = True self.assertTrue(result._is_relevant_tb_level(Frame))
def testFailFast(self): result = unittest2.TestResult() result._exc_info_to_string = lambda *_: '' result.failfast = True result.addError(None, None) self.assertTrue(result.shouldStop) result = unittest2.TestResult() result._exc_info_to_string = lambda *_: '' result.failfast = True result.addFailure(None, None) self.assertTrue(result.shouldStop) result = unittest2.TestResult() result._exc_info_to_string = lambda *_: '' result.failfast = True result.addUnexpectedSuccess(None) self.assertTrue(result.shouldStop)
def test_countTestCases_simple(self): test1 = unittest2.FunctionTestCase(lambda: None) test2 = unittest2.FunctionTestCase(lambda: None) suite = unittest2.TestSuite((test1, test2)) self.assertEqual(suite.countTestCases(), 2) # countTestCases() still works after tests are run suite.run(unittest.TestResult()) self.assertEqual(suite.countTestCases(), 2)
def test_run_sql_test_optimizer_both(self): test_loader = tinctest.TINCTestLoader() # For data provider test cases, we have to use loadTestsFromName, since loadTestsFromTestCase won't filter and expand test_suite = test_loader.loadTestsFromName( "mpp.models.regress.sql_related.regress_sql_test_case.regress_sql_test_case.MockSQLTestCaseWithOptimizerBoth" ) # Find our desired test case in test_suite. test_case = None new_test_suite = tinctest.TINCTestSuite() for temp in test_suite._tests: if "MockSQLTestCaseWithOptimizerBoth.test_query03" in temp.name: new_test_suite.addTest(temp) temp.__class__.__unittest_skip__ = False test_case = temp self.assertIsNotNone(new_test_suite) self.assertEquals(new_test_suite.countTestCases(), 2) test_result = unittest.TestResult() new_test_suite.run(test_result) self.assertEqual(test_result.testsRun, 2) self.assertEqual(len(test_result.errors), 0) self.assertEqual(len(test_result.skipped), 0) self.assertEqual(len(test_result.failures), 0) self.assertTrue( os.path.exists( os.path.join(test_case.get_out_dir(), 'query03_planner.sql'))) self.assertTrue( os.path.exists( os.path.join(test_case.get_out_dir(), 'query03_planner.out'))) self.assertTrue( self._check_str_in_file( "SET optimizer=off;", os.path.join(temp.get_out_dir(), 'query03_planner.sql'))) self.assertTrue( self._check_str_in_file( "SET optimizer=off;", os.path.join(test_case.get_out_dir(), 'query03_planner.out'))) self.assertTrue( os.path.exists( os.path.join(test_case.get_out_dir(), 'query03_orca.sql'))) self.assertTrue( os.path.exists( os.path.join(test_case.get_out_dir(), 'query03_orca.out'))) self.assertTrue( self._check_str_in_file( "SET optimizer=on;", os.path.join(test_case.get_out_dir(), 'query03_orca.sql'))) self.assertTrue( self._check_str_in_file( "SET optimizer=on;", os.path.join(test_case.get_out_dir(), 'query03_orca.out'))) shutil.rmtree(test_case.get_out_dir())
def test_sanity_run(self): tinc_test_case = MockSQLScenaioTestCase('test_serial_execution') tinc_test_case.__class__.__unittest_skip__ = False result = unittest.TestResult() tinc_test_case.run(result) self.assertEqual(len(tinc_test_case.test_case_scenario), 2) self.assertEqual(len(tinc_test_case.test_case_scenario[0][0]), 2) self.assertEqual(len(tinc_test_case.test_case_scenario[1][0]), 2) self.assertEqual(len(result.failures), 0) self.assertEqual(len(result.errors), 0)
def test_init(self): result = unittest2.TestResult() self.assertTrue(result.wasSuccessful()) self.assertEqual(len(result.errors), 0) self.assertEqual(len(result.failures), 0) self.assertEqual(result.testsRun, 0) self.assertEqual(result.shouldStop, False) self.assertIsNone(result._stdout_buffer) self.assertIsNone(result._stderr_buffer)
def testRegisterResult(self): result = unittest2.TestResult() unittest2.registerResult(result) for ref in unittest2.signals._results: if ref is result: break elif ref is not result: self.fail("odd object in result set") else: self.fail("result not found")