Best Python code snippet using Testify_python
test_runner.py
Source:test_runner.py
1# Copyright 2009 Yelp2#3# Licensed under the Apache License, Version 2.0 (the "License");4# you may not use this file except in compliance with the License.5# You may obtain a copy of the License at6#7# http://www.apache.org/licenses/LICENSE-2.08#9# Unless required by applicable law or agreed to in writing, software10# distributed under the License is distributed on an "AS IS" BASIS,11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12# See the License for the specific language governing permissions and13# limitations under the License.14"""This module contains the TestRunner class and other helper code"""15from __future__ import absolute_import16from __future__ import print_function17from collections import defaultdict18import functools19import json20import six21from .test_case import MetaTestCase, TestCase22from . import test_discovery23from . import exit24from . import exceptions25__author__ = "Oliver Nicholas <bigo@yelp.com>"26__testify = 127class TestRunner(object):28 """TestRunner is the controller class of the testify suite.29 It is responsible for collecting a list of TestCase subclasses, instantiating and30 running them, delegating the collection of results and printing of statistics.31 """32 def __init__(self,33 test_path_or_test_case,34 debugger=None,35 suites_exclude=(),36 suites_require=(),37 options=None,38 test_reporters=None,39 plugin_modules=None,40 module_method_overrides=None,41 failure_limit=None42 ):43 """After instantiating a TestRunner, call run() to run them."""44 self.test_path_or_test_case = test_path_or_test_case45 self.debugger = debugger46 self.suites_exclude = set(suites_exclude)47 self.suites_require = set(suites_require)48 self.options = options49 self.plugin_modules = plugin_modules or []50 self.test_reporters = test_reporters or []51 self.module_method_overrides = module_method_overrides if module_method_overrides is not None else {}52 self.failure_limit = failure_limit53 self.failure_count = 054 @classmethod55 def get_test_method_name(cls, test_method):56 test_method_self_t = type(six.get_method_self(test_method))57 # PY2, unbound methods hit this58 # PY3 you get attributeerror on __self__ one line above59 assert not isinstance(test_method_self_t, type(None))60 return '%s %s.%s' % (61 test_method_self_t.__module__,62 test_method_self_t.__name__,63 test_method.__name__,64 )65 def _construct_test(self, test_case_cls, **kwargs):66 name_overrides = kwargs.pop(67 'name_overrides',68 self.module_method_overrides.get(test_case_cls.__name__, None),69 )70 test_case = test_case_cls(71 suites_exclude=self.suites_exclude,72 suites_require=self.suites_require,73 name_overrides=name_overrides,74 failure_limit=(self.failure_limit - self.failure_count) if self.failure_limit else None,75 debugger=self.debugger,76 **kwargs77 )78 # Add in information from plugins79 for plugin_mod in self.plugin_modules:80 if hasattr(plugin_mod, 'add_testcase_info'):81 plugin_mod.add_testcase_info(test_case, self)82 return test_case83 def discover(self):84 if isinstance(self.test_path_or_test_case, (TestCase, MetaTestCase)):85 # For testing purposes only86 return [self.test_path_or_test_case()]87 else:88 return (89 self._construct_test(test_case_class)90 for test_case_class in test_discovery.discover(self.test_path_or_test_case)91 if not self.module_method_overrides or test_case_class.__name__ in self.module_method_overrides92 )93 def run(self):94 """Instantiate our found test case classes and run their test methods.95 We use this opportunity to apply any test method name overrides that were parsed96 from the command line (or rather, passed in on initialization).97 Logging of individual results is accomplished by registering callbacks for98 the TestCase instances to call when they begin and finish running each test.99 At its conclusion, we pass our collected results to our TestLogger to100 print out exceptions and testing summaries.101 Returns an exit code from sysexits.h. See:102 http://linux.die.net/include/sysexits.h103 """104 try:105 for test_case in self.discover():106 if self.failure_limit and self.failure_count >= self.failure_limit:107 break108 # We allow our plugins to mutate the test case prior to execution109 for plugin_mod in self.plugin_modules:110 if hasattr(plugin_mod, "prepare_test_case"):111 plugin_mod.prepare_test_case(self.options, test_case)112 if not any(test_case.runnable_test_methods()):113 continue114 def failure_counter(result_dict):115 if not result_dict['success']:116 self.failure_count += 1117 for reporter in self.test_reporters:118 test_case.register_callback(test_case.EVENT_ON_RUN_TEST_METHOD, reporter.test_start)119 test_case.register_callback(test_case.EVENT_ON_COMPLETE_TEST_METHOD, reporter.test_complete)120 test_case.register_callback(test_case.EVENT_ON_RUN_CLASS_SETUP_METHOD, reporter.class_setup_start)121 test_case.register_callback(test_case.EVENT_ON_COMPLETE_CLASS_SETUP_METHOD, reporter.class_setup_complete)122 test_case.register_callback(test_case.EVENT_ON_RUN_CLASS_TEARDOWN_METHOD, reporter.class_teardown_start)123 test_case.register_callback(124 test_case.EVENT_ON_COMPLETE_CLASS_TEARDOWN_METHOD,125 reporter.class_teardown_complete,126 )127 test_case.register_callback(test_case.EVENT_ON_RUN_TEST_CASE, reporter.test_case_start)128 test_case.register_callback(test_case.EVENT_ON_COMPLETE_TEST_CASE, reporter.test_case_complete)129 test_case.register_callback(test_case.EVENT_ON_COMPLETE_TEST_METHOD, failure_counter)130 # Now we wrap our test case like an onion. Each plugin given the opportunity to wrap it.131 runnable = test_case.run132 for plugin_mod in self.plugin_modules:133 if hasattr(plugin_mod, "run_test_case"):134 runnable = functools.partial(plugin_mod.run_test_case, self.options, test_case, runnable)135 # And we finally execute our finely wrapped test case136 runnable()137 except exceptions.DiscoveryError as exc:138 for reporter in self.test_reporters:139 reporter.test_discovery_failure(exc)140 return exit.DISCOVERY_FAILED141 except exceptions.Interruption:142 # handle interruption so we can cancel in the middle of a run143 # but still get a testing summary.144 pass145 report = [reporter.report() for reporter in self.test_reporters]146 if all(report):147 return exit.OK148 else:149 return exit.TESTS_FAILED150 def list_suites(self):151 """List the suites represented by this TestRunner's tests."""152 suites = defaultdict(list)153 for test_instance in self.discover():154 for test_method in test_instance.runnable_test_methods():155 for suite_name in test_instance.suites(test_method):156 suites[suite_name].append(test_method)157 return {suite_name: "%d tests" % len(suite_members) for suite_name, suite_members in suites.items()}158 def get_tests_for_suite(self, selected_suite_name):159 """Gets the test list for the suite"""160 for test_instance in self.discover():161 for test_method in test_instance.runnable_test_methods():162 if not selected_suite_name or TestCase.in_suite(test_method, selected_suite_name):163 yield test_method164 def list_tests(self, format, selected_suite_name=None):165 """Lists all tests, optionally scoped to a single suite."""166 for test in self.get_tests_for_suite(selected_suite_name):167 name = self.get_test_method_name(test)168 if format == 'txt':169 print(name)170 elif format == 'json':171 testcase = test.__self__172 print(json.dumps(173 dict(174 test=name,175 suites=sorted(testcase.suites(test)),176 ),177 sort_keys=True,178 ))179 else:180 raise ValueError("unknown test list format: '%s'" % format)...
test_reporter.py
Source:test_reporter.py
...31 """Called when there was a failure during test discovery. exc is the exception object generated during the error."""32 def class_setup_start(self, result):33 """Called when a class_setup or the first half of a class_setup_teardown starts"""34 pass35 def class_setup_complete(self, result):36 """Called when a class_setup or the first half of a class_setup_teardown finishes"""37 pass38 def class_teardown_start(self, result):39 """Called when a class_teardown or the second half of a class_setup_teardown starts"""40 pass41 def class_teardown_complete(self, result):42 """Called when a class_teardown or the second half of a class_setup_teardown finishes"""43 pass44 def test_case_start(self, result):45 """Called when a test case is being run. Gets passed the special "run" method as a TestResult."""46 pass47 def test_case_complete(self, result):48 """Called when a test case and all of its fixtures have been run."""49 pass...
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!!