mirror of
https://github.com/httprunner/httprunner.git
synced 2026-07-11 15:31:37 +08:00
make variables marker unified to be
This commit is contained in:
@@ -1,4 +1,6 @@
|
|||||||
|
import re
|
||||||
import importlib
|
import importlib
|
||||||
|
from ate import exception, utils
|
||||||
|
|
||||||
class Context(object):
|
class Context(object):
|
||||||
""" Manages binding of variables
|
""" Manages binding of variables
|
||||||
@@ -44,13 +46,10 @@ class Context(object):
|
|||||||
|
|
||||||
def get_eval_value(self, data):
|
def get_eval_value(self, data):
|
||||||
""" evaluate data recursively, each variable in data will be evaluated.
|
""" 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 isinstance(data, str):
|
||||||
if data.startswith('$'):
|
return utils.parse_content_with_variables(data, self.variables)
|
||||||
# this is a variable, and will replace with its bind value
|
|
||||||
return self.variables.get(data[1:])
|
|
||||||
return data
|
|
||||||
|
|
||||||
if isinstance(data, list):
|
if isinstance(data, list):
|
||||||
return [self.get_eval_value(item) for item in data]
|
return [self.get_eval_value(item) for item in data]
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
import re
|
from ate import utils
|
||||||
from ate import exception
|
|
||||||
|
|
||||||
class TestcaseParser(object):
|
class TestcaseParser(object):
|
||||||
|
|
||||||
@@ -44,22 +43,7 @@ class TestcaseParser(object):
|
|||||||
variables marker: ${variable}.
|
variables marker: ${variable}.
|
||||||
"""
|
"""
|
||||||
if isinstance(content, str):
|
if isinstance(content, str):
|
||||||
# check if content includes ${variable}
|
return utils.parse_content_with_variables(content, self.variables_binds)
|
||||||
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
|
|
||||||
|
|
||||||
if isinstance(content, list):
|
if isinstance(content, list):
|
||||||
return [self.substitute(item) for item in content]
|
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 json
|
||||||
import os.path
|
import os.path
|
||||||
import random
|
import random
|
||||||
|
import re
|
||||||
import string
|
import string
|
||||||
|
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
from ate.exception import ParamsError
|
from ate.exception import ParamsError
|
||||||
@@ -190,3 +190,23 @@ def load_testcases_by_path(path):
|
|||||||
|
|
||||||
else:
|
else:
|
||||||
return []
|
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:
|
variable_binds:
|
||||||
- TOKEN: "debugtalk"
|
- TOKEN: "debugtalk"
|
||||||
- token: $TOKEN
|
- token: ${TOKEN}
|
||||||
|
|
||||||
-
|
-
|
||||||
function_binds:
|
function_binds:
|
||||||
@@ -35,4 +35,4 @@
|
|||||||
- TOKEN: debugtalk
|
- TOKEN: debugtalk
|
||||||
- random: {"func": "gen_random_string", "args": [5]}
|
- random: {"func": "gen_random_string", "args": [5]}
|
||||||
- data: "{'name': 'user', 'password': '123456'}"
|
- 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
|
- TOKEN: debugtalk
|
||||||
- random: {"func": "gen_random_string", "args": [5]}
|
- random: {"func": "gen_random_string", "args": [5]}
|
||||||
- data: '{"name": "user", "password": "123456"}'
|
- 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
|
- expected_status_code: 201
|
||||||
request:
|
request:
|
||||||
url: http://127.0.0.1:5000/api/users/1000
|
url: http://127.0.0.1:5000/api/users/1000
|
||||||
@@ -43,7 +43,7 @@
|
|||||||
- TOKEN: debugtalk
|
- TOKEN: debugtalk
|
||||||
- random: {"func": "gen_random_string", "args": [5]}
|
- random: {"func": "gen_random_string", "args": [5]}
|
||||||
- data: '{"name": "user", "password": "123456"}'
|
- 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
|
- expected_status_code: 500
|
||||||
request:
|
request:
|
||||||
url: http://127.0.0.1:5000/api/users/1000
|
url: http://127.0.0.1:5000/api/users/1000
|
||||||
|
|||||||
@@ -11,7 +11,7 @@
|
|||||||
- TOKEN: debugtalk
|
- TOKEN: debugtalk
|
||||||
- random: {"func": "gen_random_string", "args": [5]}
|
- random: {"func": "gen_random_string", "args": [5]}
|
||||||
- data: '{"name": "user", "password": "123456"}'
|
- data: '{"name": "user", "password": "123456"}'
|
||||||
- authorization: {"func": "gen_md5", "args": ["$TOKEN", "$data", "$random"]}
|
- authorization: {"func": "gen_md5", "args": ["${TOKEN}", "${data}", "${random}"]}
|
||||||
|
|
||||||
- test:
|
- test:
|
||||||
name: create user which does not exist
|
name: create user which does not exist
|
||||||
|
|||||||
@@ -69,7 +69,7 @@ class VariableBindsUnittest(unittest.TestCase):
|
|||||||
testcase1 = {
|
testcase1 = {
|
||||||
"variable_binds": [
|
"variable_binds": [
|
||||||
{"GLOBAL_TOKEN": "debugtalk"},
|
{"GLOBAL_TOKEN": "debugtalk"},
|
||||||
{"token": "$GLOBAL_TOKEN"}
|
{"token": "${GLOBAL_TOKEN}"}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
testcase2 = self.testcases[3]
|
testcase2 = self.testcases[3]
|
||||||
|
|||||||
Reference in New Issue
Block a user