Skip to content

Commit d270ca6

Browse files
author
Abhilash Joseph C
committed
Updated more testcase and coverage
1 parent 87751f7 commit d270ca6

File tree

4 files changed

+92
-85
lines changed

4 files changed

+92
-85
lines changed

.pylintrc

-5
This file was deleted.

py3resttest/constants.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class TestCaseKeywords:
6363
validators = 'validators'
6464
options = 'options'
6565
global_env = 'global_env'
66-
absolute_urls = "absolute-url"
66+
absolute_urls = 'absolute-url'
6767

6868

6969
class EnumHttpMethod(Enum):

py3resttest/testcase.py

+78-78
Original file line numberDiff line numberDiff line change
@@ -139,32 +139,32 @@ def parse(self, base_url: str, testcase_list: List, test_file=None, working_dire
139139

140140
elif key == YamlKeyWords.TEST:
141141
with ChangeDir(working_directory):
142-
__group_name = None
143-
for node_dict in sub_testcase_node:
144-
if __group_name is None:
145-
__group_name = node_dict.get(TestCaseKeywords.group)
146-
147-
__group_name = __group_name if __group_name else TestCaseGroup.DEFAULT_GROUP
148-
try:
149-
group_object = TestSet.test_group_list_dict[__group_name]
150-
except KeyError:
151-
group_object = TestCaseGroup(TestCaseGroup.DEFAULT_GROUP, config=testcase_config_object)
152-
TestSet.test_group_list_dict[__group_name] = group_object
153-
154-
testcase_object = TestCase(
155-
base_url=base_url, extract_binds=group_object.extract_binds,
156-
variable_binds=group_object.variable_binds, context=group_object.context,
157-
config=group_object.config
158-
)
159-
testcase_object.parse(sub_testcase_node)
160-
group_object.testcase_list = testcase_object
142+
self.parse_test(base_url, sub_testcase_node, testcase_config_object)
161143

162144
elif key == YamlKeyWords.CONFIG:
163145
testcase_config_object.parse(sub_testcase_node)
164146

165147
self.config = testcase_config_object
166148

167-
return
149+
@staticmethod
150+
def parse_test(base_url, sub_testcase_node, testcase_config_object):
151+
__group_name = None
152+
for node_dict in sub_testcase_node:
153+
if __group_name is None:
154+
__group_name = node_dict.get(TestCaseKeywords.group)
155+
__group_name = __group_name if __group_name else TestCaseGroup.DEFAULT_GROUP
156+
try:
157+
group_object = TestSet.test_group_list_dict[__group_name]
158+
except KeyError:
159+
group_object = TestCaseGroup(TestCaseGroup.DEFAULT_GROUP, config=testcase_config_object)
160+
TestSet.test_group_list_dict[__group_name] = group_object
161+
testcase_object = TestCase(
162+
base_url=base_url, extract_binds=group_object.extract_binds,
163+
variable_binds=group_object.variable_binds, context=group_object.context,
164+
config=group_object.config
165+
)
166+
testcase_object.parse(sub_testcase_node)
167+
group_object.testcase_list = testcase_object
168168

169169

170170
class TestCaseGroup:
@@ -482,8 +482,7 @@ def set_template(self, variable_name, template_string):
482482
def body(self):
483483
if isinstance(self.__body, str) or self.__body is None:
484484
return self.__body
485-
else:
486-
return self.__body.get_content(context=self.__context)
485+
return self.__body.get_content(context=self.__context)
487486

488487
@body.setter
489488
def body(self, value):
@@ -553,15 +552,12 @@ def parse(self, testcase_dict):
553552
if self.http_method in ["POST", "PUT", "DELETE"]:
554553
self.expected_http_status_code_list = [200, 201, 204]
555554

556-
return
557-
558555
def pre_update(self, context):
559556
if self.variable_binds:
560557
context.bind_variables(self.variable_binds)
561558
if self.generator_binds:
562559
for key, value in self.generator_binds.items():
563560
context.bind_generator_next(key, value)
564-
return
565561

566562
def post_update(self, context):
567563
if self.extract_binds:
@@ -570,21 +566,17 @@ def post_update(self, context):
570566
body=self.body, headers=self.headers, context=context)
571567
if result:
572568
context.bind_variable(key, result)
573-
return
574569

575570
def is_dynamic(self):
576-
if self.templates:
577-
return True
578-
if isinstance(self.__body, ContentHandler) and self.__body.is_dynamic():
571+
if self.templates or (isinstance(self.__body, ContentHandler) and self.__body.is_dynamic()):
579572
return True
573+
return False
580574

581575
def render(self):
582576
if self.is_dynamic() or self.__context is not None:
583577
if isinstance(self.__body, ContentHandler):
584578
self.__body = self.__body.get_content(self.__context)
585579

586-
return
587-
588580
def __perform_validation(self) -> List:
589581

590582
failure_list = []
@@ -621,13 +613,7 @@ def run(self, context=None, timeout=None, curl_handler=None):
621613
else:
622614
curl_handler = pycurl.Curl()
623615

624-
body_byte = BytesIO()
625-
header_byte = BytesIO()
626-
curl_handler.setopt(curl_handler.URL, str(self.url))
627-
curl_handler.setopt(curl_handler.TIMEOUT, timeout)
628-
curl_handler.setopt(pycurl.WRITEFUNCTION, body_byte.write)
629-
curl_handler.setopt(pycurl.HEADERFUNCTION, header_byte.write)
630-
curl_handler.setopt(pycurl.VERBOSE, self.__verbose)
616+
body_byte, header_byte = self.__default_curl_config(curl_handler, timeout)
631617
if self.config.timeout:
632618
curl_handler.setopt(pycurl.CONNECTTIMEOUT, self.config.timeout)
633619

@@ -642,44 +628,10 @@ def run(self, context=None, timeout=None, curl_handler=None):
642628
if self.auth_username and self.auth_password:
643629
curl_handler.setopt(pycurl.USERPWD, self.auth_username + ':' + self.auth_password)
644630

645-
body_length = len(self.body) if self.body else 0
646-
if self.http_method == EnumHttpMethod.POST.name:
647-
curl_handler.setopt(EnumHttpMethod.POST.value, 1)
648-
curl_handler.setopt(pycurl.POSTFIELDSIZE, body_length)
649-
650-
elif self.http_method == EnumHttpMethod.PUT.name:
651-
curl_handler.setopt(EnumHttpMethod.PUT.value, 1)
652-
curl_handler.setopt(pycurl.INFILESIZE, body_length)
653-
654-
elif self.http_method == EnumHttpMethod.PATCH.name:
655-
curl_handler.setopt(EnumHttpMethod.PATCH.value, EnumHttpMethod.PATCH.name)
656-
curl_handler.setopt(pycurl.POSTFIELDS, self.body)
657-
658-
elif self.http_method == EnumHttpMethod.DELETE.name:
659-
curl_handler.setopt(EnumHttpMethod.DELETE.value, EnumHttpMethod.DELETE.name)
660-
if self.body:
661-
curl_handler.setopt(pycurl.POSTFIELDS, self.body)
662-
curl_handler.setopt(pycurl.POSTFIELDSIZE, body_length)
663-
664-
elif self.http_method == EnumHttpMethod.HEAD.name:
665-
curl_handler.setopt(pycurl.NOBODY, 1)
666-
curl_handler.setopt(EnumHttpMethod.HEAD.value, EnumHttpMethod.HEAD.name)
667-
else:
668-
curl_handler.setopt(pycurl.CUSTOMREQUEST, self.http_method.upper())
669-
if self.body:
670-
curl_handler.setopt(pycurl.POSTFIELDS, self.body)
671-
curl_handler.setopt(pycurl.POSTFIELDSIZE, body_length)
631+
self.__configure_curl_method(curl_handler)
672632

673633
head = self.headers
674-
if head.get('content-type'):
675-
content_type = head['content-type']
676-
head[u'content-type'] = content_type + ' ; charset=UTF-8'
677-
678-
headers = [str(header_name) + ':' + str(header_value) for header_name, header_value in head.items()]
679-
headers.append("Expect:")
680-
headers.append("Connection: close")
681-
logger.debug("Request headers %s " % head)
682-
curl_handler.setopt(curl_handler.HTTPHEADER, headers)
634+
self.__configure_curl_headers(curl_handler, head)
683635

