你好,我是新来的编程和,并试图进行测试,检查中的项目列表中的任何物品是否存在于另一个列表(在Python 2.7中使用unittest)。单元测试 - 断言一组列表项的(或不)包含在另一个列表
例如,如果我有一个列表[“狗”,“猫”,“青蛙”和我测试的方法的结果是[“老虎”,“狮子”,“袋鼠”,“青蛙]我希望测试失败,因为它包含上一个列表(“青蛙”)的项目之一。我还希望测试能够告诉我这两个列表都有哪些单词(即,哪些单词导致测试失败)。
我曾尝试:
self.assertIn(["cat", "dog"], method("cat dog tiger"))
方法的结果是[“猫”,“狗”,“虎”]但测试的结果为失败,并说:
AssertionError: ['cat', 'dog'] not found in ['cat', 'dog', 'tiger']
我想这个测试返回OK,因为'猫'和'狗'出现在第二个列表中。它似乎断言在我不认为它会做(我认为这是检查a是否存在b)。
反之亦然,当我希望它失败时assertNotIn通过。
我一直在寻找一段时间,但因为我不知道我在找什么使它很难找到。
谢谢你的阅读,我希望这是有道理的。
编辑:我已经与克里斯的解决方案和它的作品,因为我想:
def myComp(list1, list2):
section = list(set(list1).intersection(list2))
为了获得重叠词的列表(即触发失败)的错误信息,我添加了下面的代码从这里How to change the message in a Python AssertionError?:
try:
assert(len(section)==0)
except AssertionError as e:
e.args += ('The following are present in the processed text',
section)
raise
结果正是我想要的东西:
AssertionError: ('The following are pressent in the processed text', ['dog',
'cat'])
列表'[ '猫', '狗']'是没有在列表中找到'['cat','dog','tiger']''在['a']'中尝试print ['a'] - 这是错误的,因为列表不在列表中,并没有做一个元素明智的比较。 – Stael