Skip to content

Instantly share code, notes, and snippets.

@rorymurdock
Created November 23, 2019 11:50
Show Gist options
  • Save rorymurdock/f8c1ace6e35684261823530e19510478 to your computer and use it in GitHub Desktop.
Save rorymurdock/f8c1ace6e35684261823530e19510478 to your computer and use it in GitHub Desktop.
Examples for PyTest
def check_http_response(status_code, expected_code=None):
"""Checks if response is a expected or a known good response"""
if status_code == expected_code:
return True
elif status_code == 200:
print('HTTP 200\nOK')
return True
elif status_code == 201:
print('HTTP 201\nCreated')
return True
elif status_code == 204:
print('HTTP 204\nEmpty response')
return True
elif status_code == 400:
print('HTTP 400\nBad request')
return False
elif status_code == 401:
print('HTTP 401\nCheck Authorisation')
return False
elif status_code == 403:
print('HTTP 403\nPermission denied, check AW permissions')
return False
elif status_code == 404:
print('HTTP 404\nNot found')
return False
elif status_code == 422:
print('HTTP 422\nInvalid SearchBy Parameter')
return False
else:
print('Unknown code %s' % status_code)
return False
def return_string():
"""Returns a string"""
return "abc"
def return_int():
"""Returns a integer"""
return 123
def return_bool():
"""Returns a boolean"""
return True
def return_dict():
"""Returns a dictionary"""
dictionary = {}
dictionary["testing"] = 1234
return dictionary
# Improved function
def check_http_response(status_code, expected_code=None):
"""Checks if response is a expected or a known good response"""
status_codes = {}
status_codes[200] = True, 'HTTP 200: OK'
status_codes[201] = True, 'HTTP 201: Created'
status_codes[204] = True, 'HTTP 204: Empty Response'
status_codes[400] = False, 'HTTP 400: Bad Request'
status_codes[401] = False, 'HTTP 401: Check WSO Credentials'
status_codes[403] = False, 'HTTP 403: Permission denied'
status_codes[404] = False, 'HTTP 404: Not found'
status_codes[422] = False, 'HTTP 422: Invalid searchby Parameter'
if status_code == expected_code:
return True
if status_code in status_codes:
print(status_codes[status_code][1])
return status_codes[status_code][0]
print('Unknown code %s' % status_code)
return False
from functions import *
def test_return_string():
assert isinstance(return_string(), str) is True
assert return_string() == "abcdef"
def test_return_int():
assert isinstance(return_int(), int) is True
def test_return_bool():
assert isinstance(return_bool(), bool) is True
def test_return_dict():
assert isinstance(return_dict(), dict) is True
from functions import *
def test_return_string():
assert isinstance(return_string(), str) is True
assert return_string() == "abcdef"
def test_return_int():
assert isinstance(return_int(), int) is True
def test_return_bool():
assert isinstance(return_bool(), bool) is True
def test_return_dict():
assert isinstance(return_dict(), dict) is True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment