.env variables should be referenced with builtin ENV/environ function

This commit is contained in:
debugtalk
2018-11-16 12:29:02 +08:00
parent 5013861f74
commit e8d7f8e7c5
6 changed files with 36 additions and 2 deletions

View File

@@ -47,6 +47,9 @@ class FunctionNotFound(NotFoundError):
class VariableNotFound(NotFoundError):
pass
class EnvNotFound(NotFoundError):
pass
class ApiNotFound(NotFoundError):
pass

View File

@@ -415,7 +415,9 @@ def parse_string_functions(content, variables_mapping, functions_mapping):
if func_name in ["parameterize", "P"]:
from httprunner import loader
eval_value = loader.load_csv_file(*args, **kwargs)
eval_value = loader.load_csv_file(*args)
elif func_name in ["environ", "ENV"]:
eval_value = utils.get_os_environ(*args)
else:
func = get_mapping_function(func_name, functions_mapping)
eval_value = func(*args, **kwargs)

View File

@@ -29,6 +29,25 @@ def unset_os_environ(variables_mapping):
logger.log_debug("Unset OS environment variable: {}".format(variable))
def get_os_environ(variable_name):
""" get value of environment variable.
Args:
variable_name(str): variable name
Returns:
value of environment variable.
Raises:
exceptions.EnvNotFound: If environment variable not found.
"""
try:
return os.environ[variable_name]
except KeyError:
raise exceptions.EnvNotFound(variable_name)
def query_json(json_content, query, delimiter='.'):
""" Do an xpath-like query with json_content.
@param (dict/list/string) json_content

View File

@@ -3,6 +3,7 @@
variables:
- var_a: 0
- var_c: "${sum_two(1, 2)}"
- PROJECT_KEY: ${ENV(PROJECT_KEY)}
parameters:
- "var_a-var_b":
- [11, 21]

View File

@@ -27,7 +27,6 @@ class TestFileLoader(unittest.TestCase):
os.remove(yaml_tmp_file)
def test_load_json_file_file_format_error(self):
json_tmp_file = "tests/data/tmp.json"
# create empty file

View File

@@ -432,8 +432,18 @@ class TestParser(unittest.TestCase):
testcase_file_path = os.path.join(
os.getcwd(), 'tests/data/demo_testcase.yml')
testcases = loader.load_tests(testcase_file_path)
self.assertEqual(
testcases[0]["config"]["variables"][1]["var_c"],
"${sum_two(1, 2)}"
)
self.assertEqual(
testcases[0]["config"]["variables"][2]["PROJECT_KEY"],
"${ENV(PROJECT_KEY)}"
)
parsed_testcases = parser.parse_tests(testcases)
self.assertEqual(parsed_testcases[0]["config"]["variables"]["var_c"], 3)
self.assertEqual(parsed_testcases[0]["config"]["variables"]["PROJECT_KEY"], "ABCDEFGH")
self.assertEqual(len(parsed_testcases), 2 * 2)
self.assertEqual(
parsed_testcases[0]["config"]["request"]["base_url"],