add setup and teardown block, in these blocks, we can exec functions such as sleep

This commit is contained in:
debugtalk
2017-09-04 22:58:40 +08:00
parent 154e050a60
commit cd6e742808
5 changed files with 36 additions and 4 deletions

View File

@@ -1 +1 @@
__version__ = '0.6.1'
__version__ = '0.6.2'

View File

@@ -28,3 +28,8 @@ def get_current_date(fmt="%Y-%m-%d"):
""" get current date, default format is %Y-%m-%d
"""
return datetime.datetime.now().strftime(fmt)
def sleep(sec):
""" sleep specified seconds
"""
time.sleep(sec)

View File

@@ -138,7 +138,7 @@ class Context(object):
)
def get_parsed_request(self):
""" get parsed request, with each variable replaced by bind value.
""" get parsed request, with bind variables and functions.
"""
parsed_request = self.testcase_parser.parse_content_with_bindings(
self.testcase_request_config
@@ -148,3 +148,8 @@ class Context(object):
def get_testcase_variables_mapping(self):
return self.testcase_variables_mapping
def exec_content_functions(self, content):
""" execute functions in content.
"""
self.testcase_parser.eval_content_functions(content)

View File

@@ -79,7 +79,9 @@ class Runner(object):
"body": '{"name": "user", "password": "123456"}'
},
"extract_binds": [], # optional
"validators": [] # optional
"validators": [], # optional
"setup": [], # optional
"teardown": [] # optional
}
@return True or raise exception during test
"""
@@ -95,8 +97,16 @@ class Runner(object):
run_times = int(testcase.get("times", 1))
extract_binds = testcase.get("extract_binds", [])
validators = testcase.get("validators", [])
setup_actions = testcase.get("setup", [])
teardown_actions = testcase.get("teardown", [])
def setup_teardown(actions):
for action in actions:
self.context.exec_content_functions(action)
for _ in range(run_times):
setup_teardown(setup_actions)
resp = self.http_client_session.request(url=url, method=method, **parsed_request)
resp_obj = response.ResponseObject(resp)
@@ -105,6 +115,8 @@ class Runner(object):
resp_obj.validate(validators, self.context.get_testcase_variables_mapping())
setup_teardown(teardown_actions)
return True
def run_testset(self, testset):

View File

@@ -1,7 +1,8 @@
import os
import time
import unittest
from ate import utils, runner
from ate import runner, utils
from ate.context import Context
from ate.exception import ParamsError
@@ -229,3 +230,12 @@ class VariableBindsUnittest(unittest.TestCase):
self.assertIn("data", parsed_request)
self.assertEqual(parsed_request["data"], testcase["variable_binds"][2]["data"])
self.assertEqual(parsed_request["headers"]["secret_key"], "DebugTalk")
def test_exec_content_functions(self):
test_runner = runner.Runner()
content = "${sleep(1)}"
start_time = time.time()
test_runner.context.exec_content_functions(content)
end_time = time.time()
elapsed_time = end_time - start_time
self.assertGreater(elapsed_time, 1)