From 9dbf194e5898e643842722f150830aa6b8aae2f1 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Wed, 1 Jan 2020 09:35:50 +0800 Subject: [PATCH] fix: json schema validation for testcase v2 --- httprunner/loader/buildup.py | 10 +- httprunner/loader/check.py | 6 ++ httprunner/loader/schemas/common.schema.json | 31 ++++-- .../loader/schemas/testcase.schema.v2.json | 101 ++++++++++++------ tests/test_loader/test_cases.py | 5 +- 5 files changed, 111 insertions(+), 42 deletions(-) diff --git a/httprunner/loader/buildup.py b/httprunner/loader/buildup.py index 65d0042c..490d3208 100644 --- a/httprunner/loader/buildup.py +++ b/httprunner/loader/buildup.py @@ -281,19 +281,21 @@ def load_testsuite(raw_testsuite): } """ - raw_testcases = raw_testsuite.pop("testcases") - raw_testsuite["testcases"] = {} + raw_testcases = raw_testsuite["testcases"] if isinstance(raw_testcases, dict): - # make compatible with version < 2.2.0 + # format version 1, make compatible with version < 2.2.0 + JsonSchemaChecker.validate_testsuite_v1_format(raw_testsuite) + raw_testsuite["testcases"] = {} for name, raw_testcase in raw_testcases.items(): __extend_with_testcase_ref(raw_testcase) raw_testcase.setdefault("name", name) raw_testsuite["testcases"][name] = raw_testcase elif isinstance(raw_testcases, list): - JsonSchemaChecker.validate_testsuite_v2_format(raw_testsuite) # format version 2, implemented in 2.2.0 + JsonSchemaChecker.validate_testsuite_v2_format(raw_testsuite) + raw_testsuite["testcases"] = {} for raw_testcase in raw_testcases: __extend_with_testcase_ref(raw_testcase) testcase_name = raw_testcase["name"] diff --git a/httprunner/loader/check.py b/httprunner/loader/check.py index 0f01a4fc..44a0f721 100644 --- a/httprunner/loader/check.py +++ b/httprunner/loader/check.py @@ -57,6 +57,12 @@ class JsonSchemaChecker(object): return True + @staticmethod + def validate_testsuite_v1_format(content): + """ check testsuite format v1 if valid + """ + return True + @staticmethod def validate_testsuite_v2_format(content): """ check testsuite format v2 if valid diff --git a/httprunner/loader/schemas/common.schema.json b/httprunner/loader/schemas/common.schema.json index 10b610a8..de03d1cf 100644 --- a/httprunner/loader/schemas/common.schema.json +++ b/httprunner/loader/schemas/common.schema.json @@ -175,13 +175,32 @@ }, "extract": { "description": "used to extract session variables for later requests", - "type": "object", - "patternProperties": { - "^[A-Za-z_][A-Za-z0-9_]*$": { - "description": "extraction rule for session variable, maybe in jsonpath/regex/jmespath", - "type": "string" + "oneOf": [ + { + "type": "object", + "patternProperties": { + "^[A-Za-z_][A-Za-z0-9_]*$": { + "description": "extraction rule for session variable, maybe in jsonpath/regex/jmespath", + "type": "string" + } + }, + "minProperties": 1 + }, + { + "type": "array", + "items": { + "type": "object", + "patternProperties": { + "^[A-Za-z_][A-Za-z0-9_]*$": { + "description": "extraction rule for session variable, maybe in jsonpath/regex/jmespath", + "type": "string" + } + }, + "minProperties": 1, + "maxProperties": 1 + } } - } + ] }, "validate": { "description": "used to validate response fields", diff --git a/httprunner/loader/schemas/testcase.schema.v2.json b/httprunner/loader/schemas/testcase.schema.v2.json index 4a1f2395..75009f3d 100644 --- a/httprunner/loader/schemas/testcase.schema.v2.json +++ b/httprunner/loader/schemas/testcase.schema.v2.json @@ -6,42 +6,13 @@ "teststep": { "type": "object", "oneOf": [ - { - "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": { - "$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" }, "request": { + "description": "define api request directly", "$ref": "common.schema.json#/definitions/request" }, "variables": { @@ -64,6 +35,76 @@ "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" + ] } ] } diff --git a/tests/test_loader/test_cases.py b/tests/test_loader/test_cases.py index f501a3d0..d2266236 100644 --- a/tests/test_loader/test_cases.py +++ b/tests/test_loader/test_cases.py @@ -279,6 +279,7 @@ class TestSuiteLoader(unittest.TestCase): def test_load_project_tests(self): buildup.load_project_data(os.path.join(os.getcwd(), "tests")) - api_file_path = os.path.join(os.getcwd(), "tests", "api", "get_token.yml") - self.assertIn(api_file_path, self.tests_def_mapping["api"]) + self.assertIn("gen_md5", self.project_mapping["functions"]) self.assertEqual(self.project_mapping["env"]["PROJECT_KEY"], "ABCDEFGH") + self.assertEqual(self.project_mapping["PWD"], os.path.dirname(os.path.dirname(__file__))) + self.assertEqual(self.project_mapping["test_path"], os.path.dirname(os.path.dirname(__file__)))