-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunittest_example.py
46 lines (33 loc) · 979 Bytes
/
unittest_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import unittest
""" important note:
the tests will only be run if the name of the function in the
unittest.TestCase subclass begins with the work test
all tests must be labelled as such in their names
docs:
https://docs.python.org/3/library/unittest.html
run:
python unittest_example.py -v
to get a verbose output
"""
def fun(x):
return x + 1
class MyTest(unittest.TestCase):
def test_adder(self):
self.assertEqual(fun(3), 4)
def test_square(self):
self.assertEqual(2**2, 4)
self.assertEqual(4**2, 16)
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)
if __name__ == '__main__':
q