Skip to content

Instantly share code, notes, and snippets.

@anthrotype
Forked from marcdama/gist:02520ffbd11e2067ae87
Last active October 7, 2015 15:07
Show Gist options
  • Save anthrotype/6863c2f1b28b19c354f7 to your computer and use it in GitHub Desktop.
Save anthrotype/6863c2f1b28b19c354f7 to your computer and use it in GitHub Desktop.
fl_unittest_example.py
import unittest
class TestStringMethods(unittest.TestCase):
def test_upper(self):
self.assertEqual('foo'.upper(), 'FOO')
def test_isupper(self):
self.assertTrue('FOO'.isupper())
self.assertFalse('Foo'.isupper())
def test_split(self):
s = 'hello world'
self.assertEqual(s.split(), ['hello', 'world'])
# check that s.split fails when the separator is not a string
with self.assertRaises(TypeError):
s.split(2)
class TestListMethods(unittest.TestCase):
def test_index(self):
self.assertEqual(['a', 'b', 'c'].index('c'), 2)
def test_len(self):
self.assertEqual(len(['a', 'b', 'c']), 3)
def test_slice(self):
alist = ['a', 'b', 'c']
alist[1:2] = ['d']
self.assertEqual(alist, ['a', 'd', 'c'])
def run_tests():
import sys, io, inspect
current_module = sys.modules[__name__]
loader = unittest.TestLoader()
suite = unittest.TestSuite()
for name, obj in inspect.getmembers(current_module):
if inspect.isclass(obj) and issubclass(obj, unittest.TestCase):
tests = loader.loadTestsFromTestCase(obj)
suite.addTests(tests)
output = io.BytesIO()
unittest.TextTestRunner(stream=output, verbosity=2).run(suite)
print(output.getvalue())
if __name__ == "__main__":
fl.output = ""
run_tests()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment