From f1cac82a15473cb80412dddd39ca395c58b89ab5 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Wed, 28 Feb 2018 23:56:10 +0800 Subject: [PATCH] #106: support builtin functions --- httprunner/__about__.py | 2 +- httprunner/testcase.py | 5 +++++ tests/data/demo_binds.yml | 6 ++++++ tests/test_context.py | 19 +++++++++++++++++++ 4 files changed, 31 insertions(+), 1 deletion(-) diff --git a/httprunner/__about__.py b/httprunner/__about__.py index f401e934..1ae1c422 100644 --- a/httprunner/__about__.py +++ b/httprunner/__about__.py @@ -1,7 +1,7 @@ __title__ = 'HttpRunner' __description__ = 'HTTP test runner, not just about api test and load test.' __url__ = 'https://github.com/HttpRunner/HttpRunner' -__version__ = '0.9.7' +__version__ = '0.9.8' __author__ = 'debugtalk' __author_email__ = 'mail@debugtalk.com' __license__ = 'MIT' diff --git a/httprunner/testcase.py b/httprunner/testcase.py index b218ebca..cdb42d3b 100644 --- a/httprunner/testcase.py +++ b/httprunner/testcase.py @@ -703,6 +703,11 @@ class TestcaseParser(object): if item_type == "function": if item_name in self.functions: return self.functions[item_name] + + try: + return eval(item_name) + except NameError: + pass elif item_type == "variable": if item_name in self.variables: return self.variables[item_name] diff --git a/tests/data/demo_binds.yml b/tests/data/demo_binds.yml index 1ceb4db9..2bfda8a1 100644 --- a/tests/data/demo_binds.yml +++ b/tests/data/demo_binds.yml @@ -34,3 +34,9 @@ bind_module_functions: - random: ${gen_random_string(5)} - data: "{'name': 'user', 'password': '123456'}" - authorization: ${gen_md5($TOKEN, $data, $random)} + +builtin_functions: + variables: + - length: ${len(debugtalk)} + - smallest: ${min(2, 3, 8)} + - largest: ${max(2, 3, 8)} diff --git a/tests/test_context.py b/tests/test_context.py index 3d14ea2c..e099b288 100644 --- a/tests/test_context.py +++ b/tests/test_context.py @@ -100,6 +100,25 @@ class VariableBindsUnittest(ApiServerUnittest): self.assertIn("sum2nums", context_variables) self.assertEqual(context_variables["sum2nums"], 5) + def test_call_builtin_functions(self): + testcase1 = { + "variables": [ + {"length": "${len(debugtalk)}"}, + {"smallest": "${min(2, 3, 8)}"}, + {"largest": "${max(2, 3, 8)}"} + ] + } + testcase2 = self.testcases["builtin_functions"] + + for testcase in [testcase1, testcase2]: + variables = testcase['variables'] + self.context.bind_variables(variables) + + context_variables = self.context.testcase_variables_mapping + self.assertEqual(context_variables["length"], 9) + self.assertEqual(context_variables["smallest"], 2) + self.assertEqual(context_variables["largest"], 8) + def test_context_bind_lambda_functions_with_import(self): testcase1 = { "requires": ["random", "string", "hashlib"],