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"
Why not test for the length of the list:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With