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

Alfredo Deza alfredo at deza.pe
Mon Apr 7 06:44:38 PDT 2014


On Apr 7, 2014, at 9:21 AM, M S Vishwanath Bhat <msvbhat at gmail.com> wrote:

> Hi,
> 
> 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.

But that is something that is usually taken care of for you by the test framework (Nose in your case). You should be able to do that
without the need to alter the collection process directly in the class.


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

You should not use __init__ here, test setup is usually done in the setUp method when using unittest.TestCase. You should try using
that instead and see if you get any further.

> 
> 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 EXPECTED
> 
>     def test_one(self):
>         pass
> 
> if __name__ == '__main__':
>     unittest.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. 

You are setting attributes on a method, but I don’t think that will work on a method. If you are trying to set some values for a test to use
then you might want to set them in the setUp method.

But I am still not sure why are you importing modules there, cannot see what util.testcases is, and not sure where is the decorator.

> 
> 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
> 
> OK
> 
> 
> 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.
> 
> Thanks,
> MS
> 
> _______________________________________________
> testing-in-python mailing list
> testing-in-python at lists.idyll.org
> http://lists.idyll.org/listinfo/testing-in-python




More information about the testing-in-python mailing list