Skip to content

Instantly share code, notes, and snippets.

@Ogreman
Created February 24, 2014 18:19
Show Gist options
  • Select an option

  • Save Ogreman/9193793 to your computer and use it in GitHub Desktop.

Select an option

Save Ogreman/9193793 to your computer and use it in GitHub Desktop.
# test_my_module.py
import unittest
class MyClassTest(unittest.TestCase):
def setUp(self):
self.user = SomeUserClass()
self.obj = MyClass(user=user)
def test_valid_input(self):
for i in xrange(18, 100):
self.assertEqual(
self.obj.handle_input(i),
MyClass.STATUSES.OK
)
def test_invalid_input(self):
for i in xrange(0, 17):
self.assertEqual(
self.obj.handle_input(i),
MyClass.STATUSES.FAILED
)
def test_exception(self):
with self.assertRaises(MyClass.InputException):
self.obj.handle_input('xyz')
def test_none_input(self):
with self.assertRaises(MyClass.InputException):
self.obj.handle_input(None)
# my_module.py
from myhelpermodule import Choices
class MyClass(object):
class InputException(Exception):
pass
STATUSES = Choices(
'OK',
'FAILED',
'NA',
)
def __init__(self, user):
self.user = user
self.status = MyClass.STATUSES.NA
def handle_input(self, n_in=None):
if isinstance(n_in, int):
if n_in >= 18 < 100:
return MyClass.STATUSES.OK
else:
return MyClass.STATUSES.FAILED
raise MyClass.InputException
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment