feat: add json schema validation for testcase v1

This commit is contained in:
debugtalk
2020-01-01 10:37:16 +08:00
parent dc2487d418
commit 841c74bcfa
4 changed files with 150 additions and 1 deletions

View File

@@ -5,7 +5,7 @@
**Added**
- feat: add json schema validation for api
- feat: add json schema validation for testcase v2
- feat: add json schema validation for testcase v1 & v2
- feat: add json schema validation for testsuite v2
**Changed**

View File

@@ -175,6 +175,7 @@ def load_testcase(raw_testcase):
}
"""
JsonSchemaChecker.validate_testcase_v1_format(raw_testcase)
config = {}
tests = []

View File

@@ -9,6 +9,7 @@ from httprunner import exceptions, logger
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_v1_path = os.path.join(schemas_root_dir, "testcase.schema.v1.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")
@@ -19,6 +20,9 @@ with open(common_schema_path) as f:
common_schema = json.load(f)
resolver = jsonschema.RefResolver("file://{}/".format(os.path.abspath(schemas_root_dir)), common_schema)
with open(testcase_schema_v1_path) as f:
testcase_schema_v1 = json.load(f)
with open(testcase_schema_v2_path) as f:
testcase_schema_v2 = json.load(f)
@@ -43,6 +47,12 @@ class JsonSchemaChecker(object):
def validate_testcase_v1_format(content):
""" check testcase format v1 if valid
"""
try:
jsonschema.validate(content, testcase_schema_v1, resolver=resolver)
except jsonschema.exceptions.ValidationError as ex:
logger.log_error(str(ex))
raise exceptions.FileFormatError
return True
@staticmethod

View File

@@ -0,0 +1,138 @@
{
"$schema": "http://json-schema.org/draft-07/schema",
"description": "httprunner testcase schema v1 definition",
"type": "array",
"definitions": {
"test": {
"type": "object",
"oneOf": [
{
"properties": {
"name": {
"$ref": "common.schema.json#/definitions/name"
},
"request": {
"description": "define api request directly",
"$ref": "common.schema.json#/definitions/request"
},
"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",
"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"
]
}
]
}
},
"items": {
"type": "object",
"oneOf": [
{
"type": "object",
"properties": {
"config": {
"$ref": "common.schema.json#/definitions/config"
}
},
"additionalProperties": false
},
{
"type": "object",
"properties": {
"test": {
"$ref": "testcase.schema.v1.json#/definitions/test"
}
},
"additionalProperties": false
}
],
"minProperties": 1,
"maxProperties": 1
},
"minItems": 2
}