mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-12 02:21:29 +08:00
change: prepare_log_file_abs_path
This commit is contained in:
@@ -50,7 +50,7 @@ class HttpRunner(object):
|
||||
self.test_loader = unittest.TestLoader()
|
||||
self.save_tests = save_tests
|
||||
self._summary = None
|
||||
self.project_mapping = None
|
||||
self.test_path = None
|
||||
|
||||
def _add_tests(self, testcases):
|
||||
""" initialize testcase with Runner() and add to test suite.
|
||||
@@ -139,7 +139,7 @@ class HttpRunner(object):
|
||||
log_handler = None
|
||||
if self.save_tests:
|
||||
logs_file_abs_path = utils.prepare_log_file_abs_path(
|
||||
self.project_mapping, f"testcase_{index+1}.log"
|
||||
self.test_path, f"testcase_{index+1}.log"
|
||||
)
|
||||
log_handler = logger.add(logs_file_abs_path, level="DEBUG")
|
||||
|
||||
@@ -197,7 +197,7 @@ class HttpRunner(object):
|
||||
|
||||
if self.save_tests:
|
||||
logs_file_abs_path = utils.prepare_log_file_abs_path(
|
||||
self.project_mapping, f"testcase_{index + 1}.log"
|
||||
self.test_path, f"testcase_{index+1}.log"
|
||||
)
|
||||
testcase_summary["log"] = logs_file_abs_path
|
||||
|
||||
@@ -209,10 +209,13 @@ class HttpRunner(object):
|
||||
""" run testcase/testsuite data
|
||||
"""
|
||||
capture_message("start to run tests")
|
||||
self.project_mapping = tests_mapping.get("project_mapping", {})
|
||||
self.test_path = tests_mapping.get("project_mapping", {}).get("test_path", "")
|
||||
|
||||
if self.save_tests:
|
||||
utils.dump_logs(tests_mapping, self.project_mapping, "loaded")
|
||||
utils.dump_json_file(
|
||||
tests_mapping,
|
||||
utils.prepare_log_file_abs_path(self.test_path, "loaded.json")
|
||||
)
|
||||
|
||||
# parse tests
|
||||
self.exception_stage = "parse tests"
|
||||
@@ -220,14 +223,20 @@ class HttpRunner(object):
|
||||
parse_failed_testfiles = parser.get_parse_failed_testfiles()
|
||||
if parse_failed_testfiles:
|
||||
logger.warning("parse failures occurred ...")
|
||||
utils.dump_logs(parse_failed_testfiles, self.project_mapping, "parse_failed")
|
||||
utils.dump_json_file(
|
||||
parse_failed_testfiles,
|
||||
utils.prepare_log_file_abs_path(self.test_path, "parse_failed.json")
|
||||
)
|
||||
|
||||
if len(parsed_testcases) == 0:
|
||||
logger.error("failed to parse all cases, abort.")
|
||||
raise exceptions.ParseTestsFailure
|
||||
|
||||
if self.save_tests:
|
||||
utils.dump_logs(parsed_testcases, self.project_mapping, "parsed")
|
||||
utils.dump_json_file(
|
||||
parsed_testcases,
|
||||
utils.prepare_log_file_abs_path(self.test_path, "parsed.json")
|
||||
)
|
||||
|
||||
# add tests to test suite
|
||||
self.exception_stage = "add tests to test suite"
|
||||
@@ -246,10 +255,16 @@ class HttpRunner(object):
|
||||
report.stringify_summary(self._summary)
|
||||
|
||||
if self.save_tests:
|
||||
utils.dump_logs(self._summary, self.project_mapping, "summary")
|
||||
utils.dump_json_file(
|
||||
self._summary,
|
||||
utils.prepare_log_file_abs_path(self.test_path, "summary.json")
|
||||
)
|
||||
# save variables and export data
|
||||
vars_out = self.get_vars_out()
|
||||
utils.dump_logs(vars_out, self.project_mapping, "io")
|
||||
utils.dump_json_file(
|
||||
vars_out,
|
||||
utils.prepare_log_file_abs_path(self.test_path, "io.json")
|
||||
)
|
||||
|
||||
return self._summary
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ import json
|
||||
import os.path
|
||||
import re
|
||||
import uuid
|
||||
from typing import Union
|
||||
|
||||
import sentry_sdk
|
||||
from loguru import logger
|
||||
@@ -533,7 +534,7 @@ def omit_long_data(body, omit_len=512):
|
||||
return omitted_body + appendix_str
|
||||
|
||||
|
||||
def dump_json_file(json_data, json_file_abs_path):
|
||||
def dump_json_file(json_data: Union[dict, list], json_file_abs_path: str) -> None:
|
||||
""" dump json data to file
|
||||
"""
|
||||
class PythonObjectEncoder(json.JSONEncoder):
|
||||
@@ -566,11 +567,10 @@ def dump_json_file(json_data, json_file_abs_path):
|
||||
logger.error(msg)
|
||||
|
||||
|
||||
def prepare_log_file_abs_path(project_mapping, file_name):
|
||||
def prepare_log_file_abs_path(test_path: str, file_name: str) -> str:
|
||||
""" prepare dump json file absolute path.
|
||||
"""
|
||||
current_working_dir = os.getcwd()
|
||||
test_path = project_mapping.get("test_path")
|
||||
|
||||
if not test_path:
|
||||
# running passed in testcase/testsuite data structure
|
||||
@@ -592,17 +592,3 @@ def prepare_log_file_abs_path(project_mapping, file_name):
|
||||
|
||||
dumped_json_file_abs_path = os.path.join(file_foder_path, dump_file_name)
|
||||
return dumped_json_file_abs_path
|
||||
|
||||
|
||||
def dump_logs(json_data, project_mapping, tag_name):
|
||||
""" dump tests data to json file.
|
||||
the dumped file is located in PWD/logs folder.
|
||||
|
||||
Args:
|
||||
json_data (list/dict): json data to dump
|
||||
project_mapping (dict): project info
|
||||
tag_name (str): tag name, loaded/parsed/summary
|
||||
|
||||
"""
|
||||
json_file_abs_path = prepare_log_file_abs_path(project_mapping, f"{tag_name}.json")
|
||||
dump_json_file(json_data, json_file_abs_path)
|
||||
|
||||
@@ -278,30 +278,23 @@ class TestUtils(unittest.TestCase):
|
||||
|
||||
def test_prepare_dump_json_file_path_for_folder(self):
|
||||
# hrun tests/httpbin/a.b.c/ --save-tests
|
||||
project_mapping = {
|
||||
"test_path": os.path.join("tests", "httpbin", "a.b.c")
|
||||
}
|
||||
test_path = os.path.join("tests", "httpbin", "a.b.c")
|
||||
self.assertEqual(
|
||||
utils.prepare_log_file_abs_path(project_mapping, "loaded"),
|
||||
utils.prepare_log_file_abs_path(test_path, "loaded.json"),
|
||||
os.path.join(os.getcwd(), "logs", "tests/httpbin/a.b.c/all.loaded.json")
|
||||
)
|
||||
|
||||
def test_prepare_dump_json_file_path_for_file(self):
|
||||
# hrun tests/httpbin/a.b.c/rpc.yml --save-tests
|
||||
project_mapping = {
|
||||
"test_path": os.path.join("tests", "httpbin", "a.b.c", "rpc.yml")
|
||||
}
|
||||
test_path = os.path.join("tests", "httpbin", "a.b.c", "rpc.yml")
|
||||
self.assertEqual(
|
||||
utils.prepare_log_file_abs_path(project_mapping, "loaded"),
|
||||
utils.prepare_log_file_abs_path(test_path, "loaded.json"),
|
||||
os.path.join(os.getcwd(), "logs", "tests/httpbin/a.b.c/rpc.loaded.json")
|
||||
)
|
||||
|
||||
def test_prepare_dump_json_file_path_for_passed_testcase(self):
|
||||
project_working_directory = os.path.join(os.getcwd(), "tests")
|
||||
project_mapping = {
|
||||
"PWD": project_working_directory
|
||||
}
|
||||
test_path = ""
|
||||
self.assertEqual(
|
||||
utils.prepare_log_file_abs_path(project_mapping, "loaded"),
|
||||
utils.prepare_log_file_abs_path(test_path, "loaded.json"),
|
||||
os.path.join(os.getcwd(), "logs", "tests_mapping.loaded.json")
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user