mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-12 10:39:40 +08:00
make variables marker unified to be
This commit is contained in:
@@ -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]
|
||||
|
||||
@@ -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]
|
||||
|
||||
22
ate/utils.py
22
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
|
||||
|
||||
@@ -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}"]}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -69,7 +69,7 @@ class VariableBindsUnittest(unittest.TestCase):
|
||||
testcase1 = {
|
||||
"variable_binds": [
|
||||
{"GLOBAL_TOKEN": "debugtalk"},
|
||||
{"token": "$GLOBAL_TOKEN"}
|
||||
{"token": "${GLOBAL_TOKEN}"}
|
||||
]
|
||||
}
|
||||
testcase2 = self.testcases[3]
|
||||
|
||||
Reference in New Issue
Block a user