Skip to content

Instantly share code, notes, and snippets.

@bedekelly
Last active August 29, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bedekelly/93ee746b7750f730d586 to your computer and use it in GitHub Desktop.
Save bedekelly/93ee746b7750f730d586 to your computer and use it in GitHub Desktop.
Typechecking for Python - Test Suite
"""
Quick unit-test for typecheck.py.
Wonder if there's any way to unittest an exception's text?
"""
import unittest
from typecheck import typecheck
@typecheck
def add(x: int, y: int) -> int:
"""Returns the sum of the integers x and y as an integer."""
return x + y
@typecheck
def addReturnsString(x: int, y: int) -> int:
"""Returns the sum of the integers x and y as an 'integer'."""
return str(x + y)
class TestTypeChecking(unittest.TestCase):
def test_correct(self):
self.assertIsInstance(add(4,5), int)
def test_return_type(self):
with self.assertRaises(TypeError):
addReturnsString(54, 4)
def test_arg_type(self):
with self.assertRaises(TypeError):
add(4, "hello")
if __name__ == "__main__":
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment