mirror of
https://github.com/httprunner/httprunner.git
synced 2026-07-08 21:51:43 +08:00
fix: json schema validation for testcase v2
This commit is contained in:
@@ -281,19 +281,21 @@ def load_testsuite(raw_testsuite):
|
|||||||
}
|
}
|
||||||
|
|
||||||
"""
|
"""
|
||||||
raw_testcases = raw_testsuite.pop("testcases")
|
raw_testcases = raw_testsuite["testcases"]
|
||||||
raw_testsuite["testcases"] = {}
|
|
||||||
|
|
||||||
if isinstance(raw_testcases, dict):
|
if isinstance(raw_testcases, dict):
|
||||||
# make compatible with version < 2.2.0
|
# format version 1, make compatible with version < 2.2.0
|
||||||
|
JsonSchemaChecker.validate_testsuite_v1_format(raw_testsuite)
|
||||||
|
raw_testsuite["testcases"] = {}
|
||||||
for name, raw_testcase in raw_testcases.items():
|
for name, raw_testcase in raw_testcases.items():
|
||||||
__extend_with_testcase_ref(raw_testcase)
|
__extend_with_testcase_ref(raw_testcase)
|
||||||
raw_testcase.setdefault("name", name)
|
raw_testcase.setdefault("name", name)
|
||||||
raw_testsuite["testcases"][name] = raw_testcase
|
raw_testsuite["testcases"][name] = raw_testcase
|
||||||
|
|
||||||
elif isinstance(raw_testcases, list):
|
elif isinstance(raw_testcases, list):
|
||||||
JsonSchemaChecker.validate_testsuite_v2_format(raw_testsuite)
|
|
||||||
# format version 2, implemented in 2.2.0
|
# format version 2, implemented in 2.2.0
|
||||||
|
JsonSchemaChecker.validate_testsuite_v2_format(raw_testsuite)
|
||||||
|
raw_testsuite["testcases"] = {}
|
||||||
for raw_testcase in raw_testcases:
|
for raw_testcase in raw_testcases:
|
||||||
__extend_with_testcase_ref(raw_testcase)
|
__extend_with_testcase_ref(raw_testcase)
|
||||||
testcase_name = raw_testcase["name"]
|
testcase_name = raw_testcase["name"]
|
||||||
|
|||||||
@@ -57,6 +57,12 @@ class JsonSchemaChecker(object):
|
|||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def validate_testsuite_v1_format(content):
|
||||||
|
""" check testsuite format v1 if valid
|
||||||
|
"""
|
||||||
|
return True
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def validate_testsuite_v2_format(content):
|
def validate_testsuite_v2_format(content):
|
||||||
""" check testsuite format v2 if valid
|
""" check testsuite format v2 if valid
|
||||||
|
|||||||
@@ -175,13 +175,32 @@
|
|||||||
},
|
},
|
||||||
"extract": {
|
"extract": {
|
||||||
"description": "used to extract session variables for later requests",
|
"description": "used to extract session variables for later requests",
|
||||||
"type": "object",
|
"oneOf": [
|
||||||
"patternProperties": {
|
{
|
||||||
"^[A-Za-z_][A-Za-z0-9_]*$": {
|
"type": "object",
|
||||||
"description": "extraction rule for session variable, maybe in jsonpath/regex/jmespath",
|
"patternProperties": {
|
||||||
"type": "string"
|
"^[A-Za-z_][A-Za-z0-9_]*$": {
|
||||||
|
"description": "extraction rule for session variable, maybe in jsonpath/regex/jmespath",
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"minProperties": 1
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "array",
|
||||||
|
"items": {
|
||||||
|
"type": "object",
|
||||||
|
"patternProperties": {
|
||||||
|
"^[A-Za-z_][A-Za-z0-9_]*$": {
|
||||||
|
"description": "extraction rule for session variable, maybe in jsonpath/regex/jmespath",
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"minProperties": 1,
|
||||||
|
"maxProperties": 1
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
]
|
||||||
},
|
},
|
||||||
"validate": {
|
"validate": {
|
||||||
"description": "used to validate response fields",
|
"description": "used to validate response fields",
|
||||||
|
|||||||
@@ -6,42 +6,13 @@
|
|||||||
"teststep": {
|
"teststep": {
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"oneOf": [
|
"oneOf": [
|
||||||
{
|
|
||||||
"properties": {
|
|
||||||
"name": {
|
|
||||||
"$ref": "common.schema.json#/definitions/name"
|
|
||||||
},
|
|
||||||
"api": {
|
|
||||||
"description": "api reference, value is api file relative path",
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"variables": {
|
|
||||||
"$ref": "common.schema.json#/definitions/variables"
|
|
||||||
},
|
|
||||||
"extract": {
|
|
||||||
"$ref": "common.schema.json#/definitions/extract"
|
|
||||||
},
|
|
||||||
"validate": {
|
|
||||||
"$ref": "common.schema.json#/definitions/validate"
|
|
||||||
},
|
|
||||||
"setup_hooks": {
|
|
||||||
"$ref": "common.schema.json#/definitions/hook"
|
|
||||||
},
|
|
||||||
"teardown_hooks": {
|
|
||||||
"$ref": "common.schema.json#/definitions/hook"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"required": [
|
|
||||||
"name",
|
|
||||||
"api"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"properties": {
|
"properties": {
|
||||||
"name": {
|
"name": {
|
||||||
"$ref": "common.schema.json#/definitions/name"
|
"$ref": "common.schema.json#/definitions/name"
|
||||||
},
|
},
|
||||||
"request": {
|
"request": {
|
||||||
|
"description": "define api request directly",
|
||||||
"$ref": "common.schema.json#/definitions/request"
|
"$ref": "common.schema.json#/definitions/request"
|
||||||
},
|
},
|
||||||
"variables": {
|
"variables": {
|
||||||
@@ -64,6 +35,76 @@
|
|||||||
"name",
|
"name",
|
||||||
"request"
|
"request"
|
||||||
]
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"properties": {
|
||||||
|
"name": {
|
||||||
|
"$ref": "common.schema.json#/definitions/name"
|
||||||
|
},
|
||||||
|
"api": {
|
||||||
|
"description": "api reference, value is api file relative path",
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"variables": {
|
||||||
|
"$ref": "common.schema.json#/definitions/variables"
|
||||||
|
},
|
||||||
|
"extract": {
|
||||||
|
"oneOf": [
|
||||||
|
{
|
||||||
|
"type": "array",
|
||||||
|
"items": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"$ref": "common.schema.json#/definitions/extract"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"validate": {
|
||||||
|
"$ref": "common.schema.json#/definitions/validate"
|
||||||
|
},
|
||||||
|
"setup_hooks": {
|
||||||
|
"$ref": "common.schema.json#/definitions/hook"
|
||||||
|
},
|
||||||
|
"teardown_hooks": {
|
||||||
|
"$ref": "common.schema.json#/definitions/hook"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": [
|
||||||
|
"name",
|
||||||
|
"api"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"properties": {
|
||||||
|
"name": {
|
||||||
|
"$ref": "common.schema.json#/definitions/name"
|
||||||
|
},
|
||||||
|
"testcase": {
|
||||||
|
"description": "testcase reference, value is testcase file relative path",
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"variables": {
|
||||||
|
"$ref": "common.schema.json#/definitions/variables"
|
||||||
|
},
|
||||||
|
"extract": {
|
||||||
|
"type": "array",
|
||||||
|
"items": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"setup_hooks": {
|
||||||
|
"$ref": "common.schema.json#/definitions/hook"
|
||||||
|
},
|
||||||
|
"teardown_hooks": {
|
||||||
|
"$ref": "common.schema.json#/definitions/hook"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": [
|
||||||
|
"name",
|
||||||
|
"testcase"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -279,6 +279,7 @@ class TestSuiteLoader(unittest.TestCase):
|
|||||||
|
|
||||||
def test_load_project_tests(self):
|
def test_load_project_tests(self):
|
||||||
buildup.load_project_data(os.path.join(os.getcwd(), "tests"))
|
buildup.load_project_data(os.path.join(os.getcwd(), "tests"))
|
||||||
api_file_path = os.path.join(os.getcwd(), "tests", "api", "get_token.yml")
|
self.assertIn("gen_md5", self.project_mapping["functions"])
|
||||||
self.assertIn(api_file_path, self.tests_def_mapping["api"])
|
|
||||||
self.assertEqual(self.project_mapping["env"]["PROJECT_KEY"], "ABCDEFGH")
|
self.assertEqual(self.project_mapping["env"]["PROJECT_KEY"], "ABCDEFGH")
|
||||||
|
self.assertEqual(self.project_mapping["PWD"], os.path.dirname(os.path.dirname(__file__)))
|
||||||
|
self.assertEqual(self.project_mapping["test_path"], os.path.dirname(os.path.dirname(__file__)))
|
||||||
|
|||||||
Reference in New Issue
Block a user