<div dir="ltr"><div><div><div><div><div><div><div><div><div><div><div><div>Hi,<br><br></div>I'm a n00b to python and still learning tricks of the trade.<br><br></div>I have a test framework, where I am populating the tests based on some criteria and/or the number of tests present.<br>
<br>I am making use of unittest + nose to to this. But unfortunately it is not working as expected and I don't know why.<br><br></div>Below is my code<br><br>class MyTests(unittest.TestCase):<br> def __init__(self, *args, **kwargs):<br>
print "Extending __init__ of mother class"<br> super(MyTests, self).__init__(*args, **kwargs)<br> self.ts = []<br> for f in os.listdir("example"):<br> if f.startswith("test_") and f.endswith(".py"):<br>
m = __import__('example.' + f.replace(".py", ""))<br> self.ts += util.testcases<br><br> for i, t in self.ts:<br> print i ,t # IT PROPERLY PRINTS STUFF HERE. AS EXPECTED.<br>
setattr(self, "test_%s" %i, t) # BUT THIS DOES NOT WORK AS EXPECTED<br><br> def test_one(self):<br> pass<br><br>if __name__ == '__main__':<br> unittest.main()<br><br></div>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.<br>
<br></div>I have couple of questions here.<br><br></div>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?<br>
<br></div>2. I'm not sure if setattr is working properly, because the output says<br><br>Extending __init__ of mother class<br>testcase_one <function wrapper at 0x1ab3c08><br>testcase_two <function wrapper at 0x1ab3cf8><br>
testcase_one <function wrapper at 0x1ab3c08><br>testcase_two <function wrapper at 0x1ab3cf8><br>Something <function wrapper at 0x1ab3de8><br>.<br>----------------------------------------------------------------------<br>
Ran 1 test in 0.000s<br><br>OK<br><br><br></div>Again other tests are not being executed. Only dummy is being executed.<br><br></div>3. nosetests doesn't pick any of these. It just picks up the class and executes that one test_one function.<br>
<br></div>I thought this list would have some unittest + nosetests experts. Any help is appreciated.<br><br></div>Thanks,<br></div>MS<br><div><div><div><div><div><div><div><div><div><div><div><div><br></div></div></div></div>
</div></div></div></div></div></div></div></div></div>