From 841c74bcfa9e0016667d6c13aab9dd423a482665 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Wed, 1 Jan 2020 10:37:16 +0800 Subject: [PATCH] feat: add json schema validation for testcase v1 --- docs/CHANGELOG.md | 2 +- httprunner/loader/buildup.py | 1 + httprunner/loader/check.py | 10 ++ .../loader/schemas/testcase.schema.v1.json | 138 ++++++++++++++++++ 4 files changed, 150 insertions(+), 1 deletion(-) create mode 100644 httprunner/loader/schemas/testcase.schema.v1.json diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index a193c99f..8cd3202c 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -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** diff --git a/httprunner/loader/buildup.py b/httprunner/loader/buildup.py index 490d3208..edc8e92b 100644 --- a/httprunner/loader/buildup.py +++ b/httprunner/loader/buildup.py @@ -175,6 +175,7 @@ def load_testcase(raw_testcase): } """ + JsonSchemaChecker.validate_testcase_v1_format(raw_testcase) config = {} tests = [] diff --git a/httprunner/loader/check.py b/httprunner/loader/check.py index 44a0f721..58c1c90b 100644 --- a/httprunner/loader/check.py +++ b/httprunner/loader/check.py @@ -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 diff --git a/httprunner/loader/schemas/testcase.schema.v1.json b/httprunner/loader/schemas/testcase.schema.v1.json new file mode 100644 index 00000000..823399cc --- /dev/null +++ b/httprunner/loader/schemas/testcase.schema.v1.json @@ -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 +} \ No newline at end of file