有什么方法可以检查Python unittest assert,如果iterable不为空? [英] Is there any way to check with Python unittest assert if an iterable is not empty?
问题描述
向服务提交查询后,我得到了字典/列表,我想确保它不为空.我使用的是Python 2.7.
After submitting queries to a service, I get a dictionary / a list back and I want to make sure it's not empty. I am on Python 2.7.
很惊讶,我没有看到unittest.TestCase
类实例的任何assertEmpty
方法.
I am surprised I don't see any assertEmpty
method for the unittest.TestCase
class instance.
现有的替代方案,例如:
The existing alternatives such as:
self.assertTrue(bool(d))
和
self.assertNotEqual(d,{})
和
self.assertGreater(len(d),0)
只是看起来不正确.
Python单元测试框架中是否缺少这种方法?如果是,那么断言Iterable不为空的最python方法是什么?
Is this kind of method is missing in the Python unittest framework? If yes, what would be the most pythonic way to assert that an iterable is not empty?
推荐答案
空列表/字典的评估结果为False,因此self.assertTrue(d)
完成工作.
Empty lists/dicts evaluate to False, so self.assertTrue(d)
gets the job done.
这篇关于有什么方法可以检查Python unittest assert,如果iterable不为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!