Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,10 @@ Nino Walker - nino@livefyre.com
- Breaking change: `__raw__` to `raw` on the *TestStep*.
- Feature: `status` to *TestStep.asserts*, allowing for non-200
replies.

- Added `loop` to the *TestStep*. `loop` takes a list of two values [<loop_times>, <loop_interval>]
The loop is run until the `payload` assert is `True` or the loop times is over. On every loop, waits for `loop_interval`
seconds.

#TODO
- Use meta-programming to allow direct integration into unittest
frameworks, and run with tests a la `nose`, to leverage all the things.
Expand Down
62 changes: 41 additions & 21 deletions rester/exc.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import collections
import re
import traceback
from time import sleep


Failure = collections.namedtuple("Failure", "errors output")
Expand Down Expand Up @@ -61,7 +62,8 @@ def _format_logs(self, lc):
def _build_param_dict(self, test_step):
params = {}
if hasattr(test_step, 'params') and test_step.params is not None:
for key, value in test_step.params.items().items():
dct= test_step.params.items()
for key, value in dct.items():
params[key] = self.case.variables.expand(value)
return params

Expand All @@ -77,31 +79,46 @@ def _execute_test_step(self, test_step):
headers = {}
if hasattr(test_step, 'headers') and test_step.headers is not None:
self.logger.debug('Found Headers')
for key, value in test_step.headers.items().items():
dct= test_step.headers.items()
for key, value in dct.items():
headers[key] = self.case.variables.expand(value)

# process and set up params
params = self._build_param_dict(test_step)

url = self.case.variables.expand(test_step.apiUrl)
self.logger.debug('Evaluated URL : %s', url)
response_wrapper = http_client.request(url, method, headers, params, is_raw)

# expected_status = getattr(getattr(test_step, 'asserts'), 'status', 200)
# if response_wrapper.status != expected_status:
# failures.errors.append("status(%s) != expected status(%s)" % (response_wrapper.status, expected_status))

if hasattr(test_step, "asserts"):
asserts = test_step.asserts
if hasattr(asserts, "headers"):
self._assert_element_list('Header', failures, test_step, response_wrapper.headers, test_step.asserts.headers.items().items())

if hasattr(asserts, "payload"):
self.logger.debug('Evaluating Response Payload')
self._assert_element_list('Payload', failures, test_step, response_wrapper.body, test_step.asserts.payload.items().items())
else:
self.logger.warn('\n=======> No "asserts" element found in TestStep %s', test_step.name)


loop_times, loop_interval = (1, 0)
if hasattr(test_step, 'loop') and test_step.loop is not None:
loop_times, loop_interval = test_step.loop

for times in range(loop_times):
response_wrapper = http_client.request(url, method, headers, params, is_raw)

# expected_status = getattr(getattr(test_step, 'asserts'), 'status', 200)
# if response_wrapper.status != expected_status:
# failures.errors.append("status(%s) != expected status(%s)" % (response_wrapper.status, expected_status))

if hasattr(test_step, "asserts"):
asserts = test_step.asserts
if hasattr(asserts, "headers"):
dct= test_step.asserts.headers.items()
if not self._assert_element_list('Header', failures, test_step, response_wrapper.headers, dct.items()):
break

if hasattr(asserts, "payload"):
dct= test_step.asserts.payload.items()
self.logger.debug('Evaluating Response Payload')
if self._assert_element_list('Payload', failures, test_step, response_wrapper.body, dct.items()):
break

else:
self.logger.warn('\n=======> No "asserts" element found in TestStep %s', test_step.name)
break

sleep(loop_interval)

except Exception as inst:
failures.errors.append(traceback.format_exc())
self.logger.error('ERROR !!! TestStep %s Failed to execute !!! %s \
Expand All @@ -112,8 +129,9 @@ def _execute_test_step(self, test_step):
return failures

# execute all the assignment statements
if hasattr(test_step, 'postAsserts') and test_step.postAsserts is not None:
for key, value in test_step.postAsserts.items().items():
if hasattr(test_step, 'postAsserts') and test_step.postAsserts is not None:
dct= test_step.postAsserts.items()
for key, value in dct.items():
self._process_post_asserts(response_wrapper.body, key, value)

return None
Expand Down Expand Up @@ -193,6 +211,8 @@ def _assert_element_list(self, section, failures, test_step, response, assert_li
else:
assert_message = '{} Assert Statement : {} ----> Pass!'.format(section, assert_literal_expr)
self.logger.info('%s', assert_message)

return assert_result

def _process_post_asserts(self, response, key, value):
self.logger.debug("evaled value: {}".format(getattr(response, value, '')))
Expand Down
9 changes: 6 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from setuptools import setup
import sys

setup(name='Rester',
version='1.0.1',
author='Rajeev Chitamoor',
author_email='rajeev@chitamoor.com',
url='https://github.com/chitamoor/rester',
author='Srivatsa Kanchi',
author_email='srivatsa.kanchi@yahoo.com',
url='https://github.com/srivatsakanchi/rester',
license='LICENSE.txt',
packages=['rester'],
entry_points={
Expand All @@ -14,4 +15,6 @@
description='Rest API Testing',
long_description=open('README.md').read(),
install_requires=["requests", "testfixtures", "PyYAML>=3.9"],
use_2to3= sys.version_info.major >= 3,
)