使用unit test单元测试工具测试使用python实现的链表

本文介绍了如何使用Python实现链表,并通过单元测试工具对链表功能进行验证。通过单元测试,代码得到了改进和完善。
摘要由CSDN通过智能技术生成

修改前代码:
请详见《使用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(
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值