fix: log error when make testcase failed

This commit is contained in:
debugtalk
2020-05-15 10:45:53 +08:00
4 changed files with 35 additions and 11 deletions

View File

@@ -40,6 +40,10 @@ class FileFormatError(MyBaseError):
pass pass
class TestCaseFormatError(MyBaseError):
pass
class ParamsError(MyBaseError): class ParamsError(MyBaseError):
pass pass

View File

@@ -1,9 +1,11 @@
import os import os
from typing import Union
import jinja2 import jinja2
from loguru import logger from loguru import logger
from httprunner import exceptions from httprunner import exceptions
from httprunner.exceptions import TestCaseFormatError
from httprunner.new_loader import load_testcase_file, load_folder_files from httprunner.new_loader import load_testcase_file, load_folder_files
__TMPL__ = """# NOTICE: Generated By HttpRunner. DO'NOT EDIT! __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}") 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__) template = jinja2.Template(__TMPL__)
raw_file_name, _ = os.path.splitext(os.path.basename(testcase_path)) raw_file_name, _ = os.path.splitext(os.path.basename(testcase_path))
@@ -66,10 +72,14 @@ def main_make(tests_path: str) -> list:
else: else:
raise exceptions.TestcaseNotFound(f"Invalid tests path: {tests_path}") raise exceptions.TestcaseNotFound(f"Invalid tests path: {tests_path}")
return [ testcase_path_list = []
make_testcase(testcase_path) for testcase_path in testcases:
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): def init_make_parser(subparsers):

View File

@@ -6,6 +6,8 @@ class TestLoader(unittest.TestCase):
def test_load_testcase_file(self): def test_load_testcase_file(self):
path = "examples/postman_echo/request_methods/request_with_variables.yml" path = "examples/postman_echo/request_methods/request_with_variables.yml"
testcase = load_testcase_file(path) testcase_json, testcase_obj = load_testcase_file(path)
self.assertEqual(testcase.config.name, "request methods testcase with variables") self.assertEqual(testcase_json["config"]["name"], "request methods testcase with variables")
self.assertEqual(len(testcase.teststeps), 3) 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)

View File

@@ -8,6 +8,7 @@ from typing import Tuple, Dict
import yaml import yaml
from loguru import logger from loguru import logger
from pydantic import ValidationError
from httprunner import builtin, utils from httprunner import builtin, utils
from httprunner import exceptions 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}" f"testcase file should be YAML/JSON format, invalid testcase file: {testcase_file}"
) )
# validate with pydantic TestCase model try:
testcase_obj = TestCase.parse_obj(testcase_content) # 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 testcase_obj.config.path = testcase_file
return testcase_content, testcase_obj return testcase_content, testcase_obj