python功能测试

功能函数:

文件名removeSame.py

def remove_shared(L1, L2):
    """ (list list)

    Remove items from L1 that are in both L1 and L2.

    >>> list_1 = [1, 2, 3, 4, 5, 6]
    >>> list_2 = [2, 4, 5, 7]
    >>> remove_shared(list_1, list_2)
    >>> list_1
    [1, 3, 6]
    >>> list_2
    [2, 4, 5, 7]
    """

    for v in L2:
        if v in L1:
            L1.remove(v)


if __name__ == '__main__':
    import doctest
    doctest.testmod()

测试函数:

文件名test_removeSame.py

import unittest
import removeSame


class TestRemoveShared(unittest.TestCase):    #要以unittest.TestCase作为基类
    """Tests for function duplicates.remove_shared."""

    def test_general_case(self):          
        """
Test remove_shared where there are items that
	appear in both lists, and items that appear in        
	only one or the other list.        """        
	list_1 = [1, 2, 3, 4, 5, 6]        
	list_2 = [2, 4, 5, 7]        
	list_1_expected = [1, 3, 6]        
	list_2_expected = [2, 4, 5, 7]        
	removeSame.remove_shared(list_1, list_2)        
	self.assertEqual(list_1, list_1_expected)        
	self.assertEqual(list_2, list_2_expected)
    def test_general_case2(self):        
        """
        Test remove_shared where there are items that
        appear in both lists, and items that appear in
        only one or the other list.
        """

        list_1 = [1, 2, 3, 4, 5, 6]
        list_2 = [2, 4, 5, 7]
        list_1_expected = [1, 3, 6]
        list_2_expected = [2, 4, 5, 7]

        removeSame.remove_shared(list_1, list_2)

        self.assertEqual(list_1, list_1_expected)
        self.assertEqual(list_2, list_2_expected)
if __name__ == '__main__':
	unittest.main(exit=False)   
 




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值