Skip to content

Instantly share code, notes, and snippets.

@worldofchris
Created March 13, 2014 14:44
Show Gist options
  • Save worldofchris/9529802 to your computer and use it in GitHub Desktop.
Save worldofchris/9529802 to your computer and use it in GitHub Desktop.
Demonstrate how to test dealing with 500 Internal Server Errors by patching the object that raises them using mock.
"""
Demonstrate how to test dealing with 500 Internal Server Errors,
e.g. when raised by calling Google Spreadsheet API
via https://github.com/burnash/gspread,
by patching the object that raises them using mock.
"""
from mock import patch
import gspread
from urllib2 import HTTPError, urlopen
from unittest import TestCase, main
USERNAME = 'your.user.name'
PASSWORD = 'your.password'
ID = 'your.spreadsheet,id'
class testGspread(TestCase):
def test_deal_with_500_error(self):
with patch.object(gspread.Worksheet,
'update_cell',
side_effect=HTTPError('http://does.not.work',
500,
'Internal Server Error',
'hdrs',
urlopen('http://www.google.com'))):
client = gspread.login(USERNAME, PASSWORD)
spreadsheet = client.open_by_key(ID)
worksheet = spreadsheet.worksheet('Sheet1')
# This should explode
with self.assertRaises(HTTPError):
worksheet.update_cell(1, 2, 3)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment