单元测试 - 断言列表的一组项目包含(或不包含)在另一个列表中
Unittest - Assert a set of items of a list are (or not) contained in another list
你好我是编程新手,正在尝试进行测试以检查项目列表中的 any 项目是否存在于另一个列表中(在 [=57 中使用 unittest =] 2.7).
例如,如果我有一个列表 ["dog"、"cat"、"frog] and the result of the method I am testing is ["tiger"、"lion"、"kangaroo"、"frog] I want the test to fail because it contains one of the items of the previous list ("青蛙”)。我还希望测试能告诉我两个列表都有哪些词(即哪些词导致测试失败)。
我试过:
self.assertIn(["cat", "dog"], method("cat dog tiger"))
该方法的结果是 ["cat"、"dog"、"tiger"] 但测试的结果是失败并表示:
AssertionError: ['cat', 'dog'] not found in ['cat', 'dog', 'tiger']
我希望此测试 return 正常,因为 'cat' 和 'dog' 出现在第二个列表中。似乎 assertIn 没有按照我的想法去做(我认为这是检查 a 中是否存在任何 b)。
反之亦然,assertNotIn 在我希望它失败时通过。
我已经搜索了一段时间,但是因为我不确定我在找什么,所以很难找到。
感谢您的阅读,希望您能理解。
编辑:我已经采用了 Chris 的解决方案,它可以正常工作:
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'])
self.assertTrue(any(animal in method("cat dog tiger") for animal in ("cat", "dog")))
你应该看看 this question,然后你可以很容易地看到像这样的东西:
def myComp(list1, list2):
section = list(set(list1).intersection(list2))
return ((len(section)==0), section)
此函数将 return 一个元组,其中包含指示失败或成功的布尔值以及出现在两个列表中的项目列表。
如果你真的想在断言语句中这样做,你可以只使用该元组的第一个元素...
您可以遍历您的列表和 assertIn
,或者使用 set
s 并且您可以执行类似 self.assertTrue(set(a).issuperset(set(b)))
.
的操作
如果您希望序列中的值不可重复,也许最好考虑使用集合,因为您可以轻松地检查它们是否存在任何类型的重叠。
>>> a, b = {'dog', 'cat'}, {'dog', 'cat', 'wolf', 'crab'}
>>> a & b
set(['dog', 'cat'])
>>> a ^ b
set(['wolf', 'crab'])
所以检查 a 是 b 的子集会是这样的:
>>> not bool(a ^ b & a)
True
等等
你好我是编程新手,正在尝试进行测试以检查项目列表中的 any 项目是否存在于另一个列表中(在 [=57 中使用 unittest =] 2.7).
例如,如果我有一个列表 ["dog"、"cat"、"frog] and the result of the method I am testing is ["tiger"、"lion"、"kangaroo"、"frog] I want the test to fail because it contains one of the items of the previous list ("青蛙”)。我还希望测试能告诉我两个列表都有哪些词(即哪些词导致测试失败)。
我试过:
self.assertIn(["cat", "dog"], method("cat dog tiger"))
该方法的结果是 ["cat"、"dog"、"tiger"] 但测试的结果是失败并表示:
AssertionError: ['cat', 'dog'] not found in ['cat', 'dog', 'tiger']
我希望此测试 return 正常,因为 'cat' 和 'dog' 出现在第二个列表中。似乎 assertIn 没有按照我的想法去做(我认为这是检查 a 中是否存在任何 b)。
反之亦然,assertNotIn 在我希望它失败时通过。
我已经搜索了一段时间,但是因为我不确定我在找什么,所以很难找到。
感谢您的阅读,希望您能理解。
编辑:我已经采用了 Chris 的解决方案,它可以正常工作:
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'])
self.assertTrue(any(animal in method("cat dog tiger") for animal in ("cat", "dog")))
你应该看看 this question,然后你可以很容易地看到像这样的东西:
def myComp(list1, list2):
section = list(set(list1).intersection(list2))
return ((len(section)==0), section)
此函数将 return 一个元组,其中包含指示失败或成功的布尔值以及出现在两个列表中的项目列表。
如果你真的想在断言语句中这样做,你可以只使用该元组的第一个元素...
您可以遍历您的列表和 assertIn
,或者使用 set
s 并且您可以执行类似 self.assertTrue(set(a).issuperset(set(b)))
.
如果您希望序列中的值不可重复,也许最好考虑使用集合,因为您可以轻松地检查它们是否存在任何类型的重叠。
>>> a, b = {'dog', 'cat'}, {'dog', 'cat', 'wolf', 'crab'}
>>> a & b
set(['dog', 'cat'])
>>> a ^ b
set(['wolf', 'crab'])
所以检查 a 是 b 的子集会是这样的:
>>> not bool(a ^ b & a)
True
等等