From 46dcd195d0db43dfb751cb095c4ae2c4145b44ba Mon Sep 17 00:00:00 2001 From: debugtalk Date: Tue, 29 Aug 2017 21:33:02 +0800 Subject: [PATCH] #36: add debugtalk.py local per-directory plugins --- ate/__init__.py | 2 +- ate/context.py | 3 +++ ate/debugtalk.py | 26 ++++++++++++++++++++++++++ tests/data/custom_functions.py | 3 --- tests/test_context.py | 14 ++++++++++++++ 5 files changed, 44 insertions(+), 4 deletions(-) create mode 100644 ate/debugtalk.py diff --git a/ate/__init__.py b/ate/__init__.py index b4a839ec..a774690a 100644 --- a/ate/__init__.py +++ b/ate/__init__.py @@ -1 +1 @@ -__version__ = '0.5.3' \ No newline at end of file +__version__ = '0.5.4' \ No newline at end of file diff --git a/ate/context.py b/ate/context.py index bf80a8ce..0cb82b32 100644 --- a/ate/context.py +++ b/ate/context.py @@ -46,6 +46,9 @@ class Context(object): self.testcase_parser.bind_functions(self.testcase_functions_config) self.testcase_parser.bind_variables(self.testcase_variables_mapping) + if level == "testset": + self.import_module_functions(["ate.debugtalk"], "testset") + def import_requires(self, modules): """ import required modules dynamicly """ diff --git a/ate/debugtalk.py b/ate/debugtalk.py new file mode 100644 index 00000000..7b14f207 --- /dev/null +++ b/ate/debugtalk.py @@ -0,0 +1,26 @@ +import datetime +import random +import string +import time + +from ate.exception import ParamsError + + +def gen_random_string(str_len): + """ generate random string with specified length + """ + return ''.join( + random.choice(string.ascii_letters + string.digits) for _ in range(str_len)) + +def get_timestamp(str_len=13): + """ get timestamp string, length can only between 0 and 16 + """ + if isinstance(str_len, int) and 0 < str_len < 17: + return str(time.time()).replace(".", "")[:str_len] + + raise ParamsError("timestamp length can only between 0 and 16.") + +def get_current_date(fmt="%Y-%m-%d"): + """ get current date, default format is %Y-%m-%d + """ + return datetime.datetime.now().strftime(fmt) diff --git a/tests/data/custom_functions.py b/tests/data/custom_functions.py index 1cd6de6f..da4f9c95 100644 --- a/tests/data/custom_functions.py +++ b/tests/data/custom_functions.py @@ -65,6 +65,3 @@ def gen_urlencode_str(**kargs): urlencoded_str += "&" return urlencoded_str.strip("&") - -def get_timestamp(): - return int(time.time() * 1000) diff --git a/tests/test_context.py b/tests/test_context.py index 1a3f24dc..873d6cfe 100644 --- a/tests/test_context.py +++ b/tests/test_context.py @@ -13,6 +13,20 @@ class VariableBindsUnittest(unittest.TestCase): testcase_file_path = os.path.join(os.getcwd(), 'tests/data/demo_binds.yml') self.testcases = utils.load_testcases(testcase_file_path) + def test_context_init_functions(self): + self.assertIn("get_timestamp", self.context.testset_functions_config) + self.assertIn("gen_random_string", self.context.testset_functions_config) + + variable_binds = [ + {"random": "${gen_random_string(5)}"}, + {"timestamp10": "${get_timestamp(10)}"} + ] + self.context.bind_variables(variable_binds) + context_variables = self.context.get_testcase_variables_mapping() + + self.assertEqual(len(context_variables["random"]), 5) + self.assertEqual(len(context_variables["timestamp10"]), 10) + def test_context_bind_testset_variables(self): # testcase in JSON format testcase1 = {