I'm new to TDD. I've created all the main functions (insert, search, remove etc.). This is my insert_beginning() function:
def insert_beginning(self, node):
'''
Inserts a Node to the beginning of the list.
'''
node.set_next(self.head)
self.head = node
My question is, how do I properly unit-test this function? The only way I can thing of is:
class ListFunctionsTest(unittest.TestCase):
def setUp(self):
self.list1 = LinkedList()
self.node1 = Node(1)
def test_insert_beginning(self):
self.list1.insert_beginning(self.node1)
self.assertEqual(self.list1.__str__(), "1")
but then the test_insert_beginning() function is dependent on my
__str__() # string representation of my linked list
function. I have a feeling that the way I'm testing it is not correct (because the moment I decide to change the way my linked list is represented, then I'd end up having to rewrite my test cases). Is there a way to test my insert_beginning() function without depending on another function which I created / customized?
Edit: For those wondering, currently, the string representation of my linked list is just a string representation of my nodes separated by commas. For example, a linked list with the nodes 1, 2 and "a" would be represented like this:
1, 2, "a"
However, I am planning on changing my linked list's string representation (planning on changing the __ str __() function). This is when I realized that the way I am unit testing might be incorrect.
Edit 2 (a comment suggested that I create a function which helps me find the index of an item in the linked list): Assume that a function called index_of(self, item) exists, and this function finds the index of an item. Assume that this is the unit test for the index_of(self, item), and assume that the test cases successfully pass.
def test_index_of(self):
myList = LinkedList()
node1 = Node(1)
node2 = Node(2)
node3 = Node(3)
myList.head = node1
node1.next = node2
node2.next = node3
self.assertEqual(self.myList.index_of(1), 0)
self.assertEqual(self.myList.index_of(2), 1)
self.assertEqual(self.myList.index_of(3), 2)
Now, is it okay for me to rely on the index_of(self, item) function to determine if insert_beginning(self, node) is working correctly? In other words, is it okay if my test_insert_beginning() function is:
class ListFunctionsTest(unittest.TestCase):
def setUp(self):
self.list1 = LinkedList()
self.node1 = Node(1)
def test_insert_beginning(self):
self.list1.insert_beginning(self.node1)
self.assertEqual(self.list1.index_of(1), 0)
Nodes rather than working with the values themselves. A well encapsulated linked list will not make the caller think about nodes. Callers don't care about the nodes; they care about the values. Nodes are an implementation detail.def insert_beginning(self, node):, which clearly takes a node as an argument. Creating theNodeobject is something the calling code should not have to do; it's an implementation detail of the list. The method should bedef insert_beginning(self, element):, and wrapping the element in a node would be internal to the method.