简体   繁体   中英

assertTrue() in pytest to assert empty lists

Is there a way to use assertTrue() or assertFalse() like a function in pytest for python unittests? I have a function which returns a list of elements. If the list is empty the test needs to fail through assertion.

Is there anything like below:

assertFalse(function_returns_list()), "the list is non empty, contains error elements"

为什么不测试列表的长度:

assert len(function_returns_list()) == 0, "the list is non empty"

You can assert list to confirm list is not empty, or assert not list to confirm list is empty:

>>> assert not []
>>> assert []
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AssertionError
>>> assert [1, 2, 3]

So in your case, you can just write down:

assert not function_returns_list()

You can read more about Truth Value Testing on python.org.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM