如果可迭代对象不为空,有什么方法可以检查 Python unittest assert 吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33216488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 13:01:31  来源:igfitidea点击:

Is there any way to check with Python unittest assert if an iterable is not empty?

pythonpython-2.7unit-testingassertionspython-unittest

提问by Alex Tereshenkov

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.

向服务提交查询后,我得到一个字典/一个列表,我想确保它不是空的。我使用的是 Python 2.7。

I am surprised I don't see any assertEmptymethod for the unittest.TestCaseclass instance.

我很惊讶我没有看到类实例的任何assertEmpty方法unittest.TestCase

The existing alternatives such as:

现有的替代方案,例如:

self.assertTrue(bool(d))

and

self.assertNotEqual(d,{})

and

self.assertGreater(len(d),0)

just don't look right.

只是看起来不对。

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?

Python unittest 框架中是否缺少这种方法?如果是,那么断言可迭代对象不为空的最pythonic 方法是什么?

采纳答案by gplayer

Empty lists/dicts evaluate to False, so self.assertTrue(d)gets the job done.

空列表/字典评估为 False,因此self.assertTrue(d)完成工作。

回答by Eugene Soldatov

Maybe:

也许:

self.assertRaises(StopIteration, next(iterable_object))

回答by Josh J

Depends exactly what you are looking for.

取决于你正在寻找什么。

If you want to make sure the object is an iterable and it is not empty:

如果要确保对象是可迭代的并且不为空:

# TypeError: object of type 'NoneType' has no len()
# if my_iterable is None
self.assertTrue(len(my_iterable))

If it is OK for the object being tested to be None:

如果被测试的对象可以None

self.assertTrue(my_maybe_iterable)

回答by winklerrr

As @gplayer already mentioned: it's possible to check for non emptiness with assertTrue()(and vice versa with assertFalse()for emptiness of course).

正如@gplayer 已经提到的:可以检查非空assertTrue()assertFalse()当然,反之亦然)。

But, like @Alex Tereshenkov previously has pointed out in the comments, all those assertTrue()and assertFalse()method calls clutter the code and are kind of misleading because we wanted to check for emptiness.

但是,就像@Alex Tereshenkov 之前在评论中指出的那样,所有这些assertTrue()assertFalse()方法调用都会使代码变得混乱并且有点误导,因为我们想要检查是否为空。

So in the sake of cleaner code we can define our own assertEmpty()and assertNotEmpty()methods:

因此,在清洁代码起见,我们可以定义我们自己assertEmpty()assertNotEmpty()方法:

def assertEmpty(self, obj):
    self.assertFalse(obj)

def assertNotEmpty(self, obj):
    self.assertTrue(obj)

回答by Rick Graves

All the credit for this goes to winklerrr, I am just extending his idea: have importable mixins for when you need assertEmpty or assertNotEmpty:

这一切都归功于 winklerrr,我只是扩展了他的想法:在需要 assertEmpty 或 assertNotEmpty 时使用可导入的 mixin:

class AssertEmptyMixin( object ):
    def assertEmpty(self, obj):
        self.assertFalse(obj)

class AssertNotEmptyMixin( object ):
    def assertNotEmpty(self, obj):
        self.assertTrue(obj)

Caveat, mixins should go on the left:

注意,mixin 应该放在左边:

class MyThoroughTests( AssertNotEmptyMixin, TestCase ):
    def test_my_code( self ):
        ...
        self.assertNotEmpty( something )