[TIP] Unable to setattr to self class and nosetests not picking up the things.

M S Vishwanath Bhat msvbhat at gmail.com
Mon Apr 7 06:21:57 PDT 2014


I'm a n00b to python and still learning tricks of the trade.

I have a test framework, where I am populating the tests based on some
criteria and/or the number of tests present.

I am making use of unittest + nose to to this. But unfortunately it is not
working as expected and I don't know why.

Below is my code

class MyTests(unittest.TestCase):
    def __init__(self, *args, **kwargs):
        print "Extending __init__ of mother class"
        super(MyTests, self).__init__(*args, **kwargs)
        self.ts = []
        for f in os.listdir("example"):
            if f.startswith("test_") and  f.endswith(".py"):
                m = __import__('example.' + f.replace(".py", ""))
                self.ts += util.testcases

        for i, t in self.ts:
            print i ,t    # IT PROPERLY PRINTS STUFF HERE. AS EXPECTED.
            setattr(self, "test_%s" %i, t)  # BUT THIS DOES NOT WORK AS

    def test_one(self):

if __name__ == '__main__':

testcase is a decorator which populates the things inside list. And the
list is being populated because I can see that in the print statement.

I have couple of questions here.

1. Without the test_one (which is a dummy function), The __init__ function
is not called at all. Only when I have that dummy function, __init__ is
being executed. Anybody know why?

2. I'm not sure if setattr is working properly, because the output says

Extending __init__ of mother class
testcase_one <function wrapper at 0x1ab3c08>
testcase_two <function wrapper at 0x1ab3cf8>
testcase_one <function wrapper at 0x1ab3c08>
testcase_two <function wrapper at 0x1ab3cf8>
Something <function wrapper at 0x1ab3de8>
Ran 1 test in 0.000s


Again other tests are not being executed. Only dummy is being executed.

3. nosetests doesn't pick any of these. It just picks up the class and
executes that one test_one function.

I thought this list would have some unittest + nosetests experts. Any help
is appreciated.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20140407/6348aea4/attachment.html>

More information about the testing-in-python mailing list