#78: merge extractors in test with api extractors

This commit is contained in:
debugtalk
2018-01-16 20:36:27 +08:00
parent c28f975c86
commit f1a5eb56c0
3 changed files with 73 additions and 3 deletions
+1 -3
View File
@@ -102,9 +102,7 @@ class Runner(object):
raise exception.ParamsError("URL or METHOD missed!") raise exception.ParamsError("URL or METHOD missed!")
run_times = int(testcase_dict.get("times", 1)) run_times = int(testcase_dict.get("times", 1))
extractors = testcase_dict.get("extract") \ extractors = testcase_dict.get("extract", [])
or testcase_dict.get("extractors") \
or testcase_dict.get("extract_binds", [])
validators = testcase_dict.get("validate", []) validators = testcase_dict.get("validate", [])
setup_actions = testcase_dict.get("setup", []) setup_actions = testcase_dict.get("setup", [])
teardown_actions = testcase_dict.get("teardown", []) teardown_actions = testcase_dict.get("teardown", [])
+54
View File
@@ -4,6 +4,7 @@ import json
import logging import logging
import os import os
import re import re
from collections import OrderedDict
import yaml import yaml
from httprunner import exception, utils from httprunner import exception, utils
@@ -297,6 +298,48 @@ def merge_validator(api_validators, test_validators):
api_validators_mapping.update(test_validators_mapping) api_validators_mapping.update(test_validators_mapping)
return list(api_validators_mapping.values()) return list(api_validators_mapping.values())
def merge_extractor(api_extrators, test_extracors):
""" merge api_extrators with test_extracors
@params:
api_extrators: [{"var1": "val1"}, {"var2": "val2"}]
test_extracors: [{"var1": "val111"}, {"var3": "val3"}]
@return:
[
{"var1": "val111"},
{"var2": "val2"},
{"var3": "val3"}
]
"""
if not api_extrators:
return test_extracors
elif not test_extracors:
return api_extrators
else:
extractor_dict = OrderedDict()
for api_extrator in api_extrators:
if len(api_extrator) != 1:
logging.warning("incorrect extractor: {}".format(api_extrator))
continue
var_name = list(api_extrator.keys())[0]
extractor_dict[var_name] = api_extrator[var_name]
for test_extrator in test_extracors:
if len(test_extrator) != 1:
logging.warning("incorrect extractor: {}".format(test_extrator))
continue
var_name = list(test_extrator.keys())[0]
extractor_dict[var_name] = test_extrator[var_name]
extractor_list = []
for key, value in extractor_dict.items():
extractor_list.append({key: value})
return extractor_list
def extend_test_api(test_block_dict): def extend_test_api(test_block_dict):
""" update test block api with api definition """ update test block api with api definition
@param @param
@@ -321,11 +364,22 @@ def extend_test_api(test_block_dict):
api_validators = test_info.get("validate") or test_info.get("validators", []) api_validators = test_info.get("validate") or test_info.get("validators", [])
test_validators = test_block_dict.get("validate") or test_block_dict.get("validators", []) test_validators = test_block_dict.get("validate") or test_block_dict.get("validators", [])
api_extrators = test_info.get("extract") \
or test_info.get("extractors") \
or test_info.get("extract_binds", [])
test_extracors = test_block_dict.get("extract") \
or test_block_dict.get("extractors") \
or test_block_dict.get("extract_binds", [])
test_block_dict.update(test_info) test_block_dict.update(test_info)
test_block_dict["validate"] = merge_validator( test_block_dict["validate"] = merge_validator(
api_validators, api_validators,
test_validators test_validators
) )
test_block_dict["extract"] = merge_extractor(
api_extrators,
test_extracors
)
def load_test_file(file_path): def load_test_file(file_path):
""" load testset file, get testset data structure. """ load testset file, get testset data structure.
+18
View File
@@ -579,3 +579,21 @@ class TestcaseParserUnittest(unittest.TestCase):
{"check": "s3", "expect": 12, "comparator": "len_eq"}, {"check": "s3", "expect": 12, "comparator": "len_eq"},
merged_validators merged_validators
) )
def test_merge_extractor(self):
api_extrators = [{"var1": "val1"}, {"var2": "val2"}]
test_extracors = [{"var1": "val111"}, {"var3": "val3"}]
merged_extractors = testcase.merge_extractor(api_extrators, test_extracors)
self.assertIn(
{"var1": "val111"},
merged_extractors
)
self.assertIn(
{"var2": "val2"},
merged_extractors
)
self.assertIn(
{"var3": "val3"},
merged_extractors
)