有什么方法可以检查Python unittest assert,如果iterable不为空? [英] Is there any way to check with Python unittest assert if an iterable is not empty?

查看:124
本文介绍了有什么方法可以检查Python unittest assert,如果iterable不为空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

向服务提交查询后,我得到了字典/列表,我想确保它不为空.我使用的是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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