如何断言 Unittest 上的可迭代对象不为空? [英] How to assert that an iterable is not empty on Unittest?

查看:41
本文介绍了如何断言 Unittest 上的可迭代对象不为空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

向服务提交查询后,我会返回一个字典或一个列表,我想确保它不是空的.我使用 Python 2.7.

After submitting queries to a service, I get a dictionary or a list back and I want to make sure it's not empty. I using Python 2.7.

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

I am surprised of not having any assertEmpty method for the unittest.TestCase class instance.

现有的替代方案看起来不太对:

The existing alternatives just don't look right:

self.assertTrue(bool(d))
self.assertNotEqual(d,{})
self.assertGreater(len(d),0)

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

Is this kind of a missing method in the Python unittest framework? If yes, what would be the most pythonic way to assert that an iterable is not empty?

推荐答案

空列表/dicts 评估为 False,所以 self.assertTrue(d) 完成工作.

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

这篇关于如何断言 Unittest 上的可迭代对象不为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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