<div dir="ltr"><br><div class="gmail_extra"><br><br><div class="gmail_quote">On 7 April 2014 19:14, Alfredo Deza <span dir="ltr">&lt;<a href="mailto:alfredo@deza.pe" target="_blank">alfredo@deza.pe</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div class=""><br>
On Apr 7, 2014, at 9:21 AM, M S Vishwanath Bhat &lt;<a href="mailto:msvbhat@gmail.com">msvbhat@gmail.com</a>&gt; wrote:<br>
<br>
&gt; Hi,<br>
&gt;<br>
&gt; I&#39;m a n00b to python and still learning tricks of the trade.<br>
&gt;<br>
&gt; I have a test framework, where I am populating the tests based on some criteria and/or the number of tests present.<br>
<br>
</div>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<br>
without the need to alter the collection process directly in the class.<br>
<div class=""><br>
<br>
&gt; 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>You should not use __init__ here, test setup is usually done in the setUp method when using unittest.TestCase. You should try using<br>
that instead and see if you get any further.<br>
<div class=""><br>
&gt;<br>
&gt; Below is my code<br>
&gt;<br>
&gt; class MyTests(unittest.TestCase):<br>
&gt; &nbsp; &nbsp; def __init__(self, *args, **kwargs):<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; print &quot;Extending __init__ of mother class&quot;<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; super(MyTests, self).__init__(*args, **kwargs)<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; self.ts = []<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; for f in os.listdir(&quot;example&quot;):<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if f.startswith(&quot;test_&quot;) and &nbsp;f.endswith(&quot;.py&quot;):<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; m = __import__(&#39;example.&#39; + f.replace(&quot;.py&quot;, &quot;&quot;))<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.ts += util.testcases<br>
&gt;<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; for i, t in self.ts:<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print i ,t &nbsp; &nbsp;# IT PROPERLY PRINTS STUFF HERE. AS EXPECTED.<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setattr(self, &quot;test_%s&quot; %i, t) &nbsp;# BUT THIS DOES NOT WORK AS EXPECTED<br>
&gt;<br>
&gt; &nbsp; &nbsp; def test_one(self):<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; pass<br>
&gt;<br>
&gt; if __name__ == &#39;__main__&#39;:<br>
&gt; &nbsp; &nbsp; unittest.main()<br>
&gt;<br>
&gt; 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>You are setting attributes on a method, but I don&rsquo;t think that will work on a method. If you are trying to set some values for a test to use<br>
then you might want to set them in the setUp method.<br></blockquote><div>Thanks Alfred for replying.<br></div><div><br>I thought I was setting it on the class. How do I set it on the class then?<br><br></div><div>And how do I make setup and teardown to run only once for entire test suite? <br>
</div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
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.<br></blockquote><div>When I import them, decorator populate them inside a list which. And I use them to do setattr.<br>
<br></div><div>This is my decorator function.<br>def testcase(name):<br>&nbsp;&nbsp;&nbsp; global testcases<br>&nbsp;&nbsp;&nbsp; def decorator(func):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; def wrapper(self):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ret = func(self)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.assertTrue(ret)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return ret<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; testcases.append((name, wrapper))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return wrapper<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; return decorator<br><br></div><div>MS<br></div><div>&nbsp;<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">

<div class=""><br>
&gt;<br>
&gt; I have couple of questions here.<br>
&gt;<br>
&gt; 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>
&gt;<br>
&gt; 2. I&#39;m not sure if setattr is working properly, because the output says<br>
&gt;<br>
&gt; Extending __init__ of mother class<br>
&gt; testcase_one &lt;function wrapper at 0x1ab3c08&gt;<br>
&gt; testcase_two &lt;function wrapper at 0x1ab3cf8&gt;<br>
&gt; testcase_one &lt;function wrapper at 0x1ab3c08&gt;<br>
&gt; testcase_two &lt;function wrapper at 0x1ab3cf8&gt;<br>
&gt; Something &lt;function wrapper at 0x1ab3de8&gt;<br>
&gt; .<br>
&gt; ----------------------------------------------------------------------<br>
&gt; Ran 1 test in 0.000s<br>
&gt;<br>
&gt; OK<br>
&gt;<br>
&gt;<br>
&gt; Again other tests are not being executed. Only dummy is being executed.<br>
&gt;<br>
&gt; 3. nosetests doesn&#39;t pick any of these. It just picks up the class and executes that one test_one function.<br>
&gt;<br>
&gt; I thought this list would have some unittest + nosetests experts. Any help is appreciated.<br>
&gt;<br>
&gt; Thanks,<br>
&gt; MS<br>
&gt;<br>
</div>&gt; _______________________________________________<br>
&gt; testing-in-python mailing list<br>
&gt; <a href="mailto:testing-in-python@lists.idyll.org">testing-in-python@lists.idyll.org</a><br>
&gt; <a href="http://lists.idyll.org/listinfo/testing-in-python" target="_blank">http://lists.idyll.org/listinfo/testing-in-python</a><br>
<br>
</blockquote></div><br></div></div>