def _run_suites_concurrently(self, max_workers_on_suites): exit_code = 0 suites = unishark.DefaultTestLoader( method_prefix=self.method_prefix).load_test_from_dict( self.test_dict_conf) start_time = time.time() with concurrent.futures.ThreadPoolExecutor( max_workers_on_suites) as executor: futures = [] for suite_name, suite_content in suites.items(): package_name = suite_content['package'] suite = suite_content['suite'] max_workers = suite_content['max_workers'] runner = unishark.BufferedTestRunner( reporters=self.reporters, verbosity=self.verbosity, descriptions=self.descriptions) future = executor.submit(runner.run, suite, name=suite_name, description='Package: ' + package_name, max_workers=max_workers) futures.append(future) for future in concurrent.futures.as_completed(futures): result = future.result() exit_code += 0 if result.wasSuccessful() else 1 actual_duration = time.time() - start_time log.info('Actual total time taken: %.3fs' % actual_duration) for reporter in self.reporters: reporter.set_actual_duration(actual_duration) reporter.collect() return exit_code
def test_single_runner_with_multithreads(self): self.suite = self.loader.loadTestsFromNames([ 'tests.test_result.MyTestClass1.test_1', 'tests.test_result.MyTestClass1.test_2', 'tests.test_result.MyTestClass2.test_1', 'tests.test_result.MyTestClass2.test_3' ]) result = unishark.BufferedTestRunner(verbosity=0).run( self.suite, max_workers=4, concurrency_level='method') print(result.results) self.assertEqual(result.testsRun, 4) self.assertEqual(result.successes, 2) self.assertEqual(len(result.failures), 2) self.assertEqual(len(result.errors), 0) self.assertFalse(result.wasSuccessful()) self.assertEqual( result.results['test_result']['test_result.MyTestClass2'][0][4], 'INFO: 0 x 0 = 0\nINFO: 0 x 1 = 0\nINFO: 1 x 0 = 0\nINFO: 1 x 1 = 1\n' ) self.assertEqual( result.results['test_result']['test_result.MyTestClass2'][0][5], 'No Exception\n') self.assertEqual( result.results['test_result']['test_result.MyTestClass1'][1][4], 'INFO: Here is log of test_2\n') self.assertIn( 'AssertionError: 2 != 1', result.results['test_result']['test_result.MyTestClass1'][1][5])
def test_html_report(self): reporter = unishark.HtmlReporter() result = unishark.BufferedTestRunner().run( self.suite1, name='My Suite 1', description=u'Description 1: \n\t<~!@#$%^&*()_+=-?/>,;."\u6c49"\r\n' ) reporter.report(result) result = unishark.BufferedTestRunner().run( self.suite2, name='My Suite 2', description=u'Description 2: \n\t<~!@#$%^&*()_+=-?/>,;."\u6c49"\r\n' ) reporter.report(result) reporter.collect() exp_filenames = [ 'index.html', 'overview.html', 'My Suite 1_result.html', 'My Suite 2_result.html' ] filenames = os.listdir(self.dest) self.assertSetEqual(set(filenames), set(exp_filenames))
def test_init_with_wrong_reporter_type(self): class MyReporter(): def __init__(self): pass def report(self, result): pass def collect(self): pass unishark.BufferedTestRunner(reporters=[MyReporter()])
def test_init_with_wrong_reporter_type(self): class MyReporter(): def __init__(self): pass def report(self, result): pass def collect(self): pass with self.assertRaises(TypeError): unishark.BufferedTestRunner(reporters=[MyReporter()])
def setUp(self): super(RunnerTestCase, self).setUp() self.loader = unittest.TestLoader() self.suite = None self.runner = unishark.BufferedTestRunner(verbosity=0) self.__context__ = 'tests.mock3' unishark.contexts.set(self.__context__, []) self.mod1_name = 'test_concur1' self.mod2_name = 'test_concur2' self.cls11_name = 'test_concur1.Class1' self.cls12_name = 'test_concur1.Class2' self.cls21_name = 'test_concur2.Class1' self.cls22_name = 'test_concur2.Class2'
def test_buffered_result(self): result = unishark.BufferedTestRunner(verbosity=0).run(self.suite) self.assertEqual(result.successes, 1) self.assertEqual(len(result.skipped), 1) self.assertEqual(len(result.failures), 1) self.assertEqual(len(result.errors), 1) self.assertEqual(len(result.expectedFailures), 1) self.assertEqual(len(result.unexpectedSuccesses), 1) self.assertEqual(result.testsRun, 6) results = result.results['test_result']['test_result.Mocking'] res_dict = dict() duration_sum = 0 for tup in results: res_dict[tup[0]] = tup[1:] duration_sum += tup[2] self.assertGreaterEqual(result.sum_duration, duration_sum) method_names = [ 'test_successes', 'test_skipped', 'test_errors', 'test_failures', 'test_expected_failures', 'test_unexpected_successes' ] method_names = list( map(lambda x: 'test_result.Mocking.' + x, method_names)) self.assertSetEqual(set(res_dict.keys()), set(method_names)) success_res = res_dict['test_result.Mocking.test_successes'] self.assertEqual( success_res[0], '\n This is doc string.\n Great.\n ') self.assertEqual(success_res[2], PASS) self.assertEqual(success_res[3], 'No Log\n') self.assertEqual(success_res[4], 'No Exception\n') skip_res = res_dict['test_result.Mocking.test_skipped'] self.assertEqual(skip_res[0], 'No Method Doc\n') self.assertEqual(skip_res[2], SKIPPED) self.assertEqual(skip_res[3], 'No Log\n') self.assertEqual(skip_res[4], "Skipped: ''") error_res = res_dict['test_result.Mocking.test_errors'] self.assertEqual(error_res[2], ERROR) self.assertIn('ValueError', error_res[4]) fail_res = res_dict['test_result.Mocking.test_failures'] self.assertEqual(fail_res[2], FAIL) self.assertEqual(fail_res[3], 'ERROR: A logging log.\n') self.assertIn('AssertionError', fail_res[4]) exp_fail_res = res_dict['test_result.Mocking.test_expected_failures'] self.assertEqual(exp_fail_res[2], EXPECTED_FAIL) self.assertIn('AssertionError', exp_fail_res[4]) unexp_success_res = res_dict[ 'test_result.Mocking.test_unexpected_successes'] self.assertEqual(unexp_success_res[2], UNEXPECTED_PASS) self.assertEqual(unexp_success_res[4], 'No Exception\n')
def test_junit_report(self): reporter = unishark.XUnitReporter() result = unishark.BufferedTestRunner().run( self.suite1, name='My Suite 1', description=u'Description 1: \n\t<~!@#$%^&*()_+=-?/>,;."\u6c49"\r\n' ) reporter.report(result) result = unishark.BufferedTestRunner().run( self.suite2, name='My Suite 2', description=u'Description 2: \n\t<~!@#$%^&*()_+=-?/>,;."\u6c49"\r\n' ) reporter.report(result) reporter.collect() exp_filenames = [ 'My Suite 1_xunit_result.xml', 'My Suite 2_xunit_result.xml', 'summary_xunit_result.xml' ] filenames = os.listdir(self.dest) self.assertSetEqual(set(filenames), set(exp_filenames)) for filename in filenames: minidom.parse(os.path.join(self.dest, filename))
def _run_suites_concurrently(self, concurrency_type, max_workers_on_suites, timeout): exit_code = 0 suites = unishark.DefaultTestLoader( name_pattern=self.name_pattern).load_tests_from_dict( self.test_dict_conf) if concurrency_type == 'processes': pool = concurrent.futures.ProcessPoolExecutor else: pool = concurrent.futures.ThreadPoolExecutor start_time = time.time() with pool(max_workers_on_suites) as executor: futures = [] for suite_name, suite_content in suites.items(): package_name = suite_content['package'] suite = suite_content['suite'] concurrency = suite_content['concurrency'] runner = unishark.BufferedTestRunner( reporters=self.reporters, verbosity=self.verbosity, descriptions=self.descriptions) future = executor.submit( runner.run, suite, name=suite_name, description='Package: ' + package_name, max_workers=concurrency['max_workers'], concurrency_level=concurrency['level'], timeout=concurrency['timeout']) futures.append(future) results = [ future.result() for future in concurrent.futures.as_completed(futures, timeout=timeout) ] actual_duration = time.time() - start_time log.info('Actual total time taken: %.3fs' % actual_duration) for result in results: exit_code += 0 if result.wasSuccessful() else 1 if len(self.reporters): log.info('Summarizing reports of suites.') start_time = time.time() with pool(len(self.reporters)) as exe: for reporter in self.reporters: reporter.set_actual_duration(actual_duration) exe.submit(reporter.collect) log.info('Took %.3fs to summarize reports.' % (time.time() - start_time)) return exit_code
def test_multi_runner_with_multithreads(self): suite1 = self.loader.loadTestsFromTestCase(MyTestClass1) suite2 = self.loader.loadTestsFromTestCase(MyTestClass2) from concurrent.futures import ThreadPoolExecutor with ThreadPoolExecutor(max_workers=2) as executor: f1 = executor.submit( unishark.BufferedTestRunner(verbosity=0).run, suite1) f2 = executor.submit( unishark.BufferedTestRunner(verbosity=0).run, suite2) r1 = f1.result() r2 = f2.result() print('suite1 result: %r' % r1.results) self.assertEqual(r1.testsRun, 2) self.assertEqual(r1.successes, 1) self.assertEqual(len(r1.failures), 1) self.assertEqual(len(r1.errors), 0) self.assertFalse(r1.wasSuccessful()) print('suite2 result: %r' % r2.results) self.assertEqual(r2.testsRun, 2) self.assertEqual(r2.successes, 1) self.assertEqual(len(r2.failures), 1) self.assertEqual(len(r2.errors), 0) self.assertFalse(r2.wasSuccessful())
def _run_suites_sequentially(self): exit_code = 0 suites = unishark.DefaultTestLoader( method_prefix=self.method_prefix).load_test_from_dict( self.test_dict_conf) runner = unishark.BufferedTestRunner(reporters=self.reporters, verbosity=self.verbosity, descriptions=self.descriptions) for suite_name, suite_content in suites.items(): package_name = suite_content['package'] suite = suite_content['suite'] max_workers = suite_content['max_workers'] result = runner.run(suite, name=suite_name, description='Package: ' + package_name, max_workers=max_workers) exit_code += 0 if result.wasSuccessful() else 1 for reporter in self.reporters: reporter.collect() return exit_code
def test_multi_reporters(self): self.dest = 'reports' reporter1 = unishark.HtmlReporter( overview_title='My Title', overview_description= u'My Description: : \n\t<~!@#$%^&*()_+=-?/>,;."\u6c49"' u'\r\n', dest=self.dest) reporter2 = unishark.XUnitReporter(summary_title='My Title', dest=self.dest) unishark.BufferedTestRunner(reporters=[reporter1, reporter2]).run( self.suite1) reporter1.collect() reporter2.collect() exp_filenames = [ 'test_xunit_result.xml', 'summary_xunit_result.xml', 'index.html', 'overview.html', 'test_result.html' ] filenames = os.listdir(os.path.join(self.dest)) self.assertSetEqual(set(filenames), set(exp_filenames))
def _run_suites_sequentially(self): exit_code = 0 suites = unishark.DefaultTestLoader( name_pattern=self.name_pattern).load_tests_from_dict( self.test_dict_conf) runner = unishark.BufferedTestRunner(reporters=self.reporters, verbosity=self.verbosity, descriptions=self.descriptions) for suite_name, suite_content in suites.items(): package_name = suite_content['package'] suite = suite_content['suite'] concurrency = suite_content['concurrency'] result = runner.run(suite, name=suite_name, description='Package: ' + package_name, max_workers=concurrency['max_workers'], concurrency_level=concurrency['level'], timeout=concurrency['timeout']) exit_code += 0 if result.wasSuccessful() else 1 for reporter in self.reporters: reporter.collect() return exit_code
'INFO: Here is log of test_2\n') self.assertIn( 'AssertionError: 2 != 1', result.results['test_runner']['test_runner.MyTestClass1'][1][5]) @unittest.expectedFailure def test_init_with_non_iterable_reporters(self): unishark.BufferedTestRunner(reporters=unishark.HtmlReporter()) @unittest.expectedFailure def test_init_with_wrong_reporter_type(self): class MyReporter(): def __init__(self): pass def report(self, result): pass def collect(self): pass unishark.BufferedTestRunner(reporters=[MyReporter()]) if __name__ == '__main__': suite = unittest.TestLoader().loadTestsFromTestCase(RunnerTestCase) rslt = unishark.BufferedTestRunner(verbosity=2).run(suite) print('Successful: %r' % rslt.wasSuccessful()) import sys sys.exit(0 if rslt.wasSuccessful() else 1)
def test_init_with_non_iterable_reporters(self): unishark.BufferedTestRunner(reporters=unishark.HtmlReporter())
sleep(1) self.assertEqual(1, 1) def test_9(self): """Here is test_9's doc str""" log.error('This is a failure case, which raises AssertionError') sleep(2) self.assertEqual(1, 2) @unittest.expectedFailure def test_10(self): """Here is test_10's doc str""" log.info('This is expected to be failed.') sleep(1) log.debug('Here is test_10 DEBUG log') raise ValueError if __name__ == '__main__': # unittest loader will load test_5, test_6, test_7 for MyTestClass4 since it inherits MyTestClass3, # while unishark does not load test methods inherited from super class. # Try each of the following suites: # suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__]) # suite = unishark.DefaultTestLoader().load_tests_from_package('example', regex='test_module2\.\w+\.test\w*') suite = unishark.DefaultTestLoader().load_tests_from_modules( ['example.test_module2'], regex='\w+4\.test\w*') reporter = unishark.HtmlReporter(dest='log') unishark.BufferedTestRunner(reporters=[reporter], verbosity=2).run(suite, name='mytest2', max_workers=2)
if kinesis_jar_present is True: testcases.append(KinesisStreamTests) elif are_kinesis_tests_enabled is False: sys.stderr.write("Skipping all Kinesis Python tests as the optional Kinesis project was " "not compiled into a JAR. To run these tests, " "you need to build Spark with 'build/sbt -Pkinesis-asl assembly/package " "streaming-kinesis-asl-assembly/assembly' or " "'build/mvn -Pkinesis-asl package' before running this test.") else: raise Exception( ("Failed to find Spark Streaming Kinesis assembly jar in %s. " % _kinesis_asl_assembly_dir()) + "You need to build Spark with 'build/sbt -Pkinesis-asl " "assembly/package streaming-kinesis-asl-assembly/assembly'" "or 'build/mvn -Pkinesis-asl package' before running this test.") sys.stderr.write("Running tests: %s \n" % (str(testcases))) failed = False for testcase in testcases: sys.stderr.write("[Running %s]\n" % (testcase)) tests = unittest.TestLoader().loadTestsFromTestCase(testcase) runner = unishark.BufferedTestRunner( verbosity=2, reporters=[unishark.XUnitReporter('target/test-reports/pyspark.streaming_{}'.format( os.path.basename(os.environ.get("PYSPARK_PYTHON", ""))))]) result = runner.run(tests) if not result.wasSuccessful(): failed = True sys.exit(failed)
self.assertEqual(1, 1) @unittest.skip('Reason of skipping test_7') def test_7(self): """Here is test_7's doc str""" self.assertEqual(1, 1) class MyTestClass4(unittest.TestCase): def test_8(self): """Here is test_8's doc str""" log.debug('There is an error') raise TypeError def test_9(self): """Here is test_9's doc str""" log.error('This is a failure case, which raises AssertionError') self.assertEqual(1, 2) def test_10(self): """Here is test_10's doc str""" log.info('Here is test_10 INFO log') log.debug('Here is test_10 DEBUG log') self.assertEqual(1, 1) if __name__ == '__main__': import unishark reporter = unishark.HtmlReporter(dest='log') unittest.main(testRunner=unishark.BufferedTestRunner([reporter]))
def test_15(self): """Here is test_15's doc str""" log.info('Here is logging of test_15') sleep(2) self.assertEqual(1, 1) def test_16(self): """Here is test_16's doc str""" log.info('Here is logging of test_16') sleep(1) self.assertEqual(1, 1) class MyTestClass8(unittest.TestCase): @unishark.data_driven(left=list(range(9))) @unishark.data_driven(right=list(range(9))) def test_17(self, **param): """Test cross-multiply data-driven""" l = param['left'] r = param['right'] sleep(0.1) log.info('%d x %d = %d' % (l, r, l * r)) if __name__ == '__main__': suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__]) result = unishark.BufferedTestRunner().run(suite, name='mytest3', max_workers=2) reporter = unishark.HtmlReporter(dest='log') reporter.report(result)
import unittest import unishark from test_result import ResultTestCase from test_reporter import ReporterTestCase from test_suite import SuiteTestCase from test_runner import RunnerTestCase from test_loader import LoaderTestCase from test_decorator import DecoratorTestCase from test_testprogram import DefaultTestProgramTestCase from test_util import UtilTestCase import sys if __name__ == '__main__': # prepare test suite suite = unittest.TestSuite() loader = unittest.TestLoader() test_classes = [ResultTestCase, SuiteTestCase, RunnerTestCase, ReporterTestCase, LoaderTestCase, DecoratorTestCase, DefaultTestProgramTestCase, UtilTestCase] suite.addTests(list(map(loader.loadTestsFromTestCase, test_classes))) # run test suite result = unishark.BufferedTestRunner([], verbosity=2).run(suite) exit_code = 0 if result.wasSuccessful() else 1 print('Exit Code: %d' % exit_code) sys.exit(exit_code)
def test_init_with_non_iterable_reporters(self): with self.assertRaises(TypeError): unishark.BufferedTestRunner(reporters=unishark.HtmlReporter())