python功能测试

来源:互联网 发布:skype mac 旧版本 编辑:程序博客网 时间:2024/06/01 09:59

功能函数:

文件名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 unittestimport removeSameclass 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)