pytest 中的 assertTrue() 断言空列表
assertTrue() in pytest to assert empty lists
有没有办法像 pytest 中的函数一样使用 assertTrue()
或 assertFalse()
进行 python 单元测试?
我有一个 returns 元素列表的函数。如果列表为空,则测试需要通过断言失败。
有没有类似下面的:
assertFalse(function_returns_list()), "the list is non empty, contains error elements"
为什么不测试列表的长度:
assert len(function_returns_list()) == 0, "the list is non empty"
您可以assert list
确认列表不为空,或assert not list
确认列表为空:
>>> assert not []
>>> assert []
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AssertionError
>>> assert [1, 2, 3]
所以在你的情况下,你可以写下:
assert not function_returns_list()
您可以在 python.org 上阅读有关 Truth Value Testing 的更多信息。
有没有办法像 pytest 中的函数一样使用 assertTrue()
或 assertFalse()
进行 python 单元测试?
我有一个 returns 元素列表的函数。如果列表为空,则测试需要通过断言失败。
有没有类似下面的:
assertFalse(function_returns_list()), "the list is non empty, contains error elements"
为什么不测试列表的长度:
assert len(function_returns_list()) == 0, "the list is non empty"
您可以assert list
确认列表不为空,或assert not list
确认列表为空:
>>> assert not []
>>> assert []
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AssertionError
>>> assert [1, 2, 3]
所以在你的情况下,你可以写下:
assert not function_returns_list()
您可以在 python.org 上阅读有关 Truth Value Testing 的更多信息。