修改前代码:
请详见《使用python实现链表》
进行单元测试代码:
import unittest
from unittest.mock import patch
from test import LinkedList, Node
link = LinkedList()
data = ['张三', '李四', '王二']
link.init_link(data)
class TestLinkedList(unittest.TestCase):
def test01_init(self):
num = link.num
self.assertEqual(len(data), num) # 断言两个参数的值相等
head_node = link.head
self.assertTrue(head_node.data == '张三') # 断言头节点的数据域的值为张三
def test02_append(self):
with patch('builtins.print') as mocked_print:
link.append('秦国宇')
mocked_print.assert_called_with('操作成功!') # 比对函数最后返回的打印数据
link.append('12123')
mocked_print.assert_called_with('操作成功!')
def test03_remove(self):
with patch('builtins.print') as mocked_print:
link.remove('秦国宇')
mocked_print.assert_called_with('操作成功!')
link.remove('埃达尼斯')
mocked_print.assert_called_with('链表中不存在的对象!')
link.remove('10')
mocked_print.assert_called_with('链表中不存在的对象!')
link.remove('0')
mocked_print.assert_called_with('操作成功!')
def test04_insert(self):
with patch('builtins.print') as mocked_print:
link.insert('赵六', 1)
mocked_print.assert_called_with('操作成功!')
link.insert('正式开课了', 100)
mocked_print.assert_called_with('链表中不存在的对象!')
def test05_change(self):
with patch(