close

How to assert that an iterable is not empty on Unittest?

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to assert that an iterable is not empty on Unittest in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How to assert that an iterable is not empty on Unittest?

  1. How to assert that an iterable is not empty on Unittest?

    Depends exactly what you are looking for.
    If you want to make sure the object is an iterable and it is not empty:

  2. assert that an iterable is not empty on Unittest

    Depends exactly what you are looking for.
    If you want to make sure the object is an iterable and it is not empty:

Method 1

Empty lists/dicts evaluate to False, so self.assertTrue(d) gets the job done.

Method 2

Depends exactly what you are looking for.

If you want to make sure the object is an iterable and it is not empty:

# TypeError: object of type 'NoneType' has no len()
# if my_iterable is None
self.assertTrue(len(my_iterable))

If it is OK for the object being tested to be None:

self.assertTrue(my_maybe_iterable)

Conclusion

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read