-
-
Save anthrotype/6863c2f1b28b19c354f7 to your computer and use it in GitHub Desktop.
fl_unittest_example.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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