Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ethen8181
GitHub Repository: ethen8181/machine-learning
Path: blob/master/python/test.py
1470 views
1
# Associate youtube video
2
# https://www.youtube.com/watch?v=0Keq3E2bbeE
3
# https://www.youtube.com/watch?v=hKuw-8Gjwjo
4
import unittest
5
6
def my_contains(elem, lst):
7
"""Returns True if and only if the element is in the list"""
8
return elem in lst
9
10
def my_first(lst):
11
"""Returns the first element in the list"""
12
return lst[0]
13
14
def bigger(lst1, lst2):
15
"""
16
Returns True if the sum of the element in list 1
17
is greater than the sum of the elements in list 2
18
"""
19
return sum(lst1) > sum(lst2)
20
21
22
class Test(unittest.TestCase):
23
"""
24
class name starts with Test and inherits the unittest.TestCase,
25
then you organize your test into different test cases by defining
26
different methods, note that each method's name has to start with `test`.
27
two most common tests are assertEqual and assertTrue
28
"""
29
30
def setUp(self):
31
"""
32
if you find yourself using the same objects in multiple tests,
33
then you can define them in the setUp method and call them
34
"""
35
self.lst1 = [1, 2, 3]
36
self.lst2 = [-3, 4, 10]
37
38
def test_contains_simple_true(self):
39
"""assertTrue: pass test if it is true"""
40
self.assertTrue(my_contains(elem = 3, lst = [1, 2, 3]))
41
42
43
def test_first_number(self):
44
"""assertEqual: pass test if the result matches"""
45
self.assertEqual(my_first([1, 2, 3]), 1)
46
47
def test_first_empty(self):
48
"""
49
assertRaises, makes sure the code that fails returns the
50
indicated error, you pass in the expected error type, the
51
function call and all the other arguments to that function call
52
"""
53
self.assertRaises(IndexError, my_first, [])
54
55
def test_bigger_typical_true(self):
56
self.assertFalse(bigger(self.lst1, self.lst2))
57
58
59
if __name__ == '__main__':
60
# code that runs all the test
61
unittest.main()
62
63
64
65