684636
if self.__delay:
685637
time.sleep(self.__delay)
@@ -694,14 +646,12 @@ def run(self, context=None, timeout=None, curl_handler=None):
694646
self.__failure_list.append(
695647
Failure(message="Curl Exception: {0}".format(e), details=trace, failure_type=FAILURE_CURL_EXCEPTION))
696648
return
697-
body = body_byte.getvalue()
649+
self.body = body_byte.getvalue()
698650
body_byte.close()
699-
logger.debug("RESPONSE: %s" % self.body)
700651
response_code = curl_handler.getinfo(pycurl.RESPONSE_CODE)
701-
self.body = body
702652
self.__response_code = int(response_code)
703653
if self.config.print_bodies:
704-
print(body)
654+
print(self.body)
705655
try:
706656
response_headers = Parser.parse_headers(header_byte.getvalue())
707657
self.__response_headers = response_headers
@@ -730,3 +680,53 @@ def run(self, context=None, timeout=None, curl_handler=None):
730680
Failure(message=failure_message, details=None, failure_type=FAILURE_INVALID_RESPONSE)
731681
)
732682
curl_handler.close()
683+
684+
@staticmethod
685+
def __configure_curl_headers(curl_handler, head):
686+
if head.get('content-type'):
687+
content_type = head['content-type']
688+
head[u'content-type'] = '%s ; charset=UTF-8' % content_type
689+
headers = ["%s:%s" % (header_name, header_value) for header_name, header_value in head.items()]
690+
headers.append("Expect:")
691+
headers.append("Connection: close")
692+
logger.debug("Request headers %s " % head)
693+
curl_handler.setopt(curl_handler.HTTPHEADER, headers)
694+
695+
def __configure_curl_method(self, curl_handler):
696+
body_length = len(self.body) if self.body else 0
697+
if self.http_method == EnumHttpMethod.POST.name:
698+
curl_handler.setopt(EnumHttpMethod.POST.value, 1)
699+
curl_handler.setopt(pycurl.POSTFIELDSIZE, body_length)
700+
701+
elif self.http_method == EnumHttpMethod.PUT.name:
702+
curl_handler.setopt(EnumHttpMethod.PUT.value, 1)
703+
curl_handler.setopt(pycurl.INFILESIZE, body_length)
704+
705+
elif self.http_method == EnumHttpMethod.PATCH.name:
706+
curl_handler.setopt(EnumHttpMethod.PATCH.value, EnumHttpMethod.PATCH.name)
707+
curl_handler.setopt(pycurl.POSTFIELDS, self.body)
708+
709+
elif self.http_method == EnumHttpMethod.DELETE.name:
710+
curl_handler.setopt(EnumHttpMethod.DELETE.value, EnumHttpMethod.DELETE.name)
711+
if self.body:
712+
curl_handler.setopt(pycurl.POSTFIELDS, self.body)
713+
curl_handler.setopt(pycurl.POSTFIELDSIZE, body_length)
714+
715+
elif self.http_method == EnumHttpMethod.HEAD.name:
716+
curl_handler.setopt(pycurl.NOBODY, 1)
717+
curl_handler.setopt(EnumHttpMethod.HEAD.value, EnumHttpMethod.HEAD.name)
718+
else:
719+
curl_handler.setopt(pycurl.CUSTOMREQUEST, self.http_method.upper())
720+
if self.body:
721+
curl_handler.setopt(pycurl.POSTFIELDS, self.body)
722+
curl_handler.setopt(pycurl.POSTFIELDSIZE, body_length)
723+
724+
def __default_curl_config(self, curl_handler, timeout):
725+
body_byte = BytesIO()
726+
header_byte = BytesIO()
727+
curl_handler.setopt(curl_handler.URL, str(self.url))
728+
curl_handler.setopt(curl_handler.TIMEOUT, timeout)
729+
curl_handler.setopt(pycurl.WRITEFUNCTION, body_byte.write)
730+
curl_handler.setopt(pycurl.HEADERFUNCTION, header_byte.write)
731+
curl_handler.setopt(pycurl.VERBOSE, self.__verbose)
732+
return body_byte, header_byte

setup.cfg

+13-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,16 @@ universal=1
88
count = True
99
ignore = E226,E302,E41
1010
max-line-length = 120
11-
statistics = True
11+
statistics = True
12+
13+
[pylint.FORMAT]
14+
max-line-length = 120
15+
16+
[pylint.DESIGN]
17+
max-args = 8
18+
max-public-methods=30
19+
max-locals=20
20+
max-statements=75
21+
max-branches=20
22+
max-attributes=40
23+

0 commit comments

Comments
 (0)