what is a list?
A list is a container object that can hold any objects
they are represented with
[]they can be made with
list()or[]they can be changed with an operation, which means they are mutable
preview
I have these tests by the end of the chapter
1import unittest
2
3
4class TestLists(unittest.TestCase):
5
6 def test_making_a_list(self):
7 self.assertEqual(list(), [])
8 self.assertEqual(list((0, 1, 2, 'n')), [0, 1, 2, 'n'])
10 def test_dir_list(self):
11 self.maxDiff = None
12
13 dir_list = dir(object)
14 dir_list.extend((
15 '__add__', '__class_getitem__',
16 '__contains__', '__delitem__',
17 '__getitem__', '__iadd__',
18 '__imul__', '__iter__', '__len__',
19 '__mul__', '__reversed__',
20 '__rmul__', '__setitem__',
21 'append', 'clear', 'copy',
22 'count', 'extend', 'index',
23 'insert', 'pop', 'remove',
24 'reverse', 'sort',
25 ))
26 dir_list.sort()
27 self.assertEqual(dir(list), dir_list)
29 def test_append_adds_item_to_end_of_a_list(self):
30 a_list = [0, 1, 2, 'n']
31 self.assertIsNone(a_list.append('n+1'))
32 self.assertEqual(a_list, [0, 1, 2, 'n', 'n+1'])
33
34 def test_clear_empties_a_list(self):
35 a_list = [0, 1, 2, 'n']
36 self.assertIsNone(a_list.clear())
37 self.assertEqual(a_list, [])
39 def test_copy_a_list(self):
40 a_list = [0, 1, 2, 'n']
41 self.assertEqual(a_list.copy(), [0, 1, 2, 'n'])
42
43 def test_count_number_of_times_item_is_in_a_list(self):
44 a_list = [0, 1, 2, 1, 'n', 1]
45 self.assertEqual(a_list.count(0), 1)
46 self.assertEqual(a_list.count(1), 3)
47 self.assertEqual(a_list.count('not in list'), 0)
49 def test_extend_adds_items_from_an_iterable_to_end_of_a_list(self):
50 a_list = [0, 1, 2, 'n']
51 self.assertIsNone(a_list.extend((2, 1, 0)))
52 self.assertEqual(a_list, [0, 1, 2, 'n', 2, 1, 0])
53
54 another_list = [0, 1, 2, 'n']
55 for number in (2, 1, 0):
56 another_list.append(number)
57 self.assertEqual(another_list, a_list)
59 def test_index_returns_the_first_position_of_a_given_item(self):
60 a_list = ['1st', '2nd', '3rd', '...last', '1st']
61 self.assertEqual(a_list.index('1st', 0), 0)
62 self.assertEqual(a_list.index('3rd', 0), 2)
63 self.assertEqual(a_list.index('2nd', 0), 1)
64 self.assertEqual(a_list.index('...last', 0), 3)
65 self.assertEqual(a_list.index('1st', 1), 4)
66
67 with self.assertRaises(ValueError):
68 a_list.index('not in list')
70 def test_insert_item_at_position_in_a_list(self):
71 a_list = [0, 1, 2, 'n']
72 self.assertIsNone(a_list.insert(0, -1))
73 self.assertEqual(a_list, [-1, 0, 1, 2, 'n'])
74 self.assertIsNone(a_list.insert(3, 1.5))
75 self.assertEqual(a_list, [-1, 0, 1, 1.5, 2, 'n'])
76
77 def test_pop_removes_and_returns_last_item_from_a_list(self):
78 a_list = [0, 1, 2, 'n']
79 self.assertEqual(a_list.pop(), 'n')
80 self.assertEqual(a_list, [0, 1, 2])
81 self.assertEqual(a_list.pop(), 2)
82 self.assertEqual(a_list, [0, 1])
84 def test_remove_first_time_item_is_in_a_list(self):
85 a_list = [0, 1, 0, 2, 0, 'n']
86 self.assertIsNone(a_list.remove(0))
87 self.assertEqual(a_list, [1, 0, 2, 0, 'n'])
88
89 with self.assertRaises(ValueError):
90 a_list.remove('not in list')
91
92 def test_reverse_a_list(self):
93 a_list = [0, 1, 2, 'n']
94 self.assertIsNone(a_list.reverse())
95 self.assertEqual(a_list, ['n', 2, 1, 0])
97 def test_sort_a_list(self):
98 with self.assertRaises(TypeError):
99 [0, 1, 2, 'n'].sort()
100
101 a_list = [0, 1, -1, 2, -2, 3, -3]
102 self.assertIsNone(a_list.sort())
103 self.assertEqual(a_list, [-3, -2, -1, 0, 1, 2, 3])
105 def test_getting_items_of_a_list(self):
106 a_list = ['1st', '2nd', '3rd', '...last']
107 self.assertEqual(a_list[0], '1st')
108 self.assertEqual(a_list[a_list.index('1st')], '1st')
109 self.assertEqual(a_list[-4], '1st')
110 self.assertEqual(a_list[2], '3rd')
111 self.assertEqual(a_list[-2], '3rd')
112 self.assertEqual(a_list[1], '2nd')
113 self.assertEqual(a_list[-3], '2nd')
114 self.assertEqual(a_list[3], '...last')
115 self.assertEqual(a_list[-1], '...last')
117 def test_setting_items_in_a_list(self):
118 a_list = ['1st', '2nd', '3rd', '...last']
119 a_list[-1] = '4th'
120 self.assertEqual(
121 a_list, ['1st', '2nd', '3rd', '4th']
122 )
123
124 def test_getting_parts_of_a_list_aka_slicing(self):
125 a_list = ['a', 'b', 'c', 'd']
126 self.assertEqual(a_list[0:2], ['a', 'b'])
127 self.assertEqual(a_list[:2], ['a', 'b'])
128 self.assertEqual(a_list[1:4], ['b', 'c', 'd'])
129 self.assertEqual(a_list[1:], ['b', 'c', 'd'])
130 self.assertEqual(a_list[0:3], ['a', 'b', 'c'])
131 self.assertEqual(a_list[1:3], ['b', 'c'])
132 self.assertEqual(a_list[:], a_list.copy())
134 def test_index_error(self):
135 a_list = ['a', 'b', 'c', 'd']
136
137 with self.assertRaises(IndexError):
138 a_list[4]
139 with self.assertRaises(IndexError):
140 a_list[-5]
141 with self.assertRaises(IndexError):
142 [].pop()
143 with self.assertRaises(IndexError):
144 [][-1]
145
146
147# Exceptions seen
148# AssertionError
149# TypeError
150# ValueError
151# IndexError
questions about lists
start the project
I open a terminal
I give
listsas the first argument to the makePythonTdd programthe terminal is my friend, and shows AssertionError
======================== FAILURES ========================== ______________ Testlists.test_failure _________________ self = <tests.test_lists.Testlists testMethod=test_failure> def test_failure(self): > self.assertFalse(True) E AssertionError: True is not false tests/test_lists.py:7: AssertionError ================== short test summary info ================== FAILED tests/test_lists.py::Testlists::test_failure - AssertionError: True is not false ===================== 1 failed in X.YZs =====================I open
test_lists.pyfrom thetestsfolder-
4class Testlists(unittest.TestCase): 5 6 def test_failure(self): 7 self.assertFalse(False) 8 9 10# Exceptions seenthe test passes.
I change
TestliststoTestListsto match Python conventionI open a new terminal then change directory to
listsI add a git commit message
test_making_a_list
RED: make it fail
I change test_failure to test_making_a_list
4class TestLists(unittest.TestCase):
5
6 def test_making_a_list(self):
7 self.assertEqual(list(), None)
the terminal is my friend, and shows AssertionError
GREEN: make it pass
I change the expectation
the test passes. I can make an empty list with list() or []
REFACTOR: make it better
what is an iterable?
An iterable is an object that can return what it has, one at a time. In order words, I can loop over it. strings, tuples, lists, sets and dictionaries are iterable.
I change the input to a tuple
6 def test_making_a_list(self): 7 self.assertEqual(list(), []) 8 self.assertEqual(list((0, 1, 2, 'n')), []) 9 10 11# Exceptions seenthe terminal is my friend, and shows AssertionError
I change the expectation to match
the test passes.
I add a git commit message
I can make a list with ‘list()’, or with square brackets(‘[]’) which uses less characters.
test_dir_list
I want to test the things I can do with lists. I use the dir built-in function to see the attributes and methods of lists
RED: make it fail
I add a failing test for the attributes and methods of lists
8 self.assertEqual(list((0, 1, 2, 'n')), [0, 1, 2, 'n'])
9
10 def test_dir_list(self):
11 self.assertEqual(
12 dir(list),
13 []
14 )
15
16
17# Exceptions seen
the terminal is my friend, and shows AssertionError
AssertionError: Lists differ: [
'__add__', '__class__', '__class_getitem_[552 chars]ort'
] != []
there is also a note on how to see the full difference between dir(list) and my empty list
Diff is 748 characters long. Set self.maxDiff to None to see it
maxDiff is an attribute of the unittest.TestCase class that sets the maximum number of characters to show when comparing 2 objects in the terminal, when it is set to None it shows the full difference.
GREEN: make it pass
I add
self.maxDiffto the test then move the terminal to the right10 def test_dir_list(self): 11 self.maxDiff = None 12 self.assertEqual( 13 dir(list), 14 [] 15 )the terminal is my friend, and shows a long list of items. I copy (ctrl/command+c) and paste (ctrl/command+v) them then use find and replace to remove the extra characters
Eand-Note
results can be different because of the Python version
10 def test_dir_list(self): 11 self.maxDiff = None 12 self.assertEqual( 13 dir(list), 14 [ 15 '__add__', 16 '__class__', 17 '__class_getitem__', 18 '__contains__', 19 '__delattr__', 20 '__delitem__', 21 '__dir__', 22 '__doc__', 23 '__eq__', 24 '__format__', 25 '__ge__', 26 '__getattribute__', 27 '__getitem__', 28 '__getstate__', 29 '__gt__', 30 '__hash__', 31 '__iadd__', 32 '__imul__', 33 '__init__', 34 '__init_subclass__', 35 '__iter__', 36 '__le__', 37 '__len__', 38 '__lt__', 39 '__mul__', 40 '__ne__', 41 '__new__', 42 '__reduce__', 43 '__reduce_ex__', 44 '__repr__', 45 '__reversed__', 46 '__rmul__', 47 '__setattr__', 48 '__setitem__', 49 '__sizeof__', 50 '__str__', 51 '__subclasshook__', 52 'append', 53 'clear', 54 'copy', 55 'count', 56 'extend', 57 'index', 58 'insert', 59 'pop', 60 'remove', 61 'reverse', 62 'sort' 63 ] 64 )the test passes. This looks like the attributes and methods of objects with more stuff.
I copy (ctrl/command+c) and paste (ctrl/command+v) the names that do NOT have double underscores (__) to use as a TODO list
I add a git commit message
The dir built-in function returns a list of attributes and methods.
test_append_adds_item_to_end_of_a_list
RED: make it fail
I add a test for the append method
62 'sort'
63 ]
64 )
65
66 def test_append(self):
67 a_list = [0, 1, 2, 'n']
68 self.assertIsNone(a_list.append())
69
70
71 'append',
GREEN: make it pass
I add 0 as input
the test is green again, the append method returns None when called.
REFACTOR: make it better
I add another assertion to see what append did to the list
66 def test_append(self): 67 a_list = [0, 1, 2, 'n'] 68 self.assertIsNone(a_list.append(0)) 69 self.assertEqual(a_list, [0, 1, 2, 'n']) 70 71 72'append',the terminal is my friend, and shows AssertionError
the append method added a value at the end of the list.
I change the expectation to match the values in the terminal
the test passes.
I change the value given to append
66 def test_append(self): 67 a_list = [0, 1, 2, 'n'] 68 self.assertIsNone(a_list.append('n+1')) 69 self.assertEqual(a_list, [0, 1, 2, 'n', 0])the terminal is my friend, and shows AssertionError
I change the expectation of the assertion to match
66 def test_append(self): 67 a_list = [0, 1, 2, 'n'] 68 self.assertIsNone(a_list.append('n+1')) 69 self.assertEqual(a_list, [0, 1, 2, 'n', 'n+1']) 70 71 72'append',the test passes.
I change the name of the test
I remove append from the TODO list
I add a git commit message
test_clear_empties_a_list
I add a test for the clear method
69 self.assertEqual(a_list, [0, 1, 2, 'n', 'n+1'])
70
71 def test_clear(self):
72 a_list = [0, 1, 2, 'n']
73 self.assertIsNone(a_list.clear())
74
75
76'clear',
the test is still green. The clear method returns None when called
RED: make it fail
I add an assertion to see what clear did to the list
71 def test_clear(self):
72 a_list = [0, 1, 2, 'n']
73 self.assertIsNone(a_list.clear())
74 self.assertEqual(a_list, [0, 1, 2, 'n'])
75
76
77'clear',
the terminal is my friend, and shows AssertionError
the list is now empty.
GREEN: make it pass
I change the values to match
the test passes.
REFACTOR: make it better
test_copy_a_list
RED: make it fail
I add a test for the copy method
74 self.assertEqual(a_list, [])
75
76 def test_copy(self):
77 a_list = [0, 1, 2, 'n']
78 self.assertIsNone(a_list.copy())
79
80
81'copy',
the terminal is my friend, and shows AssertionError
GREEN: make it pass
I add the list to the assertion
77 a_list = [0, 1, 2, 'n'] 78 self.assertIsNone(a_list.copy(), [0, 1, 2, 'n'])the terminal is my friend, and shows AssertionError
the values are the same, the problem is assertIsNone only takes 1 input and I gave it 2
I change assertIsNone to the assertEqual method
77 a_list = [0, 1, 2, 'n'] 78 self.assertEqual(a_list.copy(), [0, 1, 2, 'n'])the test passes.
REFACTOR: make it better
test_count_number_of_times_item_is_in_a_list
RED: make it fail
I add a test for the count method
78 self.assertEqual(a_list.copy(), [0, 1, 2, 'n'])
79
80 def test_count(self):
81 a_list = [0, 1, 2, 'n']
82 self.assertIsNone(a_list.count())
83
84
85'count',
the terminal is my friend, and shows TypeError
I add a value to the call
80 def test_count(self):
81 a_list = [0, 1, 2, 'n']
82 self.assertIsNone(a_list.count(0))
the terminal is my friend, and shows AssertionError
GREEN: make it pass
I add the value
the terminal is my friend, and shows AssertionError
I change assertIsNone to the assertEqual method
the test passes.
REFACTOR: make it better
0is in this list one time, I add1to the list two more times then add an assertion for it80 def test_count(self): 81 a_list = [0, 1, 2, 1, 'n', 1] 82 self.assertEqual(a_list.count(0), 1) 83 self.assertEqual(a_list.count(1), 1) 84 85 86'count',the terminal is my friend, and shows AssertionError
I change the expectation to match
the test passes.
I want to see what happens when I try to count something that is not in the list.
83 self.assertEqual(a_list.count(1), 3) 84 self.assertEqual(a_list.count('not in list'), 3)the terminal is my friend, and shows AssertionError
The count method returns
0when the item is not in the listI change the value to match
the test passes.
I change the name of the test
78 self.assertEqual(a_list.copy(), [0, 1, 2, 'n']) 79 80 def test_count_number_of_times_item_is_in_a_list(self): 81 a_list = [0, 1, 2, 1, 'n', 1] 82 self.assertEqual(a_list.count(0), 1) 83 self.assertEqual(a_list.count(1), 3) 84 self.assertEqual(a_list.count('not in list'), 0) 85 86 87'count',I remove count from the TODO list
I add a git commit message
test_extend_adds_items_from_an_iterable_to_end_of_a_list
RED: make it fail
I add a test for the extend method
84 self.assertEqual(a_list.count('not in list'), 0)
85
86 def test_extend(self):
87 a_list = [0, 1, 2, 'n']
88 self.assertIsNone(a_list.extend())
89
90
91'extend',
GREEN: make it pass
I pass a value to the call
the terminal is my friend, and shows TypeError
I change the value to an iterable
the test passes. The extend method returns None when called.
REFACTOR: make it better
I add another assertion to see what it did to the list
86 def test_extend(self): 87 a_list = [0, 1, 2, 'n'] 88 self.assertIsNone(a_list.extend((0, 1))) 89 self.assertEqual(a_list, [0, 1, 2, 'n']) 90 91 92'extend',the terminal is my friend, and shows AssertionError
I change the expectation to match
the test passes.
I change the values given to the extend method
86 def test_extend(self): 87 a_list = [0, 1, 2, 'n'] 88 self.assertIsNone(a_list.extend((2, 1, 0))) 89 self.assertEqual(a_list, [0, 1, 2, 'n', 0, 1])the terminal is my friend, and shows AssertionError
I change the values to match
the test is green again, it looks like extend calls append for each item in the iterable.
I add another assertion to see if the extend method does the same as using a for loop with the append method
86 def test_extend(self): 87 a_list = [0, 1, 2, 'n'] 88 self.assertIsNone(a_list.extend((2, 1, 0))) 89 self.assertEqual(a_list, [0, 1, 2, 'n', 2, 1, 0]) 90 91 another_list = [0, 1, 2, 'n'] 92 for number in (2, 1, 0): 93 another_list.append(number) 94 self.assertEqual(another_list, 'BOOM!!!') 95 96 97'extend',the terminal is my friend, and shows AssertionError
I change the expectation to
a_list91 another_list = [0, 1, 2, 'n'] 92 for number in (2, 1, 0): 93 another_list.append(number) 94 self.assertEqual(another_list, a_list) 95 96 97'extend',the test passes.
I change the name of the test
I remove extend from the TODO list
I add a git commit message
test_index_returns_the_first_position_of_a_given_item
RED: make it fail
I add a test for the index method
94 self.assertEqual(another_list, a_list)
95
96 def test_index(self):
97 a_list = [0, 1, 2, 'n']
98 self.assertIsNone(a_list.index())
99
100
101'index',
GREEN: make it pass
I add a value to the call
96 def test_index(self):
97 a_list = [0, 1, 2, 'n']
98 self.assertIsNone(a_list.index(0))
the terminal is my friend, and shows AssertionError
I add the expectation
the terminal is my friend, and shows AssertionError
I change assertIsNone to the assertEqual method
96 def test_index(self):
97 a_list = [0, 1, 2, 'n']
98 self.assertEqual(a_list.index(0), 0)
the test passes.
REFACTOR: make it better
I change the list to find to make sure the method did not return the same value I gave
96 def test_index(self): 97 a_list = ['1st', '2nd', '3rd', '...last'] 98 self.assertEqual(a_list.index(0), 0)the terminal is my friend, and shows ValueError
the index method raises ValueError if the item is not in the list
I add ValueError to the list of Exceptions seen
I remove the things around the call to the index method and change the value to make it clearer
96 def test_index(self): 97 a_list = ['1st', '2nd', '3rd', '...last'] 98 a_list.index('not in list')the terminal is my friend, and shows ValueError
I add assertRaises to handle the Exception
96 def test_index(self): 97 a_list = ['1st', '2nd', '3rd', '...last'] 98 99 with self.assertRaises(ValueError): 100 a_list.index('not in list')the test is green again
I add a new assertion
96 def test_index(self): 97 a_list = ['1st', '2nd', '3rd', '...last'] 98 self.assertEqual(a_list.index('1st'), '1st') 99 100 with self.assertRaises(ValueError): 101 a_list.index('not in list')the terminal is my friend, and shows AssertionError
I change the expectation
the test passes. The index method does not just return the value I give it
I add another assertion
96 def test_index(self): 97 a_list = ['1st', '2nd', '3rd', '...last'] 98 self.assertEqual(a_list.index('1st'), 0) 99 self.assertEqual(a_list.index('3rd'), 0) 100 101 with self.assertRaises(ValueError):the terminal is my friend, and shows AssertionError
I change the expectation of the assertion
the test passes.
I add another assertion
99 self.assertEqual(a_list.index('3rd'), 2) 100 self.assertEqual(a_list.index('2nd'), 2) 101 102 with self.assertRaises(ValueError): 103 a_list.index('not in list')the terminal is my friend, and shows AssertionError
I change the expectation to match
the test is green again
I add another assertion
100 self.assertEqual(a_list.index('2nd'), 1) 101 self.assertEqual(a_list.index('...last'), 1) 102 103 with self.assertRaises(ValueError):the terminal is my friend, and shows AssertionError
I change the expectation
the test passes.
The index method returns numbers for the position of the item in the list.
Python uses zero-based indexing which means the first item has an index of
0and the last item has an index of the length of the list minus1.
I add something in the list again to see what happens if I have the same item in the list more than once
96 def test_index(self): 97 a_list = ['1st', '2nd', '3rd', '...last', '1st']the test is still green.
I add an assertion
101 self.assertEqual(a_list.index('...last'), 3) 102 self.assertEqual(a_list.index('1st'), 4) 103 104 with self.assertRaises(ValueError):the terminal is my friend, and shows AssertionError
when I first called the index method, the terminal showed TypeError
which means I should be able to send more than one argument.
I add a second argument
the terminal still shows AssertionError
I change the argument
the test passes, the second input is the position I want the index method to start from
I try the same thing with the other assertions
96 def test_index(self): 97 a_list = ['1st', '2nd', '3rd', '...last', '1st'] 98 self.assertEqual(a_list.index('1st', 0), 0) 99 self.assertEqual(a_list.index('3rd', 0), 2) 100 self.assertEqual(a_list.index('2nd', 0), 1) 101 self.assertEqual(a_list.index('...last', 0), 3) 102 self.assertEqual(a_list.index('1st', 1), 4) 103 104 with self.assertRaises(ValueError): 105 a_list.index('not in list')the test is still green.
I change the name of the test
I remove index from the TODO list
I add a git commit message
test_insert_item_at_position_in_a_list
RED: make it fail
I add a test for the next method
104 with self.assertRaises(ValueError):
105 a_list.index('not in list')
106
107 def test_insert(self):
108 a_list = [0, 1, 2, 'n']
109 self.assertIsNone(a_list.insert())
110
111
112'insert',
GREEN: make it pass
I add two values to the call
107 def test_insert(self):
108 a_list = [0, 1, 2, 'n']
109 self.assertIsNone(a_list.insert(0, 1))
the test passes.
REFACTOR: make it better
I add an assertion to see what the insert method did to the list
107 def test_insert(self): 108 a_list = [0, 1, 2, 'n'] 109 self.assertIsNone(a_list.insert(0, 1)) 110 self.assertEqual(a_list, [0, 1, 2, 'n']) 111 112 113'insert',the terminal is my friend, and shows AssertionError
there is an extra value at the beginning of the list.
I add the new value to the expectation
the test passes.
The insert method
I change the second input in the call
107 def test_insert(self): 108 a_list = [0, 1, 2, 'n'] 109 self.assertIsNone(a_list.insert(0, -1)) 110 self.assertEqual(a_list, [0, 1, 2, 'n'])the terminal is my friend, and shows AssertionError
I change the expectation to match
the test is green again
I add another assertion to see what happens when I insert an item in the middle of the list
105 def test_insert(self): 106 a_list = [0, 1, 2, 'n'] 107 self.assertIsNone(a_list.insert(0, -1)) 108 self.assertEqual(a_list, [-1, 0, 1, 2, 'n']) 109 self.assertIsNone(a_list.insert(3, 1.5)) 110 self.assertEqual(a_list, [-1, 0, 1, 2, 'n']) 111 112 113'insert',the terminal is my friend, and shows AssertionError
I add the value to the expectation
the test passes. The insert method moved everything in the list from the index I gave and after, to the right.
I change the name of the test
I remove insert from the TODO list
I add a git commit message
test_pop_removes_and_returns_last_item_from_a_list
RED: make it fail
I add a test for the pop method
112 self.assertEqual(a_list, [-1, 0, 1, 1.5, 2, 'n'])
113
114 def test_pop(self):
115 a_list = [0, 1, 2, 'n']
116 self.assertIsNone(a_list.pop())
117
118
119'pop',
the terminal is my friend, and shows AssertionError
The pop method returns the last item in the list.
GREEN: make it pass
I add the expectation
the terminal is my friend, and shows AssertionError
I change assertIsNone to the assertEqual method
114 def test_pop(self):
115 a_list = [0, 1, 2, 'n']
116 self.assertEqual(a_list.pop(), 'n')
the test passes.
REFACTOR: make it better
I add another assertion to see what happened to the list
114 def test_pop(self): 115 a_list = [0, 1, 2, 'n'] 116 self.assertEqual(a_list.pop(), 'n') 117 self.assertEqual(a_list, [0, 1, 2, 'n']) 118 119 120'pop',the terminal is my friend, and shows AssertionError
I change the expectation of the assertion
the test passes.
I add another assertion with the pop method
114 def test_pop(self): 115 a_list = [0, 1, 2, 'n'] 116 self.assertEqual(a_list.pop(), 'n') 117 self.assertEqual(a_list, [0, 1, 2]) 118 self.assertEqual(a_list.pop(), 'n') 119 120 121'pop',the terminal is my friend, and shows AssertionError
The pop method returns the last item in the list.
I change the expectation of the assertion
the test passes.
I add another assertion to see what changed in the list
114 def test_pop(self): 115 a_list = [0, 1, 2, 'n'] 116 self.assertEqual(a_list.pop(), 'n') 117 self.assertEqual(a_list, [0, 1, 2]) 118 self.assertEqual(a_list.pop(), 2) 119 self.assertEqual(a_list, [0, 1, 2]) 120 121 122'pop',the terminal is my friend, and shows AssertionError
I change the expectation of the assertion
the test passes.
I change the name of the test
I take out pop from the TODO list
I add a git commit message
test_remove_first_time_item_is_in_a_list
RED: make it fail
time for the remove method
119 self.assertEqual(a_list, [0, 1])
120
121 def test_remove(self):
122 a_list = [0, 1, 2, 'n']
123 self.assertIsNone(a_list.remove())
124
125
126'remove',
GREEN: make it pass
I add a value to the call
the test is green again. The remove method returns None.
REFACTOR: make it better
I add an assertion to see what it did to the list
121 def test_remove(self): 122 a_list = [0, 1, 2, 'n'] 123 self.assertIsNone(a_list.remove(0)) 124 self.assertEqual(a_list, [0, 1, 2, 'n']) 125 126 127'remove',the terminal is my friend, and shows AssertionError
the remove method removes the item given as input, from the list.
I change the expectation
the test passes.
I change the values in the list to see what happens when an item is in there more than one time
121 def test_remove(self): 122 a_list = [0, 1, 0, 2, 0, 'n'] 123 self.assertIsNone(a_list.remove(0)) 124 self.assertEqual(a_list, [1, 2, 'n'])the terminal is my friend, and shows AssertionError
I change the values to match
the test passes. The remove method only removes the first time something shows up in the list.
I want to see what happens when I try to remove something that is not in the list
119 def test_remove(self): 120 a_list = [0, 1, 0, 2, 0, 'n'] 121 self.assertIsNone(a_list.remove(0)) 122 self.assertEqual(a_list, [1, 0, 2, 0, 'n']) 123 self.assertIsNone(a_list.remove('not in list')) 124 125 126'remove',the terminal is my friend, and shows ValueError
I remove the things around the call then add assertRaises
121 def test_remove(self): 122 a_list = [0, 1, 0, 2, 0, 'n'] 123 self.assertIsNone(a_list.remove(0)) 124 self.assertEqual(a_list, [1, 0, 2, 0, 'n']) 125 126 with self.assertRaises(ValueError): 127 a_list.remove('not in list') 128 129 130'remove',the test passes.
I change the name of the test
I take out remove from the TODO list
I add a git commit message
test_reverse_a_list
RED: make it fail
I add a test for the reverse method
126 with self.assertRaises(ValueError): 127 a_list.remove('not in list') 128 129 def test_reverse(self): 130 a_list = [0, 1, 2, 'n'] 131 self.assertIsNone(a_list.reverse()) 132 133 134'reverse',the reverse method returns None.
I add an assertion to see what it did to the list
129 def test_reverse(self): 130 a_list = [0, 1, 2, 'n'] 131 self.assertIsNone(a_list.reverse()) 132 self.assertEqual(a_list, [0, 1, 2, 'n']) 133 134 135'reverse',the terminal is my friend, and shows AssertionError
the reverse method reversed the order of the items in the list.
GREEN: make it pass
I change the expectation of the assertion
the test passes.
REFACTOR: make it better
test_sort_a_list
RED: make it fail
I add a test for the sort method
132 self.assertEqual(a_list, ['n', 2, 1, 0])
133
134 def test_sort(self):
135 a_list = [0, 1, 2, 'n']
136 self.assertIsNone(a_list.sort())
137
138
139'sort'
the terminal is my friend, and shows TypeError
I have to change 'n' to a number or change the other numbers to strings.
GREEN: make it pass
I remove the things around the call then add assertRaises
134 def test_sort(self):
135 a_list = [0, 1, 2, 'n']
136 with self.assertRaises(TypeError):
137 a_list.sort()
138
139
140'sort'
the test passes.
REFACTOR: make it better
I can use the list directly in the call
134 def test_sort(self): 135 a_list = [0, 1, 2, 'n'] 136 with self.assertRaises(TypeError): 137 [0, 1, 2, 'n'].sort()the test is still green.
I remove the variable because it is no longer used
134 def test_sort(self): 135 with self.assertRaises(TypeError): 136 [0, 1, 2, 'n'].sort()still green.
I add a new list that has only numbers, then add another assertion
134 def test_sort(self): 135 with self.assertRaises(TypeError): 136 [0, 1, 2, 'n'].sort() 137 138 a_list = [0, 1, 2, 3] 139 self.assertIsNone(a_list.sort()) 140 141 142'sort'the test is still green. The sort method returns None when called.
I add an assertion to see what the sort method did to the list
138 a_list = [0, 1, 2, 3] 139 self.assertIsNone(a_list.sort()) 140 self.assertEqual(a_list, []) 141 142 143'sort'the terminal is my friend, and shows AssertionError
the list stayed the same.
I change the expectation to match
the test passes. The name of the method is sort and I gave it a list that is sorted.
I change the list to see what happens when it is NOT sorted
134 def test_sort(self): 135 with self.assertRaises(TypeError): 136 [0, 1, 2, 'n'].sort() 137 138 a_list = [0, 1, -1, 2, -2, 3, -3] 139 self.assertIsNone(a_list.sort()) 140 self.assertEqual(a_list, [0, 1, 2, 3])the terminal is my friend, and shows AssertionError
the sort method changed the order of the list from smallest to biggest number.
I change the expectation of the assertion to match
the test passes.
I change the name of the test
I remove sort from the TODO list
I add a git commit message
test_getting_items_of_a_list
When I want an item that is in a list, I can give its index in square brackets([]) to get it.
RED: make it fail
I add a test for getting items of a list
140 self.assertEqual(a_list, [-3, -2, -1, 0, 1, 2, 3])
141
142 def test_getting_items_of_a_list(self):
143 a_list = ['1st', '2nd', '3rd', '...last']
144 self.assertEqual(a_list[0], '')
145
146
147# Exceptions seen
the terminal is my friend, and shows AssertionError
the first item has an index of 0.
GREEN: make it pass
I change the value of the expectation
the test passes.
REFACTOR: make it better
this is the opposite of the index method which takes in the item and returns its position, in this case I provide the index (position) and it returns the item, which means I can write this
142 def test_getting_items_of_a_list(self): 143 a_list = ['1st', '2nd', '3rd', '...last'] 144 self.assertEqual(a_list[0], '1st') 145 self.assertEqual(a_list[a_list.index('1st')], '') 146 147 148# Exceptions seenthe terminal is my friend, and shows AssertionError
I change the expectation to match
the test passes because
a_list.index('1st')returns0a_list[0]returns1st
I can also use negative numbers. The last item has an index of
-1and the first item has an index of negative the length of the list145 self.assertEqual(a_list[a_list.index('1st')], '1st') 146 self.assertEqual(a_list[-4], '')the terminal is my friend, and shows AssertionError
I change the expectation to match
the test passes.
I add another assertion
146 self.assertEqual(a_list[-4], '1st') 147 self.assertEqual(a_list[2], '')the terminal is my friend, and shows AssertionError
I change the expectation to match
the test is green again
I add another assertion
147 self.assertEqual(a_list[2], '3rd') 148 self.assertEqual(a_list[-2], '')the terminal is my friend, and shows AssertionError
I change the expectation
the test passes.
I add another assertion
148 self.assertEqual(a_list[-2], '3rd') 149 self.assertEqual(a_list[1], '')the terminal is my friend, and shows AssertionError
I change the expectation
the test passes.
I add another assertion
149 self.assertEqual(a_list[1], '2nd') 150 self.assertEqual(a_list[-3], '')the terminal is my friend, and shows AssertionError
I change the expectation
the test passes.
I add another assertion
150 self.assertEqual(a_list[-3], '2nd') 151 self.assertEqual(a_list[3], '')the terminal is my friend, and shows AssertionError
I change the expectation
the test passes.
I add another assertion
151 self.assertEqual(a_list[3], '...last') 152 self.assertEqual(a_list[-1], '')the terminal is my friend, and shows AssertionError
I make the expectation match
142 def test_getting_items_of_a_list(self): 143 a_list = ['1st', '2nd', '3rd', '...last'] 144 self.assertEqual(a_list[0], '1st') 145 self.assertEqual(a_list[a_list.index('1st')], '1st') 146 self.assertEqual(a_list[-4], '1st') 147 self.assertEqual(a_list[2], '3rd') 148 self.assertEqual(a_list[-2], '3rd') 149 self.assertEqual(a_list[1], '2nd') 150 self.assertEqual(a_list[-3], '2nd') 151 self.assertEqual(a_list[3], '...last') 152 self.assertEqual(a_list[-1], '...last') 153 154 155# Exceptions seenthe test passes.
I add a git commit message
test_setting_items_in_a_list
I can also use the index of an item in a list to change the item.
RED: make it fail
I add a test for setting items in a list
152 self.assertEqual(a_list[-1], '...last')
153
154 def test_setting_items_in_a_list(self):
155 a_list = ['1st', '2nd', '3rd', '...last']
156 a_list[-1] = '4th'
157 self.assertEqual(
158 a_list, ['1st', '2nd', '3rd', '...last']
159 )
160
161
162# Exceptions seen
the terminal is my friend, and shows AssertionError
AssertionError: Lists differ: ['1st', '2nd', '3rd', '4th']
!= ['1st', '2nd', '3rd', '...last']
I can use the index of an item to change its value in a list the way I point a name to a value when I make a variable.
GREEN: make it pass
test_getting_parts_of_a_list_aka_slicing
I can use indices to look at parts of a list, this is called slicing.
RED: make it fail
I add a test for looking at parts of a list
157 self.assertEqual(
158 a_list, ['1st', '2nd', '3rd', '4th']
159 )
160
161 def test_getting_parts_of_a_list(self):
162 a_list = ['a', 'b', 'c', 'd']
163 self.assertEqual(a_list[0:2], [])
164
165
166# Exceptions seen
the terminal is my friend, and shows AssertionError
GREEN: make it pass
I change the expectation to match
the test passes.
I give two values in square brackets([]), separated by a :
the first value is the index of the item I want to start from
the second value is the index of the item I want to stop at, plus
1because Python uses zero-based indexing
REFACTOR: make it better
I can skip the first number if the starting index is
0161 def test_getting_parts_of_a_list(self): 162 a_list = ['a', 'b', 'c', 'd'] 163 self.assertEqual(a_list[0:2], ['a', 'b']) 164 self.assertEqual(a_list[:2], []) 165 166 167# Exceptions seenthe terminal is my friend, and shows AssertionError
I change the expectation to match
the test is green again.
[0:2]and[:2]are the same.I add another assertion
164 self.assertEqual(a_list[:2], ['a', 'b']) 165 self.assertEqual(a_list[1:4], []) 166 167 168# Exceptions seenthe terminal is my friend, and shows AssertionError
I add the missing values
the test passes.
I can skip the second number when it is bigger than or the same as the number of things in the list
165 self.assertEqual(a_list[1:4], ['b', 'c', 'd']) 166 self.assertEqual(a_list[1:], []) 167 168 169# Exceptions seenthe terminal is my friend, and shows AssertionError
I add the missing values
the test passes.
[1:4]and[1:]are the same because this list has 4 things.I add another assertion
166 self.assertEqual(a_list[1:], ['b', 'c', 'd']) 167 self.assertEqual(a_list[0:3], [])the terminal is my friend, and shows AssertionError
I change the expectation
the test passes.
I add another assertion
167 self.assertEqual(a_list[0:3], ['a', 'b', 'c']) 168 self.assertEqual(a_list[1:3], [])the terminal is my friend, and shows AssertionError
I change the expectation
the test passes.
I can also skip the starting and ending numbers
168 self.assertEqual(a_list[1:3], ['b', 'c']) 169 self.assertEqual(a_list[:], [])the terminal is my friend, and shows AssertionError
I get the entire list back or a copy
the test passes.
This is also called slicing, I change the name of the test
I add a git commit message
test_index_error
IndexError is raised when I try to get an item from a list and use a number that points to something that is NOT in the list.
RED: make it fail
I add a test for IndexError
169 self.assertEqual(a_list[:], a_list.copy())
170
171 def test_index_error(self):
172 a_list = ['a', 'b', 'c', 'd']
173 a_list[4]
174
175
176# Exceptions seen
the terminal is my friend, and shows IndexError
when I use an index that is greater than or the same as the number of things in the list, I am pointing to something that is NOT in the list.
GREEN: make it pass
I add assertRaises
171 def test_index_error(self):
172 a_list = ['a', 'b', 'c', 'd']
173
174 with self.assertRaises(IndexError):
175 a_list[4]
the test passes.
REFACTOR: make it better
I add IndexError to the list of Exceptions seen
I get IndexError when I use a number that is smaller than the number of things in the list as a negative number
the terminal is my friend, and shows IndexError
I add the assertRaises method
174 with self.assertRaises(IndexError): 175 a_list[4] 176 with self.assertRaises(IndexError): 177 a_list[-5] 178 179 180# Exceptions seenthe test passes.
IndexError is also raised when I call the pop method with an empty list
174 with self.assertRaises(IndexError): 175 a_list[4] 176 with self.assertRaises(IndexError): 177 a_list[-5] 178 [].pop() 179 180 181# Exceptions seenthe terminal is my friend, and shows IndexError
I add assertRaises
176 with self.assertRaises(IndexError): 177 a_list[-5] 178 with self.assertRaises(IndexError): 179 [].pop() 180 181 182# Exceptions seenthe test passes.
I cannot remove the last item from a list that has no items, this is the same as trying to get an item from a list that has no items
178 with self.assertRaises(IndexError): 179 [].pop() 180 [][-1] 181 182 183# Exceptions seenthe terminal is my friend, and shows IndexError
I add assertRaises
178 with self.assertRaises(IndexError): 179 [].pop() 180 with self.assertRaises(IndexError): 181 [][-1] 182 183 184# Exceptions seenthe test passes. Any index given to an empty list raises IndexError.
I add a git commit message
test_dir_list with extend and sort
The attributes and methods of a list look like the attributes and methods of ‘object’ with extra things, and since they are both lists, I should be able to make the attributes and methods of a list by adding the “extra” to the attributes and methods of ‘object’.
RED: make it fail
I add a variable for the attributes and methods of a list, then add an assertion for it, in test_dir_list
10 def test_dir_list(self):
11 self.maxDiff = None
12
13 dir_list = dir(object)
14 dir_list.extend((
15 'append', 'clear', 'copy',
16 'count', 'extend', 'index',
17 'insert', 'pop', 'remove',
18 'reverse', 'sort',
19 ))
20 self.assertEqual(dir(list), dir_list)
21
22 self.assertEqual(
23 dir(list),
24 [
25 '__add__',
the terminal is my friend, and shows AssertionError
AssertionError: Lists differ: [
'__add__', '__class__', '__class_getitem__',[549 chars]ort'
] != [
'__class__', '__delattr__', '__dir__', '__do[370 chars]ort'
]
with a note
I am close, but not quite.
GREEN: make it pass
I use extend to append the thirteen additional elements
14 dir_list.extend(( 15 '__add__', '__class_getitem__', 16 '__contains__', '__delitem__', 17 '__getitem__', '__iadd__', 18 '__imul__', '__iter__', '__len__', 19 '__mul__', '__reversed__', 20 '__rmul__', '__setitem__', 21 'append', 'clear', 'copy', 22 'count', 'extend', 'index', 23 'insert', 'pop', 'remove', 24 'reverse', 'sort', 25 ))the terminal does not feel like my friend, it shows AssertionError. The details show what I added as missing and as extra. How can it be both? It looks like the problem is the position of the extra items.
I add a call to the sort method
14 dir_list.extend(( 15 '__add__', '__class_getitem__', 16 '__contains__', '__delitem__', 17 '__getitem__', '__iadd__', 18 '__imul__', '__iter__', '__len__', 19 '__mul__', '__reversed__', 20 '__rmul__', '__setitem__', 21 'append', 'clear', 'copy', 22 'count', 'extend', 'index', 23 'insert', 'pop', 'remove', 24 'reverse', 'sort', 25 )) 26 dir_list.sort() 27 self.assertEqual(dir(list), dir_list) 28 29 self.assertEqual( 30 dir(list), 31 [ 32 '__add__', 33 '__class__',the test passes, a reminder that all objects inherit from ‘object’.
REFACTOR: make it better
I remove the other assertion from test_dir_list
I add a git commit message
close the project
I close
test_lists.pyI click in the terminal where the tests are running
I use q on the keyboard to leave the tests
I change directory to the parent of
lists
review
I ran tests to show that I can make a list with list() or square brackets ([]), then I ran these tests for the methods of lists from append to sort
I also added tests for
How many questions can you answer after going through this chapter?
code from the chapter
what is next?
I know how to make a Python Test Driven Development environment manually.
I know how to make a Python Test Driven Development environment automatically.
I know how to make a Python Test Driven Development environment automatically with variables.
Would you like to refactor the attributes of the person object and instance with lists?
rate pumping python
If this has been a 7 star experience for you, please CLICK HERE to leave a 5 star review of pumping python. It helps other people get into the book too.