1import unittest
2
3class TestStringMethods(unittest.TestCase):
4
5 def test_upper(self):
6 self.assertEqual('foo'.upper(), 'FOO')
7
8 def test_isupper(self):
9 self.assertTrue('FOO'.isupper())
10 self.assertFalse('Foo'.isupper())
11
12 def test_split(self):
13 s = 'hello world'
14 self.assertEqual(s.split(), ['hello', 'world'])
15 # check that s.split fails when the separator is not a string
16 with self.assertRaises(TypeError):
17 s.split(2)
18
19if __name__ == '__main__':
20 unittest.main()
21
1class MyUnitTest(unittest.TestCase):
2 @classmethod
3 def setUpClass(cls):
4 do_something_expensive_for_all_sets_of_tests()
5
6class MyFirstSetOfTests(MyUnitTest):
7 @classmethod
8 def setUpClass(cls):
9 super(MyFirstSetOfTests, cls).setUpClass()
10 do_something_expensive_for_just_these_first_tests()
1'''
2In other words, assertEquals is a function to check if two variables are equal, for purposes of automated testing:
3'''
4
5def assertEquals(var1, var2):
6 if var1 == var2:
7 return True
8 else:
9 return False