Saš*_*aba 4 python python-2.7 python-unittest
对于Python 2.7:
list1 = [1, 2]
self.assertIn(1, list1)
self.assertIn(2, list1)
Run Code Online (Sandbox Code Playgroud)
有没有办法让我更轻松?就像是:
self.assertIn((1,2), list1) # I know this is wrong, just an example
Run Code Online (Sandbox Code Playgroud)
尝试
self.assertTrue(all(x in list1 for x in [1,2]))
Run Code Online (Sandbox Code Playgroud)
如果你可以使用pytest-django,你可以使用本机断言语句:
assert all(x in in list1 for x in [1,2])
Run Code Online (Sandbox Code Playgroud)
我确定你已经弄明白但是你可以使用一个循环:
tested_list = [1, 2, 3]
checked_list = [1, 2]
# check that every number in checked_list is in tested_list:
for num in checked_list:
self.assertIn(num, tested_list)
Run Code Online (Sandbox Code Playgroud)
这对于缺少的特定数字失败,tested_list因此您可以立即知道问题所在.
| 归档时间: |
|
| 查看次数: |
1565 次 |
| 最近记录: |