From 8021fb076080d73298fa94584d2245f4f34503df Mon Sep 17 00:00:00 2001 From: debugtalk Date: Thu, 29 Nov 2018 14:43:09 +0800 Subject: [PATCH] make extract in testcase template compatible with mapping style: new mapping style: extract: { a: content.a, b: text.b } Also, the former list style is still valid: extract: [ {a: content.a}, {b: text.b} ] --- httprunner/loader.py | 4 ++-- httprunner/parser.py | 8 ++++---- httprunner/runner.py | 4 ++-- tests/data/demo_testcase_hardcode.json | 8 +++----- tests/data/demo_testcase_hardcode.yml | 4 ++-- 5 files changed, 13 insertions(+), 15 deletions(-) diff --git a/httprunner/loader.py b/httprunner/loader.py index 5f56c261..e18a72bb 100644 --- a/httprunner/loader.py +++ b/httprunner/loader.py @@ -296,7 +296,7 @@ def load_test(raw_testinfo): "api": "api_add_cart", "variables": [], "validate": [], - "extract": [] + "extract": {} } # testcase reference { @@ -310,7 +310,7 @@ def load_test(raw_testinfo): "request": {}, "variables": [], "validate": [], - "extract": [] + "extract": {} } Returns: diff --git a/httprunner/parser.py b/httprunner/parser.py index cd67f0f5..4d8d7f27 100644 --- a/httprunner/parser.py +++ b/httprunner/parser.py @@ -566,14 +566,14 @@ def _extend_with_api(test_dict, api_def_dict): } >>> test_dict = { "name": "get token 2", - "extract": [{"token": "content.token"}], + "extract": {"token": "content.token"}, "validate": [{'eq': ['status_code', 201]}, {'len_eq': ['content.token', 16]}] } >>> _extend_with_api(test_dict, api_def_dict) { "name": "get token 2", "request": {...}, - "extract": [{"token": "content.token"}], + "extract": {"token": "content.token"}, "validate": [{'eq': ['status_code', 201]}, {'len_eq': ['content.token', 16]}] } @@ -606,10 +606,10 @@ def _extend_with_api(test_dict, api_def_dict): ) # merge & override extractors - def_extrators = api_def_dict.pop("extract", []) + def_extrators = api_def_dict.pop("extract", {}) test_dict["extract"] = utils.extend_variables( def_extrators, - test_dict.get("extract", []) + test_dict.get("extract", {}) ) # TODO: merge & override request diff --git a/httprunner/runner.py b/httprunner/runner.py index c5d6de55..222aebf0 100644 --- a/httprunner/runner.py +++ b/httprunner/runner.py @@ -169,7 +169,7 @@ class Runner(object): }, "json": {"name": "user", "password": "123456"} }, - "extract": [], # optional + "extract": {}, # optional "validate": [], # optional "setup_hooks": [], # optional "teardown_hooks": [] # optional @@ -238,7 +238,7 @@ class Runner(object): self.do_hook_actions(teardown_hooks, "teardown") # extract - extractors = test_dict.get("extract", []) + extractors = test_dict.get("extract", {}) extracted_variables_mapping = resp_obj.extract_response(extractors) self.session_context.update_seesion_variables(extracted_variables_mapping) diff --git a/tests/data/demo_testcase_hardcode.json b/tests/data/demo_testcase_hardcode.json index 6d1731ad..690058f9 100644 --- a/tests/data/demo_testcase_hardcode.json +++ b/tests/data/demo_testcase_hardcode.json @@ -20,11 +20,9 @@ {"expect_status_code": 200}, {"token_len": 16} ], - "extract": [ - { - "token": "content.token" - } - ], + "extract": { + "token": "content.token" + }, "validate": [ {"check": "status_code", "comparator": "eq", "expect": 200}, {"eq": ["status_code", "$expect_status_code"]}, diff --git a/tests/data/demo_testcase_hardcode.yml b/tests/data/demo_testcase_hardcode.yml index c1285c60..004d2ac2 100644 --- a/tests/data/demo_testcase_hardcode.yml +++ b/tests/data/demo_testcase_hardcode.yml @@ -15,7 +15,7 @@ expect_status_code: 200 token_len: 16 extract: - - token: content.token + token: content.token validate: - {"check": "status_code", "comparator": "eq", "expect": 200} - eq: ["status_code", $expect_status_code] @@ -38,7 +38,7 @@ name: "user1" password: "123456" extract: - - success: content.success + success: content.success validate: - eq: ["status_code", 201] - sum_status_code: ["status_code", 3]