mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-12 19:39:44 +08:00
fix: set log level for pytest
This commit is contained in:
@@ -14,7 +14,7 @@
|
||||
**python version**
|
||||
|
||||
- feat: support skip for pytest
|
||||
- feat: print request and response details when running API cases
|
||||
- feat: print request and response details in DEBUG level when running API cases
|
||||
- fix: support None/dict/list format when printing sql response
|
||||
|
||||
## v4.1.3 (2022-06-14)
|
||||
|
||||
@@ -56,8 +56,6 @@ def main_run(extra_args) -> enum.IntEnum:
|
||||
|
||||
def main():
|
||||
"""API test: parse command line options and run commands."""
|
||||
init_logger()
|
||||
|
||||
parser = argparse.ArgumentParser(description=__description__)
|
||||
parser.add_argument(
|
||||
"-V", "--version", dest="version", action="store_true", help="show version"
|
||||
@@ -103,6 +101,19 @@ def main():
|
||||
print(f"{__version__}")
|
||||
sys.exit(0)
|
||||
|
||||
# set log level
|
||||
try:
|
||||
index = extra_args.index("--log-level")
|
||||
if index < len(extra_args) - 1:
|
||||
level = extra_args[index + 1]
|
||||
else:
|
||||
# not specify log level value
|
||||
level = "INFO" # default
|
||||
except ValueError:
|
||||
level = "INFO" # default
|
||||
|
||||
init_logger(level)
|
||||
|
||||
if sys.argv[1] == "run":
|
||||
sys.exit(main_run(extra_args))
|
||||
elif sys.argv[1] == "make":
|
||||
|
||||
@@ -27,7 +27,7 @@ from httprunner.models import (
|
||||
VariablesMapping,
|
||||
)
|
||||
from httprunner.parser import Parser
|
||||
from httprunner.utils import LOGGER_FORMAT, init_logger, merge_variables
|
||||
from httprunner.utils import LOGGER_FORMAT, merge_variables
|
||||
|
||||
|
||||
class SessionRunner(object):
|
||||
@@ -54,7 +54,6 @@ class SessionRunner(object):
|
||||
__log_path: Text = ""
|
||||
|
||||
def __init(self):
|
||||
init_logger()
|
||||
self.__config = self.config.struct()
|
||||
self.__session_variables = self.__session_variables or {}
|
||||
self.__start_at = 0
|
||||
|
||||
@@ -118,7 +118,7 @@ def run_step_request(runner: HttpRunner, step: TStep) -> StepResult:
|
||||
for k, v in parsed_request_dict.items():
|
||||
request_print += f"{k}: {pretty_format(v)}\n"
|
||||
|
||||
logger.info(request_print)
|
||||
logger.debug(request_print)
|
||||
if ALLURE is not None:
|
||||
ALLURE.attach(
|
||||
request_print,
|
||||
@@ -138,7 +138,7 @@ def run_step_request(runner: HttpRunner, step: TStep) -> StepResult:
|
||||
resp_body = resp.content
|
||||
|
||||
response_print += f"body: {pretty_format(resp_body)}\n"
|
||||
logger.info(response_print)
|
||||
logger.debug(response_print)
|
||||
if ALLURE is not None:
|
||||
ALLURE.attach(
|
||||
response_print,
|
||||
|
||||
@@ -323,7 +323,11 @@ def gen_cartesian_product(*args: List[Dict]) -> List[Dict]:
|
||||
LOGGER_FORMAT = "<green>{time:YYYY-MM-DD HH:mm:ss.SSS}</green> | <level>{level}</level> | <level>{message}</level>"
|
||||
|
||||
|
||||
def init_logger():
|
||||
def init_logger(level: str):
|
||||
level = level.upper()
|
||||
if level not in ["TRACE", "DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"]:
|
||||
level = "INFO" # default
|
||||
|
||||
# set log level to INFO
|
||||
logger.remove()
|
||||
logger.add(sys.stderr, format=LOGGER_FORMAT, level="INFO")
|
||||
logger.add(sys.stdout, format=LOGGER_FORMAT, level=level)
|
||||
|
||||
Reference in New Issue
Block a user