I have programmed a small iterator in Python:

class anything():
    def __init__(self):

    def __iter__(self):
        return self

    def next(self):
        if self.i>100:
            raise StopIteration
        return self.i

and I would like to use unittest of Python to check if some value on the generated list is even (I am just doing it for learning testing unit in Python). So I would like to have something like:

def main()
   for x in anything():
        assert x%2==0

but I just do not know how to accomplish that. I have read some material online, but there are no examples that teach how to manage the unit test within classes.

  • So you already read the Python docs about the unittest module, I guess? (docs.python.org/2/library/unittest.html) Tell us what you exactly is unclear for you in those example, then we might be able to help you.
    – Doc Brown
    Commented Nov 20, 2014 at 11:32

1 Answer 1


Below is an example of a test. BTW, you have a bug in your class. Probably self.i=self.i+i should be self.i=self.i+1

#!/usr/bin/env python
# -*- Python -*-
"Unit test for Columnize"
import unittest
from it import anything

class TestI(unittest.TestCase):

    def test_basic(self):
        for x in anything():
            if x % 2 == 0:
                self.assertTrue(True, "Found value %d" % x)
        self.assertTrue(False, "No even found in Anything")

if __name__ == '__main__':

Not the answer you're looking for? Browse other questions tagged or ask your own question.