25

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"
Daniel Holmes
  • 1,952
  • 2
  • 17
  • 28
cool77
  • 1,086
  • 4
  • 15
  • 31

2 Answers2

36

Why not test for the length of the list:

assert len(function_returns_list()) == 0, "the list is non empty"
Anton Strogonoff
  • 32,294
  • 8
  • 53
  • 61
  • 7
    This will work, but requires more typing. PEP8 suggests that [For sequences, (strings, lists, tuples), use the fact that empty sequences are false](https://www.python.org/dev/peps/pep-0008/#programming-recommendations). – sashk Jun 03 '16 at 01:36
  • 8
    @sashk PEP20 says “Explicit is better than implicit”. Deflected! Although you have a good point – Anton Strogonoff Jun 03 '16 at 13:34
  • I'm in for the “Explicit is better than implicit”. But, why not use `assert val == []`? – Muhammad Yasirroni Mar 08 '23 at 08:06
  • @MuhammadYasirroni to me it is ambiguous. Does `==` mean “same as”? Is an empty list the same as another empty list, or is every empty list different? How does _my_ Python version handle that semantic? In the end I prefer to be specific about what I *need* to know: if I need to know whether my list has any items, comparing its length to zero seems to be the least ambiguous way as far as I’m concerned. – Anton Strogonoff Mar 13 '23 at 11:54
  • To be clear, you are technically correct that it would work in Python when comparing two empty lists. However, there are very popular languages that would treat `[] == []` as false (like JavaScript), and even in Python there are special cases such as `float('NaN') == float('NaN')` being False. So personally if I’d be reading Python code and see an assertion like yours I would be delayed by a few seconds while I am recalling how Python works in this particular case to make sure it’s not a bug. – Anton Strogonoff Mar 13 '23 at 12:06
  • @AntonStrogonoff thanks for the insight. But, in my case, I also need to make sure that it returns empty list, not empty itterables or empty array, without checking the type. Then, it backs to what we really want to compare. Thanks anyway for clarifying. – Muhammad Yasirroni Mar 14 '23 at 12:43
  • @MuhammadYasirroni Any list is also an iterable, so “list but not iterable” is a meaningless statement (just like “list but not array” is a thing that does not exist). – Anton Strogonoff Mar 16 '23 at 03:30
  • Sorry if I'm not clear enough. What I mean is numpy.array, or even dictionary, etc. There are many data types in Python that can have zero length. Making sure that it is a list, so that other function did not break during refactor, require isinstance and len (if using your approach). Thus, in my case, using `== []` might be better. Thanks. – Muhammad Yasirroni Mar 20 '23 at 05:31
  • @MuhammadYasirroni if your requirement is to know whether you have a `numpy.array` that contains zero items, `val == []` does not achieve that either. I’d recommend `isinstance(val, numpy.ndarray) and len(val) == 0` for clarity. – Anton Strogonoff Mar 21 '23 at 14:22
  • @AntonStrogonoff my case is isinstance(val, list). Thus, val == [] seems sufficient. – Muhammad Yasirroni Mar 22 '23 at 21:03
  • @MuhammadYasirroni It seems like you weren’t actually asking but just wanted to push your opinion. The other answer recommends exactly what you wrote, and yet I wrote my answer for reasons. If you need to find out type, use `isinstance()`, if you want to know whether your list is empty then use `len()`. – Anton Strogonoff Mar 23 '23 at 11:21
  • Please be kind. I only ask once and that is why you don't use `[]`. You had answer that question and we are done. No need to drag it further. I already said thank you for your clarification and we are done. Please re-read everything if you feel that I push any opinion. Thank you anyway, we can stop here and i already got your answer of using len instead of [] is because other language treat [] == [] as false and might confuse other. We are done here. Thanks. – Muhammad Yasirroni Mar 24 '23 at 14:47
  • @MuhammadYasirroni You asked why use `len()` instead of equality check, and then started to argue why equality check with an empty list is better in your case. Again, if you are unsure as to what your function returns, you should use more complex checks than trying to shortcut it into one. The only benefit of checking against an empty list is that you have fewer characters to type, but it comes at a cost of clarity. – Anton Strogonoff Mar 26 '23 at 23:28
  • @AntonStrogonoff Sir, please. I'm not arguing and wanted to be nice here. I'm just conclude your own statement "To be clear, you are technically correct that it would work in Python when comparing two empty lists". Then, I will use `[]` as you suggested to check if it is `equal to empty list`. I'm using this for protecting my code from bad refactor. Are we done here? Or you strongly said that I should not use `== []` and use `isinstance(x, list) and len(x) == 0`? Please be kind in online, especially in Stack Overflow. – Muhammad Yasirroni Mar 27 '23 at 02:35
  • @MuhammadYasirroni The semantics of the word “technically” in English are such that they indicate it’s *not* the best way of achieving the desired effect, *even if* it works. That’s the whole point of questions like this: there are many ways of doing something that *would* work, but choosing one at random (or because it’s shorter to write) isn’t necessarily best because there are considerations like clarity, readability and so on. – Anton Strogonoff Mar 28 '23 at 04:22
28

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.

sashk
  • 3,946
  • 2
  • 33
  • 41
  • I'm having a hard time with th empty list, set, etc... Shouldn't this test pass: `assert [] is False`? According to the docs, empty sequences and `False` are both false. – nicorellius Apr 19 '17 at 04:26
  • `assert not []`, since `[]` is empty sequence and is False. – sashk Apr 21 '17 at 14:35
  • Had such a hard time Googling "Pytest equivalent of `assertFalse`"! Thank you very much. – Noumenon Oct 17 '19 at 15:38