<div dir="ltr"><div><div><div><div><div><div><div><div><div><div><div><div>Hi,<br><br></div>I&#39;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&#39;t know why.<br><br></div>Below is my code<br><br>class MyTests(unittest.TestCase):<br>    def __init__(self, *args, **kwargs):<br>
        print &quot;Extending __init__ of mother class&quot;<br>        super(MyTests, self).__init__(*args, **kwargs)<br>        self.ts = []<br>        for f in os.listdir(&quot;example&quot;):<br>            if f.startswith(&quot;test_&quot;) and  f.endswith(&quot;.py&quot;):<br>
                m = __import__(&#39;example.&#39; + f.replace(&quot;.py&quot;, &quot;&quot;))<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, &quot;test_%s&quot; %i, t)  # BUT THIS DOES NOT WORK AS EXPECTED<br><br>    def test_one(self):<br>        pass<br><br>if __name__ == &#39;__main__&#39;:<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&#39;m not sure if setattr is working properly, because the output says<br><br>Extending __init__ of mother class<br>testcase_one &lt;function wrapper at 0x1ab3c08&gt;<br>testcase_two &lt;function wrapper at 0x1ab3cf8&gt;<br>
testcase_one &lt;function wrapper at 0x1ab3c08&gt;<br>testcase_two &lt;function wrapper at 0x1ab3cf8&gt;<br>Something &lt;function wrapper at 0x1ab3de8&gt;<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&#39;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>