示例#1
0
    def run_testcase(testsuite, testcase):
        # 执行测试用例
        try:
            if testcase.enable:  # 测试用例启用时,执行测试
                for step in testcase.steps:
                    ctx = LoggerContext(logger,
                                        context=testsuite.ctx,
                                        testcase=testcase.ctx,
                                        teststep=step.ctx)
                    ctx.info(message="记录测试用例开始时间")
                    step.result.start()
                    while True:
                        try:
                            ctx.info(message="执行HTTP拿到测试数据")
                            response = HTTPRunner(step).run_api()
                            step.result.detail = response.dict

                            ctx.info(message="对比测试结果和期望值")
                            testassert = TestAssert(step.expectation, response)
                            testassert.run_assert()
                            step.result.status = ResultState.PASS

                            ctx.exit(message="记录测试用例结束时间")
                            step.result.ended()

                            break
                        except Exception as ex:
                            step.result.status = ResultState.FAIL
                            step.result.ended()

                            ex_data = {"exception": str(ex)}
                            if step.result.detail:  # 断言错误
                                step.result.detail.update(ex_data)
                            else:  # 网络错误
                                step.result.detail = ex_data

                            if step.result.usage < step.timeout:
                                ctx.info(**ex_data,
                                         traceback=get_traceback(),
                                         step=step.ctx,
                                         message="测试用例执行过程抛错,准备{sleep}s后重试".
                                         format(sleep=step.sleep))
                                step.result.detail = None
                                time.sleep(step.sleep)
                            else:
                                ctx.exit(**ex_data,
                                         traceback=get_traceback(),
                                         step=step.ctx,
                                         message="测试用例执行过程抛错")
                                raise ex
            # else: # 测试用例没有启用,不执行测试
            #     pass
        except Exception as ex:
            logger.error(context=testsuite.ctx,
                         testcase=testcase.ctx,
                         ex=str(ex))
        finally:
            logger.info(testcase=testcase.dict)
            return testcase
示例#2
0
    def process(self):
        count = self.RETRIES
        response = None

        # TODO HTTPAdapter(max_retries=count)
        while count > 0:
            try:
                response = getattr(HttpRequest, self.method)(self)
                break
            except HTTPError as ex:
                logger.info(ex=ex, traceback=get_traceback())

                count -= 1
                if count == 0:
                    raise HTTPError("MaxRetries||{}".format(str(ex)))

                sleep(1)
            except Exception as ex:
                logger.info(ex=str(ex), traceback=get_traceback())
                raise UncaughtError(str(ex))

        return response
示例#3
0
    def pool_process(func, count, proc):
        result = []
        try:
            pool = Pool(count)
            result = pool.starmap(func=func, iterable=proc)
            pool.close()
            pool.join()
        except Exception as ex:
            logger.error(message="UncaughtError()",
                         ex=str(ex),
                         traceback=get_traceback())

        return result
示例#4
0
    def factory(tx_hash):
        ctx = LoggerContext(logger, tx_hash=tx_hash)

        data = []
        try:
            data = ETH(tx_hash).data
        except Exception as ex:
            logger.error(message="UncaughtError()",
                         ex=str(ex),
                         traceback=get_traceback())
        finally:
            ctx.exit(chain_data=data, length=len(data))

        return data
示例#5
0
    def ledger(self, msg=None, *args, **kw):
        try:
            msg = self.format_msg(1, msg, *args, **kw)
            record = logging.LogRecord(self.module, logging.INFO, __file__, 1,
                                       msg, None, None)

            Logger.ledger_log_handler.emit(record)
            Logger.ledger_log_handler.flush()

            Logger.stream_log_handler.emit(record)
            Logger.stream_log_handler.flush()
        except Exception as ex:
            print(
                "ACCESS got an exception: {}, , msg={}, args={}, kw={}, traceback={}"
                .format(ex, msg, args, kw, get_traceback()))
示例#6
0
    def transaction2(self, depth, msg=None, *args, **kw):
        try:
            msg = self.format_msg(depth + 1, msg, *args, **kw)
            record = logging.LogRecord(self.module, logging.ERROR, __file__, 1,
                                       msg, None, None)

            Logger.access_log_handler.emit(record)
            Logger.access_log_handler.flush()

            Logger.transaction_log_handler.emit(record)
            Logger.transaction_log_handler.flush()

            Logger.stream_log_handler.emit(record)
            Logger.stream_log_handler.flush()
        except Exception as ex:
            print(
                "TRANSACTION2 got an exception: {}, depth={}, msg={}, args={}, kw={}, traceback={}"
                .format(ex, depth, msg, args, kw, get_traceback()))
示例#7
0
    def warn(self, msg=None, *args, **kw):
        try:
            if self.log_level > logging.WARNING:
                return

            msg = self.format_msg(1, msg, *args, **kw)
            record = logging.LogRecord(self.module, logging.WARNING, __file__,
                                       1, msg, None, None)

            Logger.access_log_handler.emit(record)
            Logger.access_log_handler.flush()

            Logger.stream_log_handler.emit(record)
            Logger.stream_log_handler.flush()
        except Exception as ex:
            print(
                "WARN got an exception: {}, msg={}, args={}, kw={}, traceback={}"
                .format(ex, msg, args, kw, get_traceback()))
示例#8
0
    def info2(self, depth, msg=None, *args, **kw):
        try:
            if self.log_level > logging.INFO:
                return

            msg = self.format_msg(depth + 1, msg, *args, **kw)
            record = logging.LogRecord(self.module, logging.INFO, __file__, 1,
                                       msg, None, None)

            Logger.access_log_handler.emit(record)
            Logger.access_log_handler.flush()

            Logger.stream_log_handler.emit(record)
            Logger.stream_log_handler.flush()
        except Exception as ex:
            print(
                "INFO2 got an exception: {}, depth={}, msg={}, args={}, kw={}, traceback={}"
                .format(ex, depth, msg, args, kw, get_traceback()))
示例#9
0
    def process(currency):
        ctx = LoggerContext(logger,
                            currency=currency["currency"],
                            address=currency["address"],
                            type=currency["type"])
        mappings = {
            "NEO": NEO,
            "XRP": XRP,
        }
        data = []
        try:
            func = mappings.get(currency.get("currency"))
            data = func(currency).data if func else []
        except Exception as ex:
            logger.error(message="UncaughtError()",
                         ex=str(ex),
                         traceback=get_traceback())
        finally:
            ctx.exit(chain_data=data, length=len(data))

        return data
示例#10
0
    def get(request):
        try:
            # 1)构建请求数据
            request_data = request.dict

            # 2)请求发出日志
            ctx = CustomerMessage("A http request{}")
            logger.info(**ctx.get_request_content(request_data))

            response_data = requests.get(**request_data)
            response_data.raise_for_status()
            result_data = response_data.json()

            # 3)请求接收日志
            logger.info(**ctx.get_response_content(result_data), headers=response_data.headers)

            # 4)返回响应数据
            return result_data
        except Exception as ex:
            logger.error(request=request, ex=str(ex), traceback=get_traceback())
            raise HTTPError("HttpRequest.get(request={})")