lists¶
A list is a container object that can hold any objects
they are represented with
[]
they can be made with the list constructor
they can be changed by performing an operation - they are mutable
requirements¶
I open a terminal to run makePythonTdd.sh with
lists
as the name of the project./makePythonTdd.sh lists
on Windows without Windows Subsystem Linux use makePythonTdd.ps1
./makePythonTdd.ps1 lists
it makes the folders and files that are needed, installs packages, runs the first test, and the terminal shows AssertionError
E AssertionError: True is not false tests/test_lists.py:7: AssertionError
I hold
ctrl
(windows/linux) oroption
(mac) on the keyboard and use the mouse to click ontests/test_lists.py:7
to open it in the editorthen change
True
toFalse
to make the test pass
test_making_a_list¶
red: make it fail¶
I change test_failure
to test_making_a_list
import unittest
class TestLists(unittest.TestCase):
def test_making_a_list(self):
self.assertEqual(list(), None)
the terminal shows AssertionError
AssertionError: [] != None
green: make it pass¶
I change the expectation
self.assertEqual(list(), [])
the test passes. This is how to make an empty list
refactor: make it better¶
I add another assertion, this time with input to the list constructor
self.assertEqual(list(), [])
self.assertEqual(list(0), [])
the terminal shows TypeError
TypeError: 'int' object is not iterable
I add the error to the list of Exceptions encountered
# Exceptions Encountered
# AssertionError
# TypeError
I change the input to a tuple
self.assertEqual(list(), [])
self.assertEqual(list((0, 1, 2, 'n')), [])
the terminal shows AssertionError
AssertionError: Lists differ: [0, 1, 2, 'n'] != []
I change the expectation to match
self.assertEqual(list((0, 1, 2, 'n')), [0, 1, 2, 'n'])
the test passes. I can make a list with the constructor or square brackets([]
), which uses less characters
test_attributes_and_methods_of_lists¶
I use the dir function to see the attributes and methods of lists
red: make it fail¶
I add a failing test
def test_making_a_list(self):
...
def test_attributes_and_methods_of_lists(self):
self.assertEqual(
dir(list),
[
]
)
the terminal 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 entire difference
green: make it pass¶
I add
self.maxDiff
to the test then move the terminal to the rightdef test_attributes_and_methods_of_lists(self): self.maxDiff = None self.assertEqual( dir(list), [ ] )
the terminal shows a long list of items. I copy and paste them from the terminal then use find and replace to remove the extra characters
Note
results can be different because of Python version
def test_attributes_and_methods_of_lists(self): self.maxDiff = None self.assertEqual( dir(list), [ '__add__', ... '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort' ] )
the terminal shows passing tests and I move it back to the bottom. I ignore the names with double underscores (__), then copy and paste the other names to make a TODO list
'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort' # Exceptions Encountered # AssertionError # TypeError
test_append_adds_item_to_end_of_a_list¶
red: make it fail¶
I add a test for the append method
def test_attributes_and_methods_of_lists(self):
...
def test_append(self):
a_list = [0, 1, 2, 'n']
self.assertIsNone(a_list.append())
the terminal shows TypeError
TypeError: list.append() takes exactly one argument (0 given)
green: make it pass¶
I add 0
as input
self.assertIsNone(a_list.append(0))
the terminal shows green, the append method returns None when called
refactor: make it better¶
I add another assertion to see what append did to the list
self.assertIsNone(a_list.append(0)) self.assertEqual(a_list, [0, 1, 2, 'n'])
the terminal shows AssertionError
AssertionError: Lists differ: [0, 1, 2, 'n', 0] != [0, 1, 2, 'n']
the method added a value. I change the expectation to match the values in the terminal
self.assertEqual(a_list, [0, 1, 2, 'n', 0])
the test passes
I change the value given to append
self.assertIsNone(a_list.append('n+1'))
the terminal shows AssertionError
AssertionError: Lists differ: [0, 1, 2, 'n', 'n+1'] != [0, 1, 2, 'n', 0]
I change the expectation to match
self.assertEqual(a_list, [0, 1, 2, 'n', 'n+1'])
the test passes
I rename the test
def test_append_adds_item_to_end_of_a_list(self): a_list = [0, 1, 2, 'n'] self.assertIsNone(a_list.append('n+1')) self.assertEqual(a_list, [0, 1, 2, 'n', 'n+1'])
I remove append from the TODO list
'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'
test_clear_empties_a_list¶
I add a test for the clear method
def test_append_adds_item_to_end_of_a_list(self):
...
def test_clear(self):
a_list = [0, 1, 2, 'n']
self.assertIsNone(a_list.clear())
the terminal shows green. The clear method returns None when called
red: make it fail¶
I add an assertion to show what it did to the list
self.assertIsNone(a_list.clear())
self.assertEqual(a_list, [0, 1, 2, 'n'])
the terminal shows AssertionError
AssertionError: Lists differ: [] != [0, 1, 2, 'n']
the list is now empty
green: make it pass¶
I change the values to match
self.assertEqual(a_list, [])
the test passes
refactor: make it better¶
I change the name of the test to be more descriptive
def test_clear_empties_a_list(self): a_list = [0, 1, 2, 'n'] self.assertIsNone(a_list.clear()) self.assertEqual(a_list, [])
I remove clear from the TODO list
'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'
test_copy_a_list¶
red: make it fail¶
I add another test
def test_clear_empties_a_list(self):
...
def test_copy(self):
a_list = [0, 1, 2, 'n']
self.assertIsNone(a_list.copy())
the terminal shows AssertionError
AssertionError: [0, 1, 2, 'n'] is not None
green: make it pass¶
I add the list to the assertion
a_list = [0, 1, 2, 'n']
self.assertIsNone(a_list.copy(), [0, 1, 2, 'n'])
the terminal shows AssertionError
AssertionError: [0, 1, 2, 'n'] is not None : [0, 1, 2, 'n']
the values are the same, I change assertIsNone to assertEqual
a_list = [0, 1, 2, 'n']
self.assertEqual(a_list.copy(), [0, 1, 2, 'n'])
the test passes
refactor: make it better¶
I change the name of the test
def test_copy_a_list(self): a_list = [0, 1, 2, 'n'] self.assertEqual(a_list.copy(), [0, 1, 2, 'n'])
I remove copy from the TODO list
'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'
test_count_number_of_times_item_is_in_a_list¶
red: make it fail¶
I add a test for the next method
def test_copy_a_list(self):
...
def test_count(self):
a_list = [0, 1, 2, 'n']
self.assertIsNone(a_list.count())
the terminal shows TypeError
TypeError: list.count() takes exactly one argument (0 given)
I add a value to the call
def test_count(self):
a_list = [0, 1, 2, 'n']
self.assertIsNone(a_list.count(0))
the terminal shows AssertionError
AssertionError: 1 is not None
green: make it pass¶
I add the value
self.assertIsNone(a_list.count(0), 1)
the terminal shows AssertionError
AssertionError: 1 is not None : 1
I change assertIsNone to assertEqual
self.assertEqual(a_list.count(0), 1)
the test passes
refactor: make it better¶
0
is in the list 1 time, I add1
to it 2 more times then add an assertion for itdef test_count(self): a_list = [0, 1, 2, 1, 'n', 1] self.assertEqual(a_list.count(0), 1) self.assertEqual(a_list.count(1), 1)
the terminal shows AssertionError
AssertionError: 3 != 1
I change the value to match
self.assertEqual(a_list.count(1), 3)
the test passes
I want to see what happens when I try to count an item that is not in the list
self.assertEqual(a_list.count(1), 3) self.assertEqual(a_list.count('not in list'), 3)
the terminal shows AssertionError
AssertionError: 0 != 3
The count method returns
0
when the item is not in the list. I change the value to matchself.assertEqual(a_list.count('not in list'), 0)
the test passes
I rename the test
def test_count_number_of_times_item_is_in_a_list(self): a_list = [0, 2, 1, 2, 3, 2] self.assertEqual(a_list.count(0), 1) self.assertEqual(a_list.count(2), 3) self.assertEqual(a_list.count(9), 0)
I remove count from the TODO list
'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'
test_extend_adds_items_from_an_iterable_to_end_of_a_list¶
red: make it fail¶
time for another test
def test_count_number_of_times_item_is_in_a_list(self):
...
def test_extend(self):
a_list = [0, 1, 2, 'n']
self.assertIsNone(a_list.extend())
the terminal shows TypeError
TypeError: list.extend() takes exactly one argument (0 given)
green: make it pass¶
I pass a value to the call
self.assertIsNone(a_list.extend(0))
the terminal shows TypeError
TypeError: 'int' object is not iterable
I change the value to an iterable
self.assertIsNone(a_list.extend((0, 1)))
refactor: make it better¶
I add another assertion to see what it did to the list
self.assertIsNone(a_list.extend((0, 1))) self.assertEqual(a_list, [0, 1, 2, 'n'])
the terminal shows AssertionError
AssertionError: Lists differ: [0, 1, 2, 'n', 0, 1] != [0, 1, 2, 'n']
I change the expectation to match
self.assertEqual(a_list, [0, 1, 2, 'n', 0, 1])
the test passes
I change the values given to the extend method
def test_extend(self): a_list = [0, 1, 2, 'n'] self.assertIsNone(a_list.extend((2, 1, 0))) self.assertEqual(a_list, [0, 1, 2, 'n', 0, 1])
the terminal shows AssertionError
AssertionError: Lists differ: [0, 1, 2, 'n', 2, 1, 0] != [0, 1, 2, 'n', 0, 1]
I change the values to match
self.assertEqual(a_list, [0, 1, 2, 'n', 2, 1, 0])
the test is green again, it looks like extend calls append for each item in the iterable
I change the name of the test
def test_extend_adds_items_from_an_iterable_to_end_of_a_list(self): a_list = [0, 1, 2, 'n'] self.assertIsNone(a_list.extend((2, 1, 0))) self.assertEqual(a_list, [0, 1, 2, 'n', 2, 1, 0])
I remove extend from the TODO list
'index', 'insert', 'pop', 'remove', 'reverse', 'sort'
test_index_returns_first_position_of_item_in_a_list¶
red: make it fail¶
I add a test for the index method
def test_extend_adds_items_from_an_iterable_to_end_of_a_list(self):
...
def test_index(self):
a_list = [0, 1, 2, 'n']
self.assertIsNone(a_list.index())
the terminal shows TypeError
TypeError: index expected at least 1 argument, got 0
green: make it pass¶
I add a value to the call
def test_index(self):
a_list = [0, 1, 2, 'n']
self.assertIsNone(a_list.index(0))
the terminal shows AssertionError
AssertionError: 0 is not None
I add the expectation
self.assertIsNone(a_list.index(0), 0)
the terminal shows AssertionError
AssertionError: 0 is not None : 0
I change assertIsNone to assertEqual
def test_index(self):
a_list = [0, 1, 2, 'n']
self.assertEqual(a_list.index(0), 0)
the test passes
refactor: make it better¶
it does not tell me if the method returned the same value I gave, so I change the list
def test_index(self): a_list = ['1st', '2nd', '3rd', '...last'] self.assertEqual(a_list.index(0), 0)
the terminal shows ValueError
ValueError: 0 is not in list
the index method raises ValueError when the item is not in the list. I add it to the list of Exceptions encountered
# Exceptions Encountered # AssertionError # TypeError # ValueError
I remove the things around the call and change the value to be more descriptive
def test_index(self): a_list = ['1st', '2nd', '3rd', '...last'] a_list.index('not in list')
the terminal shows ValueError
ValueError: 'not in list' is not in list
I add assertRaises to handle the Exception
def test_index(self): a_list = ['1st', '2nd', '3rd', '...last'] with self.assertRaises(ValueError): a_list.index('not in list')
the test is green again
I add a new assertion
def test_index(self): a_list = ['1st', '2nd', '3rd', '...last'] self.assertEqual(a_list.index('1st'), '1st') with self.assertRaises(ValueError): a_list.index('not in list')
the terminal shows AssertionError
AssertionError: 0 != '1st'
I change the expectation
self.assertEqual(a_list.index('1st'), 0)
the test passes
I add another assertion
self.assertEqual(a_list.index('1st'), 0) self.assertEqual(a_list.index('3rd'), 0)
the terminal shows AssertionError
AssertionError: 2 != 0
I change the value in the test
self.assertEqual(a_list.index('3rd'), 2)
the test passes
I add another assertion
self.assertEqual(a_list.index('3rd'), 2) self.assertEqual(a_list.index('2nd'), 2)
the terminal shows AssertionError
AssertionError: 1 != 2
I change the value to match
self.assertEqual(a_list.index('2nd'), 1)
the test is green again
I add another assertion
self.assertEqual(a_list.index('2nd'), 1) self.assertEqual(a_list.index('...last'), 1)
the terminal shows AssertionError
AssertionError: 3 != 1
I change the value to match the terminal
self.assertEqual(a_list.index('...last'), 3)
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
0
and the last item has an index of the length of the list minus1
I want to know what would happen if I have the same item in the list more than once, so I add a duplicate
def test_index(self): a_list = ['1st', '2nd', '3rd', '...last', '1st']
the terminal still shows green. I add an assertion
self.assertEqual(a_list.index('...last'), 3) self.assertEqual(a_list.index('1st'), 4)
the terminal shows AssertionError
AssertionError: 0 != 4
when I first called the index method, the terminal showed TypeError
TypeError: index expected at least 1 argument, got 0
I try a second argument
self.assertEqual(a_list.index('1st', 0), 4)
the terminal still shows AssertionError
AssertionError: 0 != 4
I change the argument
self.assertEqual(a_list.index('1st', 1), 4)
the test passes, the second input is the position I want the method to start from. I try the same thing with the other assertions
def test_index(self): a_list = ['1st', '2nd', '3rd', '...last', '1st'] self.assertEqual(a_list.index('1st', 0), 0) self.assertEqual(a_list.index('3rd', 0), 2) self.assertEqual(a_list.index('2nd', 0), 1) self.assertEqual(a_list.index('...last', 0), 3) self.assertEqual(a_list.index('1st', 1), 4) with self.assertRaises(ValueError): a_list.index('not in list')
the test is still green
I rename the test
def test_index_returns_first_position_of_item_in_a_list(self): a_list = ['1st', '2nd', '3rd', '...last', '1st'] ...
I also remove index from the TODO list
'insert', 'pop', 'remove', 'reverse', 'sort'
test_insert_item_at_given_index_in_a_list¶
red: make it fail¶
I add a test for the next method
def test_index_returns_first_position_of_item_in_a_list(self):
...
def test_insert(self):
a_list = [0, 1, 2, 'n']
self.assertIsNone(a_list.insert())
the terminal shows TypeError
TypeError: insert expected 2 arguments, got 0
green: make it pass¶
I add two values to the call
def test_insert(self):
a_list = [0, 1, 2, 'n']
self.assertIsNone(a_list.insert(0, 1))
the test is green
refactor: make it better¶
I add an assertion
self.assertIsNone(a_list.insert(0, 1)) self.assertEqual(a_list, [0, 1, 2, 'n'])
the terminal shows AssertionError
AssertionError: Lists differ: [1, 0, 1, 2, 'n'] != [0, 1, 2, 'n']
I add the new value to the list
self.assertEqual(a_list, [1, 0, 1, 2, 'n'])
the test passes. The insert method places the second input given at the index given as the first , it also moves the original items from that index and on in the list to the right
I change the second input in the call
self.assertIsNone(a_list.insert(0, -1))
the terminal shows AssertionError
AssertionError: Lists differ: [-1, 0, 1, 2, 'n'] != [1, 0, 1, 2, 'n']
I change the expectation to match
self.assertEqual(a_list, [-1, 0, 1, 2, 'n'])
the test is green again
I add another assertion to see what happens when I insert an item in the middle of the list
self.assertEqual(a_list, [-1, 0, 1, 2, 'n']) self.assertIsNone(a_list.insert(3, 1.5))
the terminal shows green. I add an assertion to see what it did to the list
self.assertIsNone(a_list.insert(3, 1.5)) self.assertEqual(a_list, [-1, 0, 1, 2, 'n'])
the terminal shows AssertionError
AssertionError: Lists differ: [-1, 0, 1, 1.5, 2, 'n'] != [-1, 0, 1, 2, 'n']
I add the value to the expectation
self.assertEqual(a_list, [-1, 0, 1, 1.5, 2, 'n'])
the test passes, it moved everything from the given index and after to the right
I rename the test
def test_insert_item_at_given_index_in_a_list(self): a_list = [0, 1, 2, 'n'] self.assertIsNone(a_list.insert(0, -1)) self.assertEqual(a_list, [-1, 0, 1, 2, 'n']) self.assertIsNone(a_list.insert(3, 1.5)) self.assertEqual(a_list, [-1, 0, 1, 1.5, 2, 'n'])
I remove insert from the TODO list
'pop', 'remove', 'reverse', 'sort'
test_pop_removes_and_returns_last_item_from_a_list¶
red: make it fail¶
I add a new test
def test_insert_item_at_given_index_in_a_list(self):
...
def test_pop(self):
a_list = [0, 1, 2, 'n']
self.assertIsNone(a_list.pop())
the terminal shows AssertionError
AssertionError: 'n' is not None
green: make it pass¶
I add the expectation
self.assertIsNone(a_list.pop(), 'n')
the terminal shows AssertionError
AssertionError: 'n' is not None : n
I change assertIsNone to assertEqual
a_list = [0, 1, 2, 'n']
self.assertEqual(a_list.pop(), 'n')
the test passes
refactor: make it better¶
I add an assertion
self.assertEqual(a_list.pop(), 'n') self.assertEqual(a_list, [0, 1, 2, 'n'])
the terminal shows AssertionError
AssertionError: Lists differ: [0, 1, 2] != [0, 1, 2, 'n']
I change the values in the test to match
self.assertEqual(a_list, [0, 1, 2])
the test passes
I add another assertion
self.assertEqual(a_list, [0, 1, 2]) self.assertEqual(a_list.pop(), 'n')
the terminal shows AssertionError
AssertionError: 2 != 'n'
I change the value
self.assertEqual(a_list, [0, 1, 2]) self.assertEqual(a_list.pop(), 2)
the test passes. I add another assertion
self.assertEqual(a_list.pop(), 2) self.assertEqual(a_list, [0, 1, 2])
the terminal shows AssertionError
AssertionError: Lists differ: [0, 1] != [0, 1, 2]
I change the values in the test to match
self.assertEqual(a_list, [0, 1])
the test passes
I rename the test
def test_pop_removes_and_returns_last_item_from_a_list(self): a_list = [0, 1, 2, 'n'] self.assertEqual(a_list.pop(), 'n') self.assertEqual(a_list, [0, 1, 2]) self.assertEqual(a_list.pop(), 2) self.assertEqual(a_list, [0, 1])
I take out pop from the TODO list
'remove', 'reverse', 'sort'
test_remove_first_instance_of_item_from_a_list¶
red: make it fail¶
time for the next method
def test_pop_removes_and_returns_last_item_from_a_list(self):
...
def test_remove(self):
a_list = [0, 1, 2, 'n']
self.assertIsNone(a_list.remove())
the terminal shows TypeError
TypeError: list.remove() takes exactly one argument (0 given)
green: make it pass¶
I add a value to the call
self.assertIsNone(a_list.remove(0))
refactor: make it better¶
I add an assertion
self.assertIsNone(a_list.remove(0)) self.assertEqual(a_list, [0, 1, 2, 'n'])
the terminal shows AssertionError
AssertionError: Lists differ: [1, 2, 'n'] != [0, 1, 2, 'n']
the method removes the item given from the list. I change the expectation
self.assertEqual(a_list, [1, 2, 'n'])
the test passes
I change the values in the list to see what would happen if an item is in there more than once
def test_remove(self): a_list = [0, 1, 0, 2, 0, 'n'] self.assertIsNone(a_list.remove(0)) self.assertEqual(a_list, [1, 2, 'n'])
the terminal shows AssertionError
AssertionError: Lists differ: [1, 0, 2, 0, 'n'] != [1, 2, 'n']
I change the values to match
self.assertEqual(a_list, [1, 0, 2, 0, 'n'])
the test passes
I want to know what would happen if I try to remove an item that is not in the list
self.assertEqual(a_list, [1, 0, 2, 0, 'n']) self.assertIsNone(a_list.remove('not in list'))
the terminal shows ValueError
ValueError: list.remove(x): x not in list
I remove the things around the call then add assertRaises
self.assertEqual(a_list, [1, 0, 2, 0, 'n']) with self.assertRaises(ValueError): a_list.remove('not in list')
the test passes
I rename the test
def test_remove_first_instance_of_item_from_a_list(self): a_list = [0, 1, 0, 2, 0, 'n'] self.assertIsNone(a_list.remove(0)) self.assertEqual(a_list, [1, 0, 2, 0, 'n']) with self.assertRaises(ValueError): a_list.remove('not in list')
I take out remove from the TODO list
'reverse', 'sort'
test_reverse_a_list¶
red: make it fail¶
I add the next test
def test_remove_first_instance_of_item_from_a_list(self):
...
def test_reverse(self):
a_list = [0, 1, 2, 'n']
self.assertIsNone(a_list.reverse())
the terminal shows green. This method returns None. I add an assertion to see what it did to the list
self.assertIsNone(a_list.reverse())
self.assertEqual(a_list, [0, 1, 2, 'n'])
the terminal shows AssertionError
AssertionError: Lists differ: ['n', 2, 1, 0] != [0, 1, 2, 'n']
it reversed the order of the items in the list
green: make it pass¶
I change the expectation
self.assertEqual(a_list, ['n', 2, 1, 0])
the test passes
refactor: make it better¶
I rename the test
def test_reverse_a_list(self): a_list = [0, 1, 2, 'n'] self.assertIsNone(a_list.reverse()) self.assertEqual(a_list, ['n', 2, 1, 0])
I remove the name from the TODO list
'sort'
test_sort_a_list¶
red: make it fail¶
I add a test
def test_sort(self):
a_list = [0, 1, 2, 'n']
self.assertIsNone(a_list.sort())
the terminal shows TypeError
TypeError: '<' not supported between instances of 'str' and 'int'
I have to change 'n'
to a number or change the other numbers to a string
green: make it pass¶
I remove the things around the call, and the variable name since it is not used, then add assertRaises
def test_sort(self):
with self.assertRaises(TypeError):
[0, 1, 2, 'n'].sort()
the test passes
refactor: make it better¶
I add a new list and another assertion
def test_sort(self): with self.assertRaises(TypeError): [0, 1, 2, 'n'].sort() a_list = [0, 1, 2, 3] self.assertIsNone(a_list.sort())
the terminal still shows green, sort returns None when called. I add another assertion to see what it did to the list
self.assertIsNone(a_list.sort()) self.assertEqual(a_list, [])
the terminal shows AssertionError
AssertionError: Lists differ: [0, 1, 2, 3] != []
the list stayed the same. I change the expectation
self.assertEqual(a_list, [0, 1, 2, 3])
the test passes. The name of the method is sort and I gave it a list that is sorted, I change it to see what would happen when it is not sorted
a_list = [0, 1, -1, 2, -2, 3, -3]
I get AssertionError
AssertionError: Lists differ: [-3, -2, -1, 0, 1, 2, 3] != [0, 1, 2, 3]
the sort method arranged the list in ascending order. I change the values to match
self.assertEqual(a_list, [-3, -2, -1, 0, 1, 2, 3])
the test passes
I change the name of the test
def test_sort_a_list(self): with self.assertRaises(TypeError): [0, 1, 2, 'n'].sort() a_list = [0, 1, -1, 2, -2, 3, -3] self.assertIsNone(a_list.sort()) self.assertEqual(a_list, [-3, -2, -1, 0, 1, 2, 3])
I remove sort from the TODO list
test_getting_items_of_a_list¶
When I want an item that is in a list, I can give its index in square brackets([]
)
red: make it fail¶
I add a failing test
def test_sort_a_list(self):
...
def test_getting_items_of_a_list(self):
a_list = ['1st', '2nd', '3rd', '...last']
self.assertEqual(a_list[0], '')
the terminal shows AssertionError
AssertionError: '1st' != ''
the first item has an index of 0
green: make it pass¶
I change the value in the test
self.assertEqual(a_list[0], '1st')
the test passes
refactor: make it better¶
this is the reverse of the index method which takes in the item and returns its position, in this case I provide the index and it returns the item, which means I can write this
self.assertEqual(a_list[0], '1st') self.assertEqual(a_list[a_list.index('1st')], '')
the terminal shows AssertionError
AssertionError: '1st' != ''
I change the value to match
self.assertEqual(a_list[a_list.index('1st')], '1st')
the test passes
I can also use negative numbers. The last item has an index of
-1
and the first item has an index of negative the length of the listself.assertEqual(a_list[a_list.index('1st')], '1st') self.assertEqual(a_list[-4], '')
the terminal shows AssertionError
AssertionError: '1st' != ''
I change the value to match
self.assertEqual(a_list[-4], '1st')
the test passes
I add another assertion
self.assertEqual(a_list[-4], '1st') self.assertEqual(a_list[2], '')
the terminal shows AssertionError
AssertionError: '3rd' != ''
I change the expectation to match
self.assertEqual(a_list[2], '3rd')
the terminal shows green again
I add another assertion
self.assertEqual(a_list[2], '3rd') self.assertEqual(a_list[-2], '')
and get AssertionError
AssertionError: '3rd' != ''
I change the expectation
self.assertEqual(a_list[-2], '3rd')
the test passes
I add another line
self.assertEqual(a_list[-2], '3rd') self.assertEqual(a_list[1], '')
the terminal shows AssertionError
AssertionError: '2nd' != ''
I make the test pass
self.assertEqual(a_list[1], '2nd')
I add another
self.assertEqual(a_list[1], '2nd') self.assertEqual(a_list[-3], '')
the terminal shows AssertionError
AssertionError: '2nd' != ''
I change the value
self.assertEqual(a_list[-3], '2nd')
the test passes
I add another assertion
self.assertEqual(a_list[-3], '2nd') self.assertEqual(a_list[3], '')
and get AssertionError
AssertionError: '...last' != ''
I change the value
self.assertEqual(a_list[3], '...last')
the test passes
I add another assertion
self.assertEqual(a_list[3], '...last') self.assertEqual(a_list[-1], '')
the terminal shows AssertionError
AssertionError: '...last' != ''
I make the values match
self.assertEqual(a_list[-1], '...last')
the test passes
test_setting_items_in_a_list¶
red: make it fail¶
I add a test
def test_getting_items_of_a_list(self):
...
def test_setting_items_in_a_list(self):
a_list = ['1st', '2nd', '3rd', '...last']
a_list[-1] = '4th'
self.assertEqual(a_list, ['1st', '2nd', '3rd', '...last'])
the terminal 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
green: make it pass¶
I change the expectation to match the terminal
self.assertEqual(a_list, ['1st', '2nd', '3rd', '4th'])
the test passes
test_viewing_parts_of_a_list_aka_slicing¶
red: make it fail¶
I add another test
def test_setting_items_in_a_list(self):
...
def test_viewing_parts_of_a_list(self):
a_list = ['a', 'b', 'c', 'd']
self.assertEqual(a_list[0:2], [])
the terminal shows AssertionError
AssertionError: Lists differ: ['a', 'b'] != []
green: make it pass¶
I change the values to match
self.assertEqual(a_list[0:2], ['a', 'b'])
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, and the second value is the index of the item I want to stop at plus 1
refactor: make it better¶
I can skip the first number when the starting index is
0
self.assertEqual(a_list[0:2], ['a', 'b']) self.assertEqual(a_list[:2], [])
the terminal shows AssertionError
AssertionError: Lists differ: ['a', 'b'] != []
I change the values to match
self.assertEqual(a_list[:2], ['a', 'b'])
the terminal shows green again
I add another assertion
self.assertEqual(a_list[:2], ['a', 'b']) self.assertEqual(a_list[1:4], [])
the terminal shows AssertionError
AssertionError: Lists differ: ['b', 'c', 'd'] != []
I add the missing values
self.assertEqual(a_list[1:4], ['b', 'c', 'd'])
the test passes
I can skip the second number when it is bigger than or the same as the length of the list
self.assertEqual(a_list[1:4], ['b', 'c', 'd']) self.assertEqual(a_list[1:], [])
the terminal shows AssertionError
AssertionError: Lists differ: ['b', 'c', 'd'] != []
I add the missing values
self.assertEqual(a_list[1:], ['b', 'c', 'd'])
the test passes
I add another line
self.assertEqual(a_list[1:], ['b', 'c', 'd']) self.assertEqual(a_list[0:3], [])
the terminal shows AssertionError
AssertionError: Lists differ: ['a', 'b', 'c'] != []
I change the expectation
self.assertEqual(a_list[0:3], ['a', 'b', 'c'])
the test is green again
I add another
self.assertEqual(a_list[0:3], ['a', 'b', 'c']) self.assertEqual(a_list[1:3], [])
I get AssertionError
AssertionError: Lists differ: ['b', 'c'] != []
I change the expectation
self.assertEqual(a_list[1:3], ['b', 'c'])
the test is green again
I can also skip both numbers
self.assertEqual(a_list[1:3], ['b', 'c']) self.assertEqual(a_list[:], [])
the terminal shows AssertionError
AssertionError: Lists differ: ['a', 'b', 'c', 'd'] != []
I get the entire list back or a copy
self.assertEqual(a_list[:], a_list.copy())
the test is green again
This is also called slicing, I change the name of the test
def test_viewing_parts_of_a_list_aka_slicing(self): a_list = ['a', 'b', 'c', 'd'] self.assertEqual(a_list[0:2], ['a', 'b']) self.assertEqual(a_list[:2], ['a', 'b']) self.assertEqual(a_list[1:4], ['b', 'c', 'd']) self.assertEqual(a_list[1:], ['b', 'c', 'd']) self.assertEqual(a_list[0:3], ['a', 'b', 'c']) self.assertEqual(a_list[1:3], ['b', 'c']) self.assertEqual(a_list[:], a_list.copy())
test_index_error¶
IndexError is raised when I try to get an item from a list but use a number that points to something that is NOT in it. When I see this Exception I know the underlying data structure is a list
red: make it fail¶
I add a failing test
def test_viewing_parts_of_a_list_aka_slicing(self):
...
def test_index_error(self):
a_list = ['a', 'b', 'c', 'd']
a_list[4]
the terminal shows IndexError
IndexError: list index out of range
when I use an index that is the same as or greater than the length of the list I am pointing to something that is NOT in it
green: make it pass¶
I add it to the list of Exceptions encountered
# Exceptions Encountered
# AssertionError
# TypeError
# ValueError
# IndexError
I add assertRaises
def test_index_error(self):
a_list = ['a', 'b', 'c', 'd']
with self.assertRaises(IndexError):
a_list[4]
the test passes
refactor: make it better¶
the same thing happens when I use a negative number that is lower than the length of the list as a negative number
with self.assertRaises(IndexError): a_list[4] a_list[-5]
the terminal shows IndexError
IndexError: list index out of range
I add another assertRaises
with self.assertRaises(IndexError): a_list[4] with self.assertRaises(IndexError): a_list[-5]
the test passes
IndexError is also raised when I call the pop method with an empty list
with self.assertRaises(IndexError): a_list[-5] [].pop()
the terminal shows IndexError
IndexError: pop from empty list
I add assertRaises
with self.assertRaises(IndexError): a_list[-5] with self.assertRaises(IndexError): [].pop()
the terminal shows green. I cannot remove the last item from a list that has no items, this is like trying to get an item from a list that has no items
with self.assertRaises(IndexError): [].pop() [][-1]
the terminal shows IndexError
IndexError: list index out of range
I add assertRaises
with self.assertRaises(IndexError): [][-1]
the test passes. Any index given to an empty list will raise IndexError
review¶
I ran tests to show that I can make a list with the constructor or square brackets ([]
), then tested the different methods from append to sort, added tests for getting items of a list, setting items in a list, slicing a list and the IndexError
Would you like to test list comprehensions?