mirror of
https://github.com/httprunner/httprunner.git
synced 2026-07-06 15:01:22 +08:00
add testcase content check
This commit is contained in:
@@ -4,7 +4,7 @@ import os
|
|||||||
import sys
|
import sys
|
||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
|
|
||||||
from ate import __version__
|
from ate import __version__, exception
|
||||||
from ate.task import TaskSuite
|
from ate.task import TaskSuite
|
||||||
from ate.utils import create_scaffold
|
from ate.utils import create_scaffold
|
||||||
|
|
||||||
@@ -62,7 +62,12 @@ def main_ate():
|
|||||||
for testset_path in set(args.testset_paths):
|
for testset_path in set(args.testset_paths):
|
||||||
|
|
||||||
testset_path = testset_path.rstrip('/')
|
testset_path = testset_path.rstrip('/')
|
||||||
task_suite = TaskSuite(testset_path)
|
|
||||||
|
try:
|
||||||
|
task_suite = TaskSuite(testset_path)
|
||||||
|
except exception.FileFormatError:
|
||||||
|
success = False
|
||||||
|
continue
|
||||||
|
|
||||||
output_folder_name = os.path.basename(os.path.splitext(testset_path)[0])
|
output_folder_name = os.path.basename(os.path.splitext(testset_path)[0])
|
||||||
kwargs = {
|
kwargs = {
|
||||||
|
|||||||
@@ -7,6 +7,9 @@ except NameError:
|
|||||||
class MyBaseError(BaseException):
|
class MyBaseError(BaseException):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
class FileFormatError(MyBaseError):
|
||||||
|
pass
|
||||||
|
|
||||||
class ParamsError(MyBaseError):
|
class ParamsError(MyBaseError):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import ast
|
import ast
|
||||||
|
import logging
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
|
|
||||||
@@ -326,6 +327,21 @@ def substitute_variables_with_mapping(content, mapping):
|
|||||||
|
|
||||||
return content
|
return content
|
||||||
|
|
||||||
|
def check_format(file_path, content):
|
||||||
|
""" check testcase format if valid
|
||||||
|
"""
|
||||||
|
if not content:
|
||||||
|
# testcase file content is empty
|
||||||
|
err_msg = "Testcase file content is empty: {}".format(file_path)
|
||||||
|
logging.error(err_msg)
|
||||||
|
raise exception.FileFormatError(err_msg)
|
||||||
|
|
||||||
|
elif not isinstance(content, (list, dict)):
|
||||||
|
# testcase file content does not match testcase format
|
||||||
|
err_msg = "Testcase file content format invalid: {}".format(file_path)
|
||||||
|
logging.error(err_msg)
|
||||||
|
raise exception.FileFormatError(err_msg)
|
||||||
|
|
||||||
|
|
||||||
class TestcaseParser(object):
|
class TestcaseParser(object):
|
||||||
|
|
||||||
|
|||||||
20
ate/utils.py
20
ate/utils.py
@@ -13,7 +13,7 @@ import types
|
|||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
|
|
||||||
import yaml
|
import yaml
|
||||||
from ate import exception
|
from ate import exception, testcase
|
||||||
from requests.structures import CaseInsensitiveDict
|
from requests.structures import CaseInsensitiveDict
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@@ -42,12 +42,26 @@ def get_sign(*args):
|
|||||||
return sign
|
return sign
|
||||||
|
|
||||||
def load_yaml_file(yaml_file):
|
def load_yaml_file(yaml_file):
|
||||||
|
""" load yaml file and check file content format
|
||||||
|
"""
|
||||||
with codecs.open(yaml_file, 'r+', encoding='utf-8') as stream:
|
with codecs.open(yaml_file, 'r+', encoding='utf-8') as stream:
|
||||||
return yaml.load(stream)
|
yaml_content = yaml.load(stream)
|
||||||
|
testcase.check_format(yaml_file, yaml_content)
|
||||||
|
return yaml_content
|
||||||
|
|
||||||
def load_json_file(json_file):
|
def load_json_file(json_file):
|
||||||
|
""" load json file and check file content format
|
||||||
|
"""
|
||||||
with codecs.open(json_file, encoding='utf-8') as data_file:
|
with codecs.open(json_file, encoding='utf-8') as data_file:
|
||||||
return json.load(data_file)
|
try:
|
||||||
|
json_content = json.load(data_file)
|
||||||
|
except json.decoder.JSONDecodeError:
|
||||||
|
err_msg = "JSONDecodeError: JSON file format error: {}".format(json_file)
|
||||||
|
logging.error(err_msg)
|
||||||
|
raise exception.FileFormatError(err_msg)
|
||||||
|
|
||||||
|
testcase.check_format(json_file, json_content)
|
||||||
|
return json_content
|
||||||
|
|
||||||
def load_tests(testcase_file_path):
|
def load_tests(testcase_file_path):
|
||||||
file_suffix = os.path.splitext(testcase_file_path)[1]
|
file_suffix = os.path.splitext(testcase_file_path)[1]
|
||||||
|
|||||||
@@ -34,6 +34,55 @@ class TestUtils(ApiServerUnittest):
|
|||||||
self.assertIn('url', testcase['request'])
|
self.assertIn('url', testcase['request'])
|
||||||
self.assertIn('method', testcase['request'])
|
self.assertIn('method', testcase['request'])
|
||||||
|
|
||||||
|
def test_load_yaml_file_file_format_error(self):
|
||||||
|
yaml_tmp_file = "tests/data/tmp.yml"
|
||||||
|
# create empty yaml file
|
||||||
|
with open(yaml_tmp_file, 'w') as f:
|
||||||
|
f.write("")
|
||||||
|
|
||||||
|
with self.assertRaises(exception.FileFormatError):
|
||||||
|
utils.load_yaml_file(yaml_tmp_file)
|
||||||
|
|
||||||
|
os.remove(yaml_tmp_file)
|
||||||
|
|
||||||
|
# create invalid format yaml file
|
||||||
|
with open(yaml_tmp_file, 'w') as f:
|
||||||
|
f.write("abc")
|
||||||
|
|
||||||
|
with self.assertRaises(exception.FileFormatError):
|
||||||
|
utils.load_yaml_file(yaml_tmp_file)
|
||||||
|
|
||||||
|
os.remove(yaml_tmp_file)
|
||||||
|
|
||||||
|
def test_load_json_file_file_format_error(self):
|
||||||
|
json_tmp_file = "tests/data/tmp.json"
|
||||||
|
# create empty file
|
||||||
|
with open(json_tmp_file, 'w') as f:
|
||||||
|
f.write("")
|
||||||
|
|
||||||
|
with self.assertRaises(exception.FileFormatError):
|
||||||
|
utils.load_json_file(json_tmp_file)
|
||||||
|
|
||||||
|
os.remove(json_tmp_file)
|
||||||
|
|
||||||
|
# create empty json file
|
||||||
|
with open(json_tmp_file, 'w') as f:
|
||||||
|
f.write("{}")
|
||||||
|
|
||||||
|
with self.assertRaises(exception.FileFormatError):
|
||||||
|
utils.load_json_file(json_tmp_file)
|
||||||
|
|
||||||
|
os.remove(json_tmp_file)
|
||||||
|
|
||||||
|
# create invalid format json file
|
||||||
|
with open(json_tmp_file, 'w') as f:
|
||||||
|
f.write("abc")
|
||||||
|
|
||||||
|
with self.assertRaises(exception.FileFormatError):
|
||||||
|
utils.load_json_file(json_tmp_file)
|
||||||
|
|
||||||
|
os.remove(json_tmp_file)
|
||||||
|
|
||||||
def test_load_folder_files(self):
|
def test_load_folder_files(self):
|
||||||
folder = os.path.join(os.getcwd(), 'tests')
|
folder = os.path.join(os.getcwd(), 'tests')
|
||||||
file1 = os.path.join(os.getcwd(), 'tests', 'test_utils.py')
|
file1 = os.path.join(os.getcwd(), 'tests', 'test_utils.py')
|
||||||
@@ -50,7 +99,6 @@ class TestUtils(ApiServerUnittest):
|
|||||||
api_file = os.path.join(os.getcwd(), 'tests', 'api', 'demo.yml')
|
api_file = os.path.join(os.getcwd(), 'tests', 'api', 'demo.yml')
|
||||||
self.assertEqual(files_1[0], api_file)
|
self.assertEqual(files_1[0], api_file)
|
||||||
|
|
||||||
folder_list = [folder, folder]
|
|
||||||
files_2 = utils.load_folder_files(folder)
|
files_2 = utils.load_folder_files(folder)
|
||||||
api_file = os.path.join(os.getcwd(), 'tests', 'api', 'demo.yml')
|
api_file = os.path.join(os.getcwd(), 'tests', 'api', 'demo.yml')
|
||||||
self.assertEqual(files_2[0], api_file)
|
self.assertEqual(files_2[0], api_file)
|
||||||
|
|||||||
Reference in New Issue
Block a user