예제 #1
0
            def wrapper(*args, **kw):
                # info = description if description else func.__doc__
                template = '%(time)s - %(mobile)s - %(level)s - %(caseName)s - %(func)s%(args)s ' \
                           + '- %(description)s'
                TestLogger.log_level = "INFO"
                turn_on = False
                try:
                    if TestLogger._do_log:
                        turn_on = True
                        TestLogger._do_log = False
                    result = func(*args, **kw)
                    return result
                except Exception as error:
                    TestLogger.log_level = "ERROR"
                    raise error

                finally:
                    if turn_on:
                        TestLogger._do_log = True
                        # from library.core.BasePage import BasePage
                        from library.core.utils import applicationcache
                        from library.core.utils import common
                        current_mobile = getattr(applicationcache,
                                                 'current_mobile',
                                                 lambda: None)
                        mobile = current_mobile()
                        log_info = func.__doc__ if info is None else info

                        current_time = _time()
                        (current_time_int,
                         current_time_fraction) = divmod(current_time, 1)
                        current_time_struct = _localtime(current_time_int)

                        timestamp = _strftime(
                            "%Y-%m-%dT%H:%M:%S.", current_time_struct
                        ) + "%03d" % (int(current_time_fraction * 1000))
                        import inspect
                        received_args = inspect.getcallargs(func, *args, **kw)
                        if 'self' in received_args:
                            received_args.pop('self')
                        print(template % dict(
                            time=timestamp,
                            level=TestLogger.log_level,
                            # caseName=getattr(TestLogger.current_test, '_testMethodName', None),
                            caseName=common.get_test_id(
                                TestLogger.current_test),
                            func=common.get_method_fullname(func),
                            mobile=mobile.__str__(),
                            description=log_info
                            if log_info else "no description",
                            # args='[Args: {} {}]'.format(
                            #     args[1:] if bool(args[:1]) and isinstance(args[0],
                            #                                               BasePage) else args,
                            #     kw)
                            args='{}'.format(received_args),
                        ))
                        TestLogger.log_level = "INFO"
예제 #2
0
 def addSuccess(self, test):
     super(_TestResult, self).addSuccess(test)
     from library.core.TestLogger import TestLogger
     from library.core.utils import common
     TestLogger.test_success(test)
     self.success_count += 1
     output = self.log_output.getvalue()
     self.result.append((0, test, output, ''))
     if self.verbosity > 1:
         _real_stdout.write('PASS  {}\n'.format(common.get_test_id(test)))
         _real_stdout.flush()
     else:
         _real_stdout.write('PASS\n')
         _real_stdout.flush()
예제 #3
0
 def addFailure(self, test, err):
     from library.core.TestLogger import TestLogger
     from library.core.utils import common
     TestLogger.test_fail(test, err)
     self.failure_count += 1
     super(_TestResult, self).addFailure(test, err)
     _, _exc_str = self.failures[-1]
     output = self.log_output.getvalue()
     self.result.append((1, test, output, _exc_str))
     if self.verbosity > 1:
         _real_stdout.write('FAIL  {}\n'.format(common.get_test_id(test)))
         _real_stdout.flush()
     else:
         _real_stdout.write('FAIL')
         _real_stdout.flush()
예제 #4
0
    def test_success(test):
        from library.core.utils import common
        current_time = _time()
        (current_time_int, current_time_fraction) = divmod(current_time, 1)
        current_time_struct = _localtime(current_time_int)

        timestamp = _strftime(
            "%Y-%m-%dT%H:%M:%S.",
            current_time_struct) + "%03d" % (int(current_time_fraction * 1000))
        if getattr(test, '_testMethodName', None):
            print(' - '.join([
                timestamp, TestLogger.log_level,
                common.get_test_id(test), '********** TEST SUCCESS **********'
            ]))
        TestLogger.current_test = None
예제 #5
0
    def start_test(test):
        from library.core.utils import common
        TestLogger.current_test = test
        current_time = _time()
        (current_time_int, current_time_fraction) = divmod(current_time, 1)
        current_time_struct = _localtime(current_time_int)

        timestamp = _strftime(
            "%Y-%m-%dT%H:%M:%S.",
            current_time_struct) + "%03d" % (int(current_time_fraction * 1000))
        if getattr(TestLogger.current_test, '_testMethodName', None):
            print(' - '.join([
                '\n' + timestamp,
                # getattr(TestLogger.current_test.__class__, '__name__'),
                TestLogger.log_level,
                common.get_test_id(test),
                '********** TEST START **********'
            ]))
예제 #6
0
 def addError(self, test, err):
     from library.core.TestLogger import TestLogger
     from library.core.utils import common
     TestLogger.test_error(test, err)
     self.error_count += 1
     super(_TestResult, self).addError(test, err)
     _, _exc_str = self.errors[-1]
     output = self.log_output.getvalue()
     self.result.append((2, test, output, _exc_str))
     if self.verbosity > 1:
         log = 'ERROR {}\n'.format(common.get_test_id(test))
         lists.add(log)
         _real_stdout.write(log)
         _real_stdout.flush()
         self._progress_bars()
     else:
         _real_stdout.write('ERROR')
         _real_stdout.flush()
         self._progress_bars()
예제 #7
0
    def test_error(test, err):
        from library.core.utils import common
        current_time = _time()
        (current_time_int, current_time_fraction) = divmod(current_time, 1)
        current_time_struct = _localtime(current_time_int)

        timestamp = _strftime(
            "%Y-%m-%dT%H:%M:%S.",
            current_time_struct) + "%03d" % (int(current_time_fraction * 1000))
        import sys
        sys.excepthook(*err)
        TestLogger.take_screen_shot()
        # print(common.convert_error_to_string(err))
        if getattr(test, '_testMethodName', None):
            print(' - '.join([
                timestamp, TestLogger.log_level,
                common.get_test_id(test), '********** TEST ERROR **********'
            ]))
        TestLogger.current_test = None