From 7ee5a789cb196f68837dde736701146ebb3a687c Mon Sep 17 00:00:00 2001 From: debugtalk Date: Fri, 17 Jun 2022 19:44:45 +0800 Subject: [PATCH] refactor: import allure --- httprunner/runner.py | 20 ++++++++++---------- httprunner/step_request.py | 18 +++++++----------- httprunner/step_sql_request.py | 27 +++++++++++---------------- httprunner/step_thrift_request.py | 26 +++++++++++++++++--------- 4 files changed, 45 insertions(+), 46 deletions(-) diff --git a/httprunner/runner.py b/httprunner/runner.py index 3b73778c..9e17b0ee 100644 --- a/httprunner/runner.py +++ b/httprunner/runner.py @@ -7,9 +7,9 @@ from typing import Dict, List, Text try: import allure - USE_ALLURE = True + ALLURE = allure except ModuleNotFoundError: - USE_ALLURE = False + ALLURE = None from loguru import logger @@ -178,8 +178,8 @@ class SessionRunner(object): # run step for i in range(step.retry_times + 1): try: - if USE_ALLURE: - with allure.step(f"step: {step.name()}"): + if ALLURE is not None: + with ALLURE.step(f"step: {step.name()}"): step_result: StepResult = step.run(self) else: step_result: StepResult = step.run(self) @@ -209,10 +209,10 @@ class SessionRunner(object): self.__init() self.__parse_config(param) - if USE_ALLURE: + if ALLURE is not None: # update allure report meta - allure.dynamic.title(self.__config.name) - allure.dynamic.description(f"TestCase ID: {self.case_id}") + ALLURE.dynamic.title(self.__config.name) + ALLURE.dynamic.description(f"TestCase ID: {self.case_id}") logger.info( f"Start to run testcase: {self.__config.name}, TestCase ID: {self.case_id}" @@ -226,11 +226,11 @@ class SessionRunner(object): self.__run_step(step) finally: logger.info(f"generate testcase log: {self.__log_path}") - if USE_ALLURE: - allure.attach.file( + if ALLURE is not None: + ALLURE.attach.file( self.__log_path, name="all log", - attachment_type=allure.attachment_type.TEXT, + attachment_type=ALLURE.attachment_type.TEXT, ) self.__duration = time.time() - self.__start_at diff --git a/httprunner/step_request.py b/httprunner/step_request.py index 8050ab2e..055d3d79 100644 --- a/httprunner/step_request.py +++ b/httprunner/step_request.py @@ -19,7 +19,7 @@ from httprunner.models import ( ) from httprunner.parser import build_url from httprunner.response import ResponseObject -from httprunner.runner import USE_ALLURE, HttpRunner +from httprunner.runner import ALLURE, HttpRunner def call_hooks( @@ -119,13 +119,11 @@ def run_step_request(runner: HttpRunner, step: TStep) -> StepResult: request_print += f"{k}: {pretty_format(v)}\n" print(request_print) - if USE_ALLURE: - import allure - - allure.attach( + if ALLURE is not None: + ALLURE.attach( request_print, name="request details", - attachment_type=allure.attachment_type.TEXT, + attachment_type=ALLURE.attachment_type.TEXT, ) resp = runner.session.request(method, url, **parsed_request_dict) @@ -141,13 +139,11 @@ def run_step_request(runner: HttpRunner, step: TStep) -> StepResult: response_print += f"body: {pretty_format(resp_body)}\n" print(response_print) - if USE_ALLURE: - import allure - - allure.attach( + if ALLURE is not None: + ALLURE.attach( response_print, name="response details", - attachment_type=allure.attachment_type.TEXT, + attachment_type=ALLURE.attachment_type.TEXT, ) resp_obj = ResponseObject(resp, runner.parser) step_variables["response"] = resp_obj diff --git a/httprunner/step_sql_request.py b/httprunner/step_sql_request.py index 128264d2..bef988d2 100644 --- a/httprunner/step_sql_request.py +++ b/httprunner/step_sql_request.py @@ -2,15 +2,14 @@ import sys import time from typing import Text + from loguru import logger from httprunner import utils -from httprunner.exceptions import SqlMethodNotSupport -from httprunner.exceptions import ValidationFailure -from httprunner.models import IStep, StepResult, TStep -from httprunner.models import SqlMethodEnum, TSqlRequest +from httprunner.exceptions import SqlMethodNotSupport, ValidationFailure +from httprunner.models import IStep, SqlMethodEnum, StepResult, TSqlRequest, TStep from httprunner.response import SqlResponseObject -from httprunner.runner import HttpRunner, USE_ALLURE +from httprunner.runner import ALLURE, HttpRunner from httprunner.step_request import ( StepRequestExtraction, StepRequestValidation, @@ -18,8 +17,8 @@ from httprunner.step_request import ( ) try: - import sqlalchemy import pymysql + import sqlalchemy SQL_READY = True except ModuleNotFoundError: @@ -103,13 +102,11 @@ def run_step_sql_request(runner: HttpRunner, step: TStep) -> StepResult: sql_request_print += "\n" - if USE_ALLURE: - import allure - - allure.attach( + if ALLURE is not None: + ALLURE.attach( sql_request_print, name="sql request details", - attachment_type=allure.attachment_type.TEXT, + attachment_type=ALLURE.attachment_type.TEXT, ) logger.info(f"Executing SQL: {parsed_request_dict['sql']}") if step.sql_request.method == SqlMethodEnum.FETCHONE: @@ -147,13 +144,11 @@ def run_step_sql_request(runner: HttpRunner, step: TStep) -> StepResult: sql_response_print += "-" * 34 + "\n" elif sql_resp is None: sql_response_print += "None\n" - if USE_ALLURE: - import allure - - allure.attach( + if ALLURE is not None: + ALLURE.attach( sql_response_print, name="sql response details", - attachment_type=allure.attachment_type.TEXT, + attachment_type=ALLURE.attachment_type.TEXT, ) resp_obj = SqlResponseObject(sql_resp, parser=runner.parser) diff --git a/httprunner/step_thrift_request.py b/httprunner/step_thrift_request.py index b77e3c3c..50772df6 100644 --- a/httprunner/step_thrift_request.py +++ b/httprunner/step_thrift_request.py @@ -3,6 +3,7 @@ import platform import sys import time from typing import Text, Union + from loguru import logger from httprunner import utils @@ -11,20 +12,21 @@ from httprunner.models import ( IStep, ProtoType, StepResult, + TransType, TStep, TThriftRequest, - TransType, ) from httprunner.response import ThriftResponseObject -from httprunner.runner import HttpRunner, USE_ALLURE +from httprunner.runner import ALLURE, HttpRunner from httprunner.step_request import ( - call_hooks, StepRequestExtraction, StepRequestValidation, + call_hooks, ) try: import thriftpy2 + from thrift.Thrift import TType THRIFT_READY = True @@ -127,9 +129,12 @@ def run_step_thrift_request(runner: HttpRunner, step: TStep) -> StepResult: v = utils.omit_long_data(v) thrift_request_print += f"{k}: {repr(v)}\n" thrift_request_print += "\n" - if USE_ALLURE: - import allure - allure.attach(thrift_request_print, name="thrift request details", attachment_type=allure.attachment_type.TEXT) + if ALLURE is not None: + ALLURE.attach( + thrift_request_print, + name="thrift request details", + attachment_type=ALLURE.attachment_type.TEXT, + ) # thrift request resp = runner.thrift_client.send_request( @@ -143,9 +148,12 @@ def run_step_thrift_request(runner: HttpRunner, step: TStep) -> StepResult: for k, v in resp.items(): v = utils.omit_long_data(v) thrift_response_print += f"{k}: {repr(v)}\n" - if USE_ALLURE: - import allure - allure.attach(thrift_request_print, name="thrift response details", attachment_type=allure.attachment_type.TEXT) + if ALLURE is not None: + ALLURE.attach( + thrift_request_print, + name="thrift response details", + attachment_type=ALLURE.attachment_type.TEXT, + ) # teardown hooks if step.teardown_hooks: