diff --git a/httprunner/exceptions.py b/httprunner/exceptions.py index 77d1be52..66e8f7a5 100644 --- a/httprunner/exceptions.py +++ b/httprunner/exceptions.py @@ -40,6 +40,10 @@ class FileFormatError(MyBaseError): pass +class TestCaseFormatError(MyBaseError): + pass + + class ParamsError(MyBaseError): pass diff --git a/httprunner/ext/make/__init__.py b/httprunner/ext/make/__init__.py index 37ca4f46..bab9ba80 100644 --- a/httprunner/ext/make/__init__.py +++ b/httprunner/ext/make/__init__.py @@ -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): diff --git a/httprunner/loader_test.py b/httprunner/loader_test.py index e66d47ad..1296ee84 100644 --- a/httprunner/loader_test.py +++ b/httprunner/loader_test.py @@ -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) diff --git a/httprunner/new_loader.py b/httprunner/new_loader.py index d58f8ad8..0a559dd8 100644 --- a/httprunner/new_loader.py +++ b/httprunner/new_loader.py @@ -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