mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-12 02:21:29 +08:00
feat: add json schema validation for testsuite
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
|
||||
- feat: add json schema validation for api
|
||||
- feat: add json schema validation for testcase v2
|
||||
- feat: add json schema validation for testsuite
|
||||
|
||||
**Changed**
|
||||
|
||||
|
||||
@@ -281,6 +281,7 @@ def load_testsuite(raw_testsuite):
|
||||
}
|
||||
|
||||
"""
|
||||
JsonSchemaChecker.validate_testsuite_format(raw_testsuite)
|
||||
raw_testcases = raw_testsuite.pop("testcases")
|
||||
raw_testsuite["testcases"] = {}
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ schemas_root_dir = os.path.join(os.path.dirname(__file__), "schemas")
|
||||
common_schema_path = os.path.join(schemas_root_dir, "common.schema.json")
|
||||
api_schema_path = os.path.join(schemas_root_dir, "api.schema.json")
|
||||
testcase_schema_v2_path = os.path.join(schemas_root_dir, "testcase.schema.v2.json")
|
||||
testsuite_schema_v2_path = os.path.join(schemas_root_dir, "testsuite.schema.v2.json")
|
||||
testsuite_schema_path = os.path.join(schemas_root_dir, "testsuite.schema.json")
|
||||
|
||||
with open(api_schema_path) as f:
|
||||
api_schema = json.load(f)
|
||||
@@ -22,8 +22,8 @@ with open(common_schema_path) as f:
|
||||
with open(testcase_schema_v2_path) as f:
|
||||
testcase_schema_v2 = json.load(f)
|
||||
|
||||
with open(testsuite_schema_v2_path) as f:
|
||||
testsuite_schema_v2 = json.load(f)
|
||||
with open(testsuite_schema_path) as f:
|
||||
testsuite_schema = json.load(f)
|
||||
|
||||
|
||||
class JsonSchemaChecker(object):
|
||||
@@ -39,6 +39,16 @@ class JsonSchemaChecker(object):
|
||||
|
||||
return True
|
||||
|
||||
@staticmethod
|
||||
def validate_testsuite_format(content):
|
||||
try:
|
||||
jsonschema.validate(content, testsuite_schema, resolver=resolver)
|
||||
except jsonschema.exceptions.ValidationError as ex:
|
||||
logger.log_error(str(ex))
|
||||
raise exceptions.FileFormatError
|
||||
|
||||
return True
|
||||
|
||||
|
||||
class JsonSchemaV1Checker(JsonSchemaChecker):
|
||||
pass
|
||||
@@ -58,16 +68,6 @@ class JsonSchemaV2Checker(JsonSchemaChecker):
|
||||
|
||||
return True
|
||||
|
||||
@staticmethod
|
||||
def validate_testsuite_format(content):
|
||||
try:
|
||||
jsonschema.validate(content, testsuite_schema_v2, resolver=resolver)
|
||||
except jsonschema.exceptions.ValidationError as ex:
|
||||
logger.log_error(str(ex))
|
||||
raise exceptions.FileFormatError
|
||||
|
||||
return True
|
||||
|
||||
|
||||
def is_testcase(data_structure):
|
||||
""" check if data_structure is a testcase.
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"$schema": "http://json-schema.org/draft-07/schema",
|
||||
"description": "httprunner testcase schema definition",
|
||||
"description": "httprunner testcase schema v2 definition",
|
||||
"type": "object",
|
||||
"definitions": {
|
||||
"teststep": {
|
||||
|
||||
46
httprunner/loader/schemas/testsuite.schema.json
Normal file
46
httprunner/loader/schemas/testsuite.schema.json
Normal file
@@ -0,0 +1,46 @@
|
||||
{
|
||||
"$schema": "http://json-schema.org/draft-07/schema",
|
||||
"description": "httprunner testsuite schema v2 definition",
|
||||
"type": "object",
|
||||
"definitions": {
|
||||
"testcase": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"name": {
|
||||
"$ref": "common.schema.json#/definitions/name"
|
||||
},
|
||||
"variables": {
|
||||
"$ref": "common.schema.json#/definitions/variables"
|
||||
},
|
||||
"parameters": {
|
||||
"description": "generate cartesian product variables with parameters, each group of variables will be run once",
|
||||
"type": "object"
|
||||
},
|
||||
"testcase": {
|
||||
"description": "testcase reference, value is testcase file relative path",
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"testcase"
|
||||
]
|
||||
}
|
||||
},
|
||||
"properties": {
|
||||
"config": {
|
||||
"$ref": "common.schema.json#/definitions/config"
|
||||
},
|
||||
"testcases": {
|
||||
"description": "testcase of a testsuite",
|
||||
"type": "array",
|
||||
"minItems": 1,
|
||||
"items": {
|
||||
"$ref": "testsuite.schema.json#/definitions/testcase"
|
||||
}
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"config",
|
||||
"testcases"
|
||||
]
|
||||
}
|
||||
@@ -1,58 +0,0 @@
|
||||
{
|
||||
"$schema": "http://json-schema.org/draft-07/schema",
|
||||
"$id": "https://raw.githubusercontent.com/httprunner/httprunner/master/httprunner/loader/schemas/v2/testsuite.schema.json",
|
||||
"title": "Testsuite for httprunner",
|
||||
"description": "Testsuite for httprunner",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"config": {
|
||||
"allOf": [
|
||||
{
|
||||
"$ref": "https://raw.githubusercontent.com/httprunner/httprunner/master/httprunner/loader/schemas/common/config.schema.json"
|
||||
}
|
||||
],
|
||||
"properties": {
|
||||
"verify": {
|
||||
"type": "boolean"
|
||||
}
|
||||
}
|
||||
},
|
||||
"testcases": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"$ref": "#/definitions/testcase"
|
||||
}
|
||||
},
|
||||
"setup_hooks": {
|
||||
"$ref": "https://raw.githubusercontent.com/httprunner/httprunner/master/httprunner/loader/schemas/common/hook.schema.json"
|
||||
},
|
||||
"teardown_hooks": {
|
||||
"$ref": "https://raw.githubusercontent.com/httprunner/httprunner/master/httprunner/loader/schemas/common/hook.schema.json"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"testcases"
|
||||
],
|
||||
"definitions": {
|
||||
"testcase": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"name": {
|
||||
"description": "Testcase name",
|
||||
"type": "string"
|
||||
},
|
||||
"parameters": {
|
||||
"description": "Parameters will generate cartesian product variables, each set of variables is tested once",
|
||||
"type": "object"
|
||||
},
|
||||
"testcase": {
|
||||
"description": "Testcase reference, it's usually the relative path of the testcase",
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"testcase"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user