python比较两个列表,将每个故障视为独立故障而不是单个故障

时间:2014-12-15 23:06:16

标签: python list testing tdd unit-testing

在unittest中,我正在比较两个列表

class MyTest(unittest.TestCase):
    def setUp(self):
        self.list_to_check_against = ['hu','ge','li','st']

    def test_with_sub_list(self):
        #raise failure for each elenment in sublist not in list_to_check_against
        sublist = ['hu','go','le']
        #???

我该怎么办?我不会被通知sublistlist_to_check_againstsublist中的每个元素都失败,并且因为列表很大而无法看到2 out 3 failures.. 'go' , 'le' ... 元素列表的输出,我看不到什么在stdout。

示例输出..

{{1}}

2 个答案:

答案 0 :(得分:1)

class MyTest(unittest.TestCase):
    def setUp(self):
        self.list_to_check_against = ['hu','ge','li','st']

    def test_with_sub_list(self):
        #raise failure for each elenment in sublist not in list_to_check_against
        sublist = ['hu','go','le']
        self.assertSequenceEqual(self.list_to_check_against, sublist)

如果您只是想知道缺少什么,可以打印差异

print set(list_to_check_against) - set(sublist)

答案 1 :(得分:1)

def test_with_sub_list(self):
        sublist = ['hu','go','le']
        missing_elems = set(sublist)-set(self.list_to_check_against)
        assert not missing_elems, 
             "Error: %s not in list_to_check_against"%missing_elems

可能?