mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-12 19:39:44 +08:00
relocate testcase validator
This commit is contained in:
@@ -4,7 +4,7 @@ import json
|
||||
import os
|
||||
|
||||
import yaml
|
||||
from httprunner import exceptions, logger, parser, utils
|
||||
from httprunner import exceptions, logger, parser, utils, validator
|
||||
|
||||
###############################################################################
|
||||
## file loader
|
||||
@@ -399,7 +399,7 @@ def load_testcases(path):
|
||||
testcases_list = []
|
||||
|
||||
else:
|
||||
err_msg = "file not found: {}".format(path)
|
||||
err_msg = "path not exist: {}".format(path)
|
||||
logger.log_error(err_msg)
|
||||
raise exceptions.FileNotFound(err_msg)
|
||||
|
||||
@@ -412,5 +412,8 @@ def load(path):
|
||||
@param (str) path: testcase file/folder path
|
||||
@return (list) testcases list
|
||||
"""
|
||||
if validator.is_testcases(path):
|
||||
return path
|
||||
|
||||
_load_test_dependencies()
|
||||
return load_testcases(path)
|
||||
|
||||
@@ -177,10 +177,7 @@ def init_test_suites(path_or_testsets, mapping=None, http_client_session=None):
|
||||
mapping (dict):
|
||||
passed in variables mapping, it will override variables in config block
|
||||
"""
|
||||
if not testcase.is_testsets(path_or_testsets):
|
||||
testsets = loader.load(path_or_testsets)
|
||||
else:
|
||||
testsets = path_or_testsets
|
||||
testsets = loader.load(path_or_testsets)
|
||||
|
||||
# TODO: move comparator uniform here
|
||||
mapping = mapping or {}
|
||||
|
||||
@@ -32,47 +32,6 @@ def extract_functions(content):
|
||||
return []
|
||||
|
||||
|
||||
def is_testset(data_structure):
|
||||
""" check if data_structure is a testset
|
||||
testset should always be in the following data structure:
|
||||
{
|
||||
"name": "desc1",
|
||||
"config": {},
|
||||
"api": {},
|
||||
"testcases": [testcase11, testcase12]
|
||||
}
|
||||
"""
|
||||
if not isinstance(data_structure, dict):
|
||||
return False
|
||||
|
||||
if "name" not in data_structure or "testcases" not in data_structure:
|
||||
return False
|
||||
|
||||
if not isinstance(data_structure["testcases"], list):
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
def is_testsets(data_structure):
|
||||
""" check if data_structure is testset or testsets
|
||||
testsets should always be in the following data structure:
|
||||
testset_dict
|
||||
or
|
||||
[
|
||||
testset_dict_1,
|
||||
testset_dict_2
|
||||
]
|
||||
"""
|
||||
if not isinstance(data_structure, list):
|
||||
return is_testset(data_structure)
|
||||
|
||||
for item in data_structure:
|
||||
if not is_testset(item):
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
|
||||
def gen_cartesian_product(*args):
|
||||
""" generate cartesian product for lists
|
||||
@param
|
||||
|
||||
45
httprunner/validator.py
Normal file
45
httprunner/validator.py
Normal file
@@ -0,0 +1,45 @@
|
||||
# encoding: utf-8
|
||||
|
||||
""" validate data format
|
||||
TODO: refactor with JSON schema validate
|
||||
"""
|
||||
|
||||
def is_testcase(data_structure):
|
||||
""" check if data_structure is a testcase
|
||||
testcase should always be in the following data structure:
|
||||
{
|
||||
"name": "desc1",
|
||||
"config": {},
|
||||
"api": {},
|
||||
"testcases": [testcase11, testcase12]
|
||||
}
|
||||
"""
|
||||
if not isinstance(data_structure, dict):
|
||||
return False
|
||||
|
||||
if "name" not in data_structure or "testcases" not in data_structure:
|
||||
return False
|
||||
|
||||
if not isinstance(data_structure["testcases"], list):
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
def is_testcases(data_structure):
|
||||
""" check if data_structure is testcase or testcases list
|
||||
testsets should always be in the following data structure:
|
||||
testset_dict
|
||||
or
|
||||
[
|
||||
testset_dict_1,
|
||||
testset_dict_2
|
||||
]
|
||||
"""
|
||||
if not isinstance(data_structure, list):
|
||||
return is_testcase(data_structure)
|
||||
|
||||
for item in data_structure:
|
||||
if not is_testcase(item):
|
||||
return False
|
||||
|
||||
return True
|
||||
@@ -368,32 +368,3 @@ class TestcaseParserUnittest(unittest.TestCase):
|
||||
parsed_testcase["headers"]["sum"],
|
||||
3
|
||||
)
|
||||
|
||||
def test_is_testsets(self):
|
||||
data_structure = "path/to/file"
|
||||
self.assertFalse(testcase.is_testsets(data_structure))
|
||||
data_structure = ["path/to/file1", "path/to/file2"]
|
||||
self.assertFalse(testcase.is_testsets(data_structure))
|
||||
|
||||
data_structure = {
|
||||
"name": "desc1",
|
||||
"config": {},
|
||||
"api": {},
|
||||
"testcases": ["testcase11", "testcase12"]
|
||||
}
|
||||
self.assertTrue(data_structure)
|
||||
data_structure = [
|
||||
{
|
||||
"name": "desc1",
|
||||
"config": {},
|
||||
"api": {},
|
||||
"testcases": ["testcase11", "testcase12"]
|
||||
},
|
||||
{
|
||||
"name": "desc2",
|
||||
"config": {},
|
||||
"api": {},
|
||||
"testcases": ["testcase21", "testcase22"]
|
||||
}
|
||||
]
|
||||
self.assertTrue(data_structure)
|
||||
|
||||
35
tests/test_validator.py
Normal file
35
tests/test_validator.py
Normal file
@@ -0,0 +1,35 @@
|
||||
import unittest
|
||||
|
||||
from httprunner import validator
|
||||
|
||||
|
||||
class TestValidator(unittest.TestCase):
|
||||
|
||||
def test_is_testcases(self):
|
||||
data_structure = "path/to/file"
|
||||
self.assertFalse(validator.is_testcases(data_structure))
|
||||
data_structure = ["path/to/file1", "path/to/file2"]
|
||||
self.assertFalse(validator.is_testcases(data_structure))
|
||||
|
||||
data_structure = {
|
||||
"name": "desc1",
|
||||
"config": {},
|
||||
"api": {},
|
||||
"testcases": ["testcase11", "testcase12"]
|
||||
}
|
||||
self.assertTrue(data_structure)
|
||||
data_structure = [
|
||||
{
|
||||
"name": "desc1",
|
||||
"config": {},
|
||||
"api": {},
|
||||
"testcases": ["testcase11", "testcase12"]
|
||||
},
|
||||
{
|
||||
"name": "desc2",
|
||||
"config": {},
|
||||
"api": {},
|
||||
"testcases": ["testcase21", "testcase22"]
|
||||
}
|
||||
]
|
||||
self.assertTrue(data_structure)
|
||||
Reference in New Issue
Block a user