Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor execution as precursor to xUnit and other goodies #171

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
WIP for ongoing work
svanoort committed Dec 20, 2016
commit 9b04e6adda0b2065170e8421dba3509bcf488e6d
13 changes: 13 additions & 0 deletions pyresttest/macros.py
Original file line number Diff line number Diff line change
@@ -92,6 +92,19 @@ class Macro(object):
name = u'Unnamed'
macro_name = None

def pre_request(self, *args, **kwargs):
""" Work done before request can run

"""
pass

def execute_request(self, *args, **kwargs):
pass

def post_request(self, *args, **kwargs):
""" Work done after request is run """
pass

def execute_macro(self, testset_config=TestSetConfig(), context=None, cmdline_args=None, callbacks=MacroCallbacks(), curl_handle=None, *args, **kwargs):
""" Skeletal execution basis """

33 changes: 19 additions & 14 deletions pyresttest/tests.py
Original file line number Diff line number Diff line change
@@ -288,21 +288,24 @@ def __str__(self):
def execute_macro(self, testset_config=TestSetConfig(), context=None, cmdline_args=None, callbacks=MacroCallbacks(), curl_handle=None, *args, **kwargs):
""" Put together test pieces: configure & run actual test, return results """

# Initialize a context if not supplied
mytest=self

# Initialize a context if not supplied, and do context updates
my_context = context
if my_context is None:
my_context = Context()

mytest=self

mytest.update_context_before(my_context)


# Pre-run initialization of object, generate executable test objects
templated_test = mytest.realize(my_context)
curl = templated_test.configure_curl(
timeout=testset_config.timeout, context=my_context, curl_handle=curl_handle)
result = TestResponse()
result.test = templated_test
result.passed = None

# reset the body, it holds values from previous runs otherwise
# Request setup
curl = templated_test.configure_curl(
timeout=testset_config.timeout, context=my_context, curl_handle=curl_handle)
headers = MyIO()
body = MyIO()
curl.setopt(pycurl.WRITEFUNCTION, body.write)
@@ -311,10 +314,9 @@ def execute_macro(self, testset_config=TestSetConfig(), context=None, cmdline_ar
curl.setopt(pycurl.VERBOSE, True)
if testset_config.ssl_insecure:
curl.setopt(pycurl.SSL_VERIFYPEER, 0)
curl.setopt(pycurl.SSL_VERIFYHOST, 0)

result.passed = None
curl.setopt(pycurl.SSL_VERIFYHOST, 0)

# Pre-request work, wait for input or add a delay before the request runs
if testset_config.interactive:
callbacks.log_status("===================================")
callbacks.log_status("%s" % mytest.name)
@@ -331,6 +333,7 @@ def execute_macro(self, testset_config=TestSetConfig(), context=None, cmdline_ar
callbacks.log_status("Delaying for %ds" % mytest.delay)
time.sleep(mytest.delay)

# Execute the test, and handle errors
try:
curl.perform() # Run the actual call
except Exception as e:
@@ -343,14 +346,16 @@ def execute_macro(self, testset_config=TestSetConfig(), context=None, cmdline_ar
curl.close()
return result

# Retrieve values
# Post-request work: perform cleanup and gather info from the request as needed
response_code = curl.getinfo(pycurl.RESPONSE_CODE)
result.response_code = response_code
result.body = body.getvalue()
body.close()
result.response_headers = text_type(headers.getvalue(), HEADER_ENCODING) # Per RFC 2616
headers.close()

response_code = curl.getinfo(pycurl.RESPONSE_CODE)
result.response_code = response_code
# We are now done with the request, now we can do all the analysis and reporting
# This uses the result object and result bodies + test config

callbacks.log_intermediate("Initial Test Result, based on expected response code: " +
str(response_code in mytest.expected_status))