refactor: import allure

This commit is contained in:
debugtalk
2022-06-17 19:44:45 +08:00
parent 6321e0a04a
commit 7ee5a789cb
4 changed files with 45 additions and 46 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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)

View File

@@ -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: