有没有办法像python单元测试pytest中的函数一样使用assertTrue()或assertFalse().我有一个函数,如果返回元素列表,测试需要通过断言失败.
有下面的东西:
assertFalse(function_returns_list()), "the list is non empty, contains error elements"
Run Code Online (Sandbox Code Playgroud)
Ant*_*off 14
为什么不测试列表的长度:
assert len(function_returns_list()) == 0, "the list is non empty"
Run Code Online (Sandbox Code Playgroud)
您可以assert list确认列表不为空,或assert not list确认列表为空:
>>> assert not []
>>> assert []
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AssertionError
>>> assert [1, 2, 3]
Run Code Online (Sandbox Code Playgroud)
所以在你的情况下,你可以写下来:
assert not function_returns_list()
Run Code Online (Sandbox Code Playgroud)
您可以在python.org上阅读有关真值测试的更多信息.