From 887845493d08d0c3da81f5c4c9b1890e193e05ab Mon Sep 17 00:00:00 2001 From: debugtalk Date: Tue, 27 Jun 2017 23:56:59 +0800 Subject: [PATCH] make variables marker unified to be --- ate/context.py | 9 ++++----- ate/testcase.py | 20 ++------------------ ate/utils.py | 22 +++++++++++++++++++++- test/data/demo_binds.yml | 4 ++-- test/data/demo_template_separate.yml | 4 ++-- test/data/demo_template_sets.yml | 2 +- test/test_context.py | 2 +- 7 files changed, 33 insertions(+), 30 deletions(-) diff --git a/ate/context.py b/ate/context.py index 44e09e71..3499b6f7 100644 --- a/ate/context.py +++ b/ate/context.py @@ -1,4 +1,6 @@ +import re import importlib +from ate import exception, utils class Context(object): """ Manages binding of variables @@ -44,13 +46,10 @@ class Context(object): def get_eval_value(self, data): """ evaluate data recursively, each variable in data will be evaluated. - variable will always be a string started with $, such as $token + variables marker: ${variable}. """ if isinstance(data, str): - if data.startswith('$'): - # this is a variable, and will replace with its bind value - return self.variables.get(data[1:]) - return data + return utils.parse_content_with_variables(data, self.variables) if isinstance(data, list): return [self.get_eval_value(item) for item in data] diff --git a/ate/testcase.py b/ate/testcase.py index c245a07b..0fa22929 100644 --- a/ate/testcase.py +++ b/ate/testcase.py @@ -1,5 +1,4 @@ -import re -from ate import exception +from ate import utils class TestcaseParser(object): @@ -44,22 +43,7 @@ class TestcaseParser(object): variables marker: ${variable}. """ if isinstance(content, str): - # check if content includes ${variable} - matched = re.match(r"(.*)\$\{(.*)\}(.*)", content) - if matched: - # this is a variable, and will replace with its bind value - variable_name = matched.group(2) - value = self.variables_binds.get(variable_name) - if value is None: - raise exception.ParamsError( - "%s is not defined in bind variables!" % variable_name) - if matched.group(1) or matched.group(3): - # e.g. /api/users/${uid} - return re.sub(r"\$\{.*\}", value, content) - - return value - - return content + return utils.parse_content_with_variables(content, self.variables_binds) if isinstance(content, list): return [self.substitute(item) for item in content] diff --git a/ate/utils.py b/ate/utils.py index 6c10da68..aa2d8a90 100644 --- a/ate/utils.py +++ b/ate/utils.py @@ -2,8 +2,8 @@ import hashlib import json import os.path import random +import re import string - import yaml from ate.exception import ParamsError @@ -190,3 +190,23 @@ def load_testcases_by_path(path): else: return [] + +def parse_content_with_variables(content, variables_binds): + """ replace variables with bind value + """ + # check if content includes ${variable} + matched = re.match(r"(.*)\$\{(.*)\}(.*)", content) + if matched: + # this is a variable, and will replace with its bind value + variable_name = matched.group(2) + value = variables_binds.get(variable_name) + if value is None: + raise ParamsError( + "%s is not defined in bind variables!" % variable_name) + if matched.group(1) or matched.group(3): + # e.g. /api/users/${uid} + return re.sub(r"\$\{.*\}", value, content) + + return value + + return content diff --git a/test/data/demo_binds.yml b/test/data/demo_binds.yml index 0de0a135..0ac48942 100644 --- a/test/data/demo_binds.yml +++ b/test/data/demo_binds.yml @@ -13,7 +13,7 @@ - variable_binds: - TOKEN: "debugtalk" - - token: $TOKEN + - token: ${TOKEN} - function_binds: @@ -35,4 +35,4 @@ - TOKEN: debugtalk - random: {"func": "gen_random_string", "args": [5]} - data: "{'name': 'user', 'password': '123456'}" - - authorization: {"func": "gen_md5", "args": [$TOKEN, $data, $random]} + - authorization: {"func": "gen_md5", "args": ["${TOKEN}", "${data}", "${random}"]} diff --git a/test/data/demo_template_separate.yml b/test/data/demo_template_separate.yml index 1aa5b01e..bd025317 100644 --- a/test/data/demo_template_separate.yml +++ b/test/data/demo_template_separate.yml @@ -12,7 +12,7 @@ - TOKEN: debugtalk - random: {"func": "gen_random_string", "args": [5]} - data: '{"name": "user", "password": "123456"}' - - authorization: {"func": "gen_md5", "args": ["$TOKEN", "$data", "$random"]} + - authorization: {"func": "gen_md5", "args": ["${TOKEN}", "${data}", "${random}"]} - expected_status_code: 201 request: url: http://127.0.0.1:5000/api/users/1000 @@ -43,7 +43,7 @@ - TOKEN: debugtalk - random: {"func": "gen_random_string", "args": [5]} - data: '{"name": "user", "password": "123456"}' - - authorization: {"func": "gen_md5", "args": ["$TOKEN", "$data", "$random"]} + - authorization: {"func": "gen_md5", "args": ["${TOKEN}", "${data}", "${random}"]} - expected_status_code: 500 request: url: http://127.0.0.1:5000/api/users/1000 diff --git a/test/data/demo_template_sets.yml b/test/data/demo_template_sets.yml index 494cef6b..2b33dd38 100644 --- a/test/data/demo_template_sets.yml +++ b/test/data/demo_template_sets.yml @@ -11,7 +11,7 @@ - TOKEN: debugtalk - random: {"func": "gen_random_string", "args": [5]} - data: '{"name": "user", "password": "123456"}' - - authorization: {"func": "gen_md5", "args": ["$TOKEN", "$data", "$random"]} + - authorization: {"func": "gen_md5", "args": ["${TOKEN}", "${data}", "${random}"]} - test: name: create user which does not exist diff --git a/test/test_context.py b/test/test_context.py index d7092a64..fe6cbf40 100644 --- a/test/test_context.py +++ b/test/test_context.py @@ -69,7 +69,7 @@ class VariableBindsUnittest(unittest.TestCase): testcase1 = { "variable_binds": [ {"GLOBAL_TOKEN": "debugtalk"}, - {"token": "$GLOBAL_TOKEN"} + {"token": "${GLOBAL_TOKEN}"} ] } testcase2 = self.testcases[3]