Best Python code snippet using lemoncheesecake
unitcore.py
Source:unitcore.py
...49 try:50 if hasattr(self, '__libs_options'):51 for opt in self.__libs_options:52 if 'teardown_suite' in opt.REGISTERED:53 self.__syslogger.info('Call teardown_suite() of "%s" options module...' % opt.fullname)54 CONFIG.TEST.CURRENT_STATE = 'teardown_suite of "%s" module' % opt.fullname55 opt.teardown_suite()56 except Exception as e:57 self.__syslogger.exception(e)58 raise59 # change state name60 CONFIG.TEST.CURRENT_STATE = "TearDownClass"61 # call original function62 try:63 self.__originalTearDownClass()64 except Exception as e:65 self.__syslogger.exception(e)66 raise67def setUp(self):68 """69 Replacement function of original unittest **setUp** function...
runner.py
Source:runner.py
...67 self.syslogger.info('Execute "setup_suite" of %s options module...'68 % NAME.safe_substitute(name=opt.fullname))69 opt.setup_suite()70 self.syslogger.done()71 def _teardown_suite(self):72 """ execute all "teardown_suite" function from libs options """73 for opt in self.libs_options:74 if 'teardown_suite' in opt.REGISTERED:75 self.syslogger.info('Execute "teardown_suite" of %s options module...'76 % NAME.safe_substitute(name=opt.fullname))77 opt.teardown_suite()78 self.syslogger.done()79 def launch(self):80 """81 Launch all tests from **CONFIG.UNITTEST.SELECTED_TEST_CASES**82 """83 # global TestCase cycle84 start_time = time()85 CONFIG.UNITTEST.__LAST_ERROR__ = None86 try:87 for cycle in range(CONFIG.SYSTEM.TOTAL_CYCLES_GLOBAL):88 CONFIG.SYSTEM.CURRENT_CYCLE_GLOBAL = cycle+189 self.logger.newline(self.syslogger)90 if CONFIG.SYSTEM.TOTAL_CYCLES_GLOBAL > 1:91 self.logger.table(': *', ('CYCLE %d/%d' % (CONFIG.SYSTEM.CURRENT_CYCLE_GLOBAL,92 CONFIG.SYSTEM.TOTAL_CYCLES_GLOBAL), 15, 'C'), ': *',93 self.syslogger, border_delimiter=' ')94 self.logger.newline(self.syslogger)95 self.logger.info('ARGUMENTS: %s %s' % (sys.argv[0], CONFIG.UNITTEST.__USED_OPTIONS__))96 # TestCases97 for i, case in enumerate(CONFIG.UNITTEST.SELECTED_TEST_CASES):98 # update current TestCase and TestCase index99 CONFIG.TEST.CURRENT_CASE = case['name']100 CONFIG.TEST.CURRENT_CASE_INDEX = i+1101 # TestSuites102 for suite in case['suites']:103 self.logger.newline(self.syslogger, lines=1)104 # self.logger.table('-*', self.syslogger, border_delimiter='-')105 # update current_suite106 CONFIG.TEST.CURRENT_SUITE = suite['name']107 # Add TestSuite notify108 CONFIG.UNITTEST.__NOTIFICATION__ = '%s TestSuite ' \109 % SUITE_FULL.safe_substitute(case=case['name'],110 index=case['index'],111 suite=suite['name'])112 CONFIG.UNITTEST.__NOTIFICATION__ += ('with parameters: %s' %113 PARAMS.safe_substitute(name=suite['params'])) \114 if suite['params'] is not None else 'without parameters'115 CONFIG.UNITTEST.__NOTIFICATION__ += ('| [%s][%s_%s] ' % (CONFIG.DEVICE.DEVICE_NAME,116 CONFIG.DEVICE.BUILD_TYPE,117 CONFIG.DEVICE.BUILD_VERSION)) \118 if CONFIG.DEVICE.DEVICE_NAME != '' else ''119 CONFIG.UNITTEST.__NOTIFICATION__ += ' | Cycle: %d/%d ' % (CONFIG.SYSTEM.CURRENT_CYCLE_GLOBAL,120 CONFIG.SYSTEM.TOTAL_CYCLES_GLOBAL)121 # Run TestSuite122 try:123 try:124 self._setup_suite()125 self.run_suite(case=case, suite=suite)126 self._teardown_suite()127 finally:128 # print results after suite129 # print results if we have more than one test case or test suite130 if not (len(CONFIG.UNITTEST.SELECTED_TEST_CASES) == 1 and len(case['suites']) == 1):131 Console.print_results(logger=self.logger, case=case, cycle=cycle+1, suite=suite)132 if CONFIG.UNITTEST.INTERRUPT_BY_FAIL:133 for c in CONFIG.UNITTEST.SELECTED_TEST_CASES:134 for s in c['suites']:135 msg = '%s TestSuite %s' % (SUITE_FULL.safe_substitute(case=case['name'],136 suite=suite['name'],137 index=case['index']),138 ('with parameters: %s'139 % PARAMS.safe_substitute(name=suite['params'])140 if suite['params'] is not None...
test_execution.py
Source:test_execution.py
...35 def test_executes_teardown_suite_if_no_failures(self):36 ran = False37 def wrapper():38 class Suite(TestSuite):39 def teardown_suite(self):40 nonlocal ran41 ran = True42 wrapper()43 assert ran, "Teardown did not run"44 def test_executes_teardown_suite_if_setup_suite_fails(self):45 ran = False46 def wrapper():47 class Suite(TestSuite):48 def setup_suite(self):49 raise RuntimeError("intentional")50 def teardown_suite(self):51 nonlocal ran52 ran = True53 wrapper()54 assert ran, "Teardown did not run"55 def test_executes_teardown_suite_if_test_fails(self):56 ran = False57 def wrapper():58 class Suite(TestSuite):59 def test_boom(self):60 raise AssertionError("intentional")61 def teardown_suite(self):62 nonlocal ran63 ran = True64 wrapper()65 assert ran, "Suite teardown did not run"66 def test_executes_first_test(self):67 ran = False68 def wrapper():69 class SandySuite(TestSuite):70 def test_spam(self):71 nonlocal ran72 ran = True73 wrapper()74 assert ran, "Test did not run"75 def test_does_not_execute_non_test(self):...
executor.py
Source:executor.py
...16 successful_setup = suite_executor.setup_suite(suite)17 if successful_setup:18 suite_executor.exec_suite()19 finally:20 suite_executor.teardown_suite(suite)21def instantiate(class_name, *args, **kwargs):22 """Helper to dynamically instantiate a class from a name."""23 split_name = class_name.split(".")24 module_name = split_name[0]25 class_name = ".".join(split_name[1:])26 module = __import__(module_name)27 class_ = getattr(module, class_name)28 return class_(*args, **kwargs)29class TestSuiteExecutor(object):30 """The state of execution of a suite of tests.31 The job of the TestSuiteExecutor is to convert the incoming fixtures and tester configuration32 into Fixture and TestCase objects, then execute them using the standard unittest framework.33 """34 def __init__(self, logger, testers={}, fixtures={}, fail_fast=False, **kwargs):35 self.logger = logger36 self.testers = testers37 self.fixtures = fixtures38 self.fail_fast = fail_fast39 if len(kwargs) > 0:40 raise optparse.OptionValueError("Unrecognized options for executor: %s" % kwargs)41 for fixture_name in self.fixtures:42 self.fixtures[fixture_name] = \43 self.build_fixture(fixture_name, **self.fixtures[fixture_name])44 def build_fixture(self, fixture_name, fixture_class=None, fixture_logger=None,45 **fixture_kwargs):46 if not fixture_class:47 fixture_class = fixtures.DEFAULT_FIXTURE_CLASSES[fixture_name]48 if not fixture_logger:49 fixture_logger = self.logger.getChild("fixtures.%s" % fixture_name)50 else:51 fixture_logger = logging.getLogger(fixture_logger)52 return instantiate(fixture_class, fixture_logger, **fixture_kwargs)53 def build_tester(self, test):54 tester_type = test.test_type55 def extract_tester_args(tester_class=None, tester_logger=None, **tester_kwargs):56 return tester_class, tester_logger, tester_kwargs57 tester_class, tester_logger, tester_kwargs = \58 extract_tester_args(59 **(self.testers[tester_type] if tester_type in self.testers else {}))60 if not tester_class:61 tester_class = testers.DEFAULT_TESTER_CLASSES[tester_type]62 if not tester_logger:63 tester_logger = self.logger.getChild("testers.%s.%s" % (tester_type, test.uri))64 else:65 tester_logger = logging.getLogger(tester_logger)66 test_apis = []67 for fixture_name, fixture in self.fixtures.items():68 test_api = fixture.build_api(tester_type, tester_logger)69 if test_api:70 test_apis.append(test_api)71 return instantiate(tester_class, test, test_apis, tester_logger, **tester_kwargs)72 def setup_suite(self, suite):73 self.setup_fixtures = {}74 for fixture_name, fixture in self.fixtures.items():75 try:76 fixture.setup()77 self.setup_fixtures[fixture_name] = fixture78 except:79 print "Suite setup failed: %s" % fixture_name80 traceback.print_exc()81 return False82 self.unittest_suite = unittest.TestSuite()83 for test in suite:84 self.unittest_suite.addTest(self.build_tester(test))85 return True86 def exec_suite(self):87 # TODO: More stuff here?88 unittest.TextTestRunner(89 verbosity=2, failfast=self.fail_fast).run(self.unittest_suite)90 def teardown_suite(self, suite):91 for fixture_name, fixture in self.setup_fixtures.items():92 try:93 fixture.teardown()94 except:95 print "Suite teardown failed: %s" % fixture_name...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!