From 5f66dc069d64041e6a8f57bf21e9468e998e1bc6 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Wed, 1 Jan 2020 21:50:00 +0800 Subject: [PATCH] feat: add json schema validation for testsutie v1 --- docs/CHANGELOG.md | 2 +- httprunner/loader/check.py | 10 +++ httprunner/loader/schemas/common.schema.json | 71 +++++++++++-------- .../loader/schemas/testsuite.schema.v1.json | 66 +++++++++++++++++ 4 files changed, 117 insertions(+), 32 deletions(-) create mode 100644 httprunner/loader/schemas/testsuite.schema.v1.json diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 8cd3202c..f2d1a1c9 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -6,7 +6,7 @@ - feat: add json schema validation for api - feat: add json schema validation for testcase v1 & v2 -- feat: add json schema validation for testsuite v2 +- feat: add json schema validation for testsuite v1 & v2 **Changed** diff --git a/httprunner/loader/check.py b/httprunner/loader/check.py index 58c1c90b..cf9b9ad8 100644 --- a/httprunner/loader/check.py +++ b/httprunner/loader/check.py @@ -11,6 +11,7 @@ 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_v1_path = os.path.join(schemas_root_dir, "testsuite.schema.v1.json") testsuite_schema_v2_path = os.path.join(schemas_root_dir, "testsuite.schema.v2.json") with open(api_schema_path) as f: @@ -26,6 +27,9 @@ with open(testcase_schema_v1_path) as f: with open(testcase_schema_v2_path) as f: testcase_schema_v2 = json.load(f) +with open(testsuite_schema_v1_path) as f: + testsuite_schema_v1 = json.load(f) + with open(testsuite_schema_v2_path) as f: testsuite_schema_v2 = json.load(f) @@ -71,6 +75,12 @@ class JsonSchemaChecker(object): def validate_testsuite_v1_format(content): """ check testsuite format v1 if valid """ + try: + jsonschema.validate(content, testsuite_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/common.schema.json b/httprunner/loader/schemas/common.schema.json index 9d132b51..b2e09835 100644 --- a/httprunner/loader/schemas/common.schema.json +++ b/httprunner/loader/schemas/common.schema.json @@ -12,7 +12,19 @@ }, "variables": { "description": "define variables for api/teststep/testcase/testsuite", - "type": "object" + "oneOf": [ + { + "type": "object" + }, + { + "type": "array", + "items": { + "type": "object", + "maxProperties": 1, + "minProperties": 1 + } + } + ] }, "config": { "description": "used in testcase/testsuite to configure common fields", @@ -207,37 +219,34 @@ "type": "array", "items": { "description": "one validator definition", - "type": "object", - "propertyNames": { - "enum": [ - "eq", "equals", "==", "is", - "lt", "less_than", - "le", "less_than_or_equals", - "gt", "greater_than", - "ge", "greater_than_or_equals", - "ne", "not_equals", - "str_eq", "string_equals", - "len_eq", "length_equals", "count_eq", - "len_gt", "count_gt", "length_greater_than", "count_greater_than", - "len_ge", "count_ge", "length_greater_than_or_equals", "count_greater_than_or_equals", - "len_lt", "count_lt", "length_less_than", "count_less_than", - "len_le", "count_le", "length_less_than_or_equals", "count_less_than_or_equals", - "contains", - "contained_by", - "type_match", - "regex_match", - "startswith", - "endswith" - ] - }, - "patternProperties": { - "^[A-Za-z_][A-Za-z0-9_]*$": { - "description": "validate_func_name: [check_value, expect_value]", - "type": "array", - "minItems": 2, - "maxItems": 2 + "oneOf": [ + { + "type": "object", + "properties": { + "check": { + "type": "string" + }, + "comparator": { + "type": "string" + }, + "expect": { + "description": "expected value" + } + }, + "required": ["check", "expect"] + }, + { + "type": "object", + "patternProperties": { + "^[A-Za-z_][A-Za-z0-9_]*$": { + "description": "validate_func_name: [check_value, expect_value]", + "type": "array", + "minItems": 2, + "maxItems": 2 + } + } } - } + ] } } } diff --git a/httprunner/loader/schemas/testsuite.schema.v1.json b/httprunner/loader/schemas/testsuite.schema.v1.json new file mode 100644 index 00000000..af9fda01 --- /dev/null +++ b/httprunner/loader/schemas/testsuite.schema.v1.json @@ -0,0 +1,66 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema", + "description": "httprunner testsuite schema v1 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": "object", + "minProperties": 1, + "patternProperties": { + ".*": { + "description": "testcase definition", + "$ref": "testsuite.schema.v1.json#/definitions/testcase" + } + } + } + }, + "required": [ + "config", + "testcases" + ], + "examples": [ + { + "config": { + "name": "testsuite name" + }, + "testcases": { + "testcase 1": { + "name": "xxx", + "testcase": "/path/to/testcase1" + }, + "testcase 2": { + "name": "xxx", + "testcase": "/path/to/testcase2" + } + } + } + ] +} \ No newline at end of file