Best Python code snippet using Testify_python
unit.py
Source:unit.py  
...21    implementing a "get_test_class_name" method on you TestCase subclass.22    """23    return "%s.%s" % (test.__class__.__module__,24                      test.__class__.__name__)25def get_test_method_name(test):26    """27    This method is used to return the full method name from a28    :class:`unittest.TestCase` instance.29    It is used as a default to define the "method_name" extra value30    passed in structured loggers. You can override the default by31    implementing a "get_test_method_name" method on you TestCase subclass.32    """33    return test._testMethodName34class StructuredTestResult(TextTestResult):35    def __init__(self, *args, **kwargs):36        self.logger = kwargs.pop('logger')37        self.test_list = kwargs.pop("test_list", [])38        self.result_callbacks = kwargs.pop('result_callbacks', [])39        self.passed = 040        self.testsRun = 041        TextTestResult.__init__(self, *args, **kwargs)42    def call_callbacks(self, test, status):43        debug_info = {}44        for callback in self.result_callbacks:45            info = callback(test, status)46            if info is not None:47                debug_info.update(info)48        return debug_info49    def startTestRun(self):50        # This would be an opportunity to call the logger's suite_start action,51        # however some users may use multiple suites, and per the structured52        # logging protocol, this action should only be called once.53        pass54    def startTest(self, test):55        self.testsRun += 156        self.logger.test_start(test.id())57    def stopTest(self, test):58        pass59    def stopTestRun(self):60        # This would be an opportunity to call the logger's suite_end action,61        # however some users may use multiple suites, and per the structured62        # logging protocol, this action should only be called once.63        pass64    def _extract_err_message(self, err):65        # Format an exception message in the style of unittest's _exc_info_to_string66        # while maintaining a division between a traceback and a message.67        exc_ty, val, _ = err68        exc_msg = "".join(traceback.format_exception_only(exc_ty, val))69        if self.buffer:70            output_msg = "\n".join([sys.stdout.getvalue(), sys.stderr.getvalue()])71            return "".join([exc_msg, output_msg])72        return exc_msg.rstrip()73    def _extract_stacktrace(self, err, test):74        # Format an exception stack in the style of unittest's _exc_info_to_string75        # while maintaining a division between a traceback and a message.76        # This is mostly borrowed from unittest.result._exc_info_to_string.77        exctype, value, tb = err78        while tb and self._is_relevant_tb_level(tb):79            tb = tb.tb_next80        # Header usually included by print_exception81        lines = ["Traceback (most recent call last):\n"]82        if exctype is test.failureException:83            length = self._count_relevant_tb_levels(tb)84            lines += traceback.format_tb(tb, length)85        else:86            lines += traceback.format_tb(tb)87        return "".join(lines)88    def _get_class_method_name(self, test):89        if hasattr(test, 'get_test_class_name'):90            class_name = test.get_test_class_name()91        else:92            class_name = get_test_class_name(test)93        if hasattr(test, 'get_test_method_name'):94            method_name = test.get_test_method_name()95        else:96            method_name = get_test_method_name(test)97        return {98            'class_name': class_name,99            'method_name': method_name100        }101    def addError(self, test, err):102        self.errors.append((test, self._exc_info_to_string(err, test)))103        extra = self.call_callbacks(test, "ERROR")104        extra.update(self._get_class_method_name(test))105        self.logger.test_end(test.id(),106                             "ERROR",107                             message=self._extract_err_message(err),108                             expected="PASS",109                             stack=self._extract_stacktrace(err, test),110                             extra=extra)...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!!
