From c334c9f33520abe5efff0d53c6690d3a548e9e17 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Tue, 31 Dec 2019 18:57:20 +0800 Subject: [PATCH] feat: add json schema validation for testsuite --- docs/CHANGELOG.md | 1 + httprunner/loader/buildup.py | 1 + httprunner/loader/check.py | 26 ++++----- .../loader/schemas/testcase.schema.v2.json | 2 +- .../loader/schemas/testsuite.schema.json | 46 +++++++++++++++ .../loader/schemas/testsuite.schema.v2.json | 58 ------------------- 6 files changed, 62 insertions(+), 72 deletions(-) create mode 100644 httprunner/loader/schemas/testsuite.schema.json delete mode 100644 httprunner/loader/schemas/testsuite.schema.v2.json diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 362cedb7..3e6f92b6 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -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** diff --git a/httprunner/loader/buildup.py b/httprunner/loader/buildup.py index 0e95911a..01361880 100644 --- a/httprunner/loader/buildup.py +++ b/httprunner/loader/buildup.py @@ -281,6 +281,7 @@ def load_testsuite(raw_testsuite): } """ + JsonSchemaChecker.validate_testsuite_format(raw_testsuite) raw_testcases = raw_testsuite.pop("testcases") raw_testsuite["testcases"] = {} diff --git a/httprunner/loader/check.py b/httprunner/loader/check.py index c73156b6..7f389e25 100644 --- a/httprunner/loader/check.py +++ b/httprunner/loader/check.py @@ -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. diff --git a/httprunner/loader/schemas/testcase.schema.v2.json b/httprunner/loader/schemas/testcase.schema.v2.json index 09729489..4a1f2395 100644 --- a/httprunner/loader/schemas/testcase.schema.v2.json +++ b/httprunner/loader/schemas/testcase.schema.v2.json @@ -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": { diff --git a/httprunner/loader/schemas/testsuite.schema.json b/httprunner/loader/schemas/testsuite.schema.json new file mode 100644 index 00000000..6d021123 --- /dev/null +++ b/httprunner/loader/schemas/testsuite.schema.json @@ -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" + ] +} \ No newline at end of file diff --git a/httprunner/loader/schemas/testsuite.schema.v2.json b/httprunner/loader/schemas/testsuite.schema.v2.json deleted file mode 100644 index cfdfbb14..00000000 --- a/httprunner/loader/schemas/testsuite.schema.v2.json +++ /dev/null @@ -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" - ] - } - } -} \ No newline at end of file