How to properly unit-test a linked list (using Python)? -


i'm new tdd. i've created main functions (insert, search, remove etc.). insert_beginning() function:

def insert_beginning(self, node):     '''     inserts node beginning of list.     '''     node.set_next(self.head)     self.head = node 

my question is, how unit-test function? way 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 test_insert_beginning() function dependent on my

__str__() # string representation of linked list 

function. have feeling way i'm testing not correct (because moment decide change way linked list represented, i'd end having rewrite test cases). there way test insert_beginning() function without depending on function created / customized?

edit: wondering, currently, string representation of linked list string representation of nodes separated commas. example, linked list nodes 1, 2 , "a" represented this:

1, 2, "a" 

however, planning on changing linked list's string representation (planning on changing __ str __() function). when realized way unit testing might incorrect.

edit 2 (a comment suggested create function helps me find index of item in linked list): assume function called index_of(self, item) exists, , function finds index of item. assume unit test index_of(self, item), , assume test cases 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, okay me rely on index_of(self, item) function determine if insert_beginning(self, node) working correctly? in other words, okay if 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) 

you have point. don't want tie arbitrary string representation of list, change. "proper" way check check values in list. i.e., instead of checking string representation, should check post conditions seperatly:

  • the first value 1
  • the previous value doesn't exist (does whatever want happen on error)
  • the next value same previous value
  • the size 1
  • ...

    of course methods support have have separate unit tests.


Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -