mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-12 02:21:29 +08:00
fix: log error when make testcase failed
This commit is contained in:
@@ -40,6 +40,10 @@ class FileFormatError(MyBaseError):
|
||||
pass
|
||||
|
||||
|
||||
class TestCaseFormatError(MyBaseError):
|
||||
pass
|
||||
|
||||
|
||||
class ParamsError(MyBaseError):
|
||||
pass
|
||||
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
import os
|
||||
from typing import Union
|
||||
|
||||
import jinja2
|
||||
from loguru import logger
|
||||
|
||||
from httprunner import exceptions
|
||||
from httprunner.exceptions import TestCaseFormatError
|
||||
from httprunner.new_loader import load_testcase_file, load_folder_files
|
||||
|
||||
__TMPL__ = """# NOTICE: Generated By HttpRunner. DO'NOT EDIT!
|
||||
@@ -28,9 +30,13 @@ class {{ class_name }}(unittest.TestCase):
|
||||
"""
|
||||
|
||||
|
||||
def make_testcase(testcase_path: str) -> str:
|
||||
def make_testcase(testcase_path: str) -> Union[str, None]:
|
||||
logger.info(f"start to make testcase: {testcase_path}")
|
||||
testcase, _ = load_testcase_file(testcase_path)
|
||||
try:
|
||||
testcase, _ = load_testcase_file(testcase_path)
|
||||
except TestCaseFormatError:
|
||||
return None
|
||||
|
||||
template = jinja2.Template(__TMPL__)
|
||||
|
||||
raw_file_name, _ = os.path.splitext(os.path.basename(testcase_path))
|
||||
@@ -66,10 +72,14 @@ def main_make(tests_path: str) -> list:
|
||||
else:
|
||||
raise exceptions.TestcaseNotFound(f"Invalid tests path: {tests_path}")
|
||||
|
||||
return [
|
||||
make_testcase(testcase_path)
|
||||
for testcase_path in testcases
|
||||
]
|
||||
testcase_path_list = []
|
||||
for testcase_path in testcases:
|
||||
testcase_path = make_testcase(testcase_path)
|
||||
if not testcase_path:
|
||||
continue
|
||||
testcase_path_list.append(testcase_path)
|
||||
|
||||
return testcase_path_list
|
||||
|
||||
|
||||
def init_make_parser(subparsers):
|
||||
|
||||
@@ -6,6 +6,8 @@ class TestLoader(unittest.TestCase):
|
||||
|
||||
def test_load_testcase_file(self):
|
||||
path = "examples/postman_echo/request_methods/request_with_variables.yml"
|
||||
testcase = load_testcase_file(path)
|
||||
self.assertEqual(testcase.config.name, "request methods testcase with variables")
|
||||
self.assertEqual(len(testcase.teststeps), 3)
|
||||
testcase_json, testcase_obj = load_testcase_file(path)
|
||||
self.assertEqual(testcase_json["config"]["name"], "request methods testcase with variables")
|
||||
self.assertEqual(testcase_obj.config.name, "request methods testcase with variables")
|
||||
self.assertEqual(len(testcase_json["teststeps"]), 3)
|
||||
self.assertEqual(len(testcase_obj.teststeps), 3)
|
||||
|
||||
@@ -8,6 +8,7 @@ from typing import Tuple, Dict
|
||||
|
||||
import yaml
|
||||
from loguru import logger
|
||||
from pydantic import ValidationError
|
||||
|
||||
from httprunner import builtin, utils
|
||||
from httprunner import exceptions
|
||||
@@ -61,8 +62,15 @@ def load_testcase_file(testcase_file) -> Tuple[Dict, TestCase]:
|
||||
f"testcase file should be YAML/JSON format, invalid testcase file: {testcase_file}"
|
||||
)
|
||||
|
||||
# validate with pydantic TestCase model
|
||||
testcase_obj = TestCase.parse_obj(testcase_content)
|
||||
try:
|
||||
# validate with pydantic TestCase model
|
||||
testcase_obj = TestCase.parse_obj(testcase_content)
|
||||
except ValidationError as ex:
|
||||
err_msg = f"Invalid testcase format: {testcase_file}"
|
||||
logger.error(f"{err_msg}\n{ex}")
|
||||
raise exceptions.TestCaseFormatError(err_msg)
|
||||
|
||||
testcase_content["config"]["path"] = testcase_file
|
||||
testcase_obj.config.path = testcase_file
|
||||
|
||||
return testcase_content, testcase_obj
|
||||
|
||||
Reference in New Issue
Block a user