<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"><<a href="mailto:alfredo@deza.pe" target="_blank">alfredo@deza.pe</a>></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 <<a href="mailto:msvbhat@gmail.com">msvbhat@gmail.com</a>> wrote:<br>
<br>
> Hi,<br>
><br>
> I'm a n00b to python and still learning tricks of the trade.<br>
><br>
> 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>
> 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>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>
><br>
> 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>
> 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’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> global testcases<br> def decorator(func):<br> def wrapper(self):<br> ret = func(self)<br> self.assertTrue(ret)<br>
return ret<br> testcases.append((name, wrapper))<br> return wrapper<br> <br> return decorator<br><br></div><div>MS<br></div><div> <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>
><br>
> I have couple of questions here.<br>
><br>
> 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>
> 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>
> Again other tests are not being executed. Only dummy is being executed.<br>
><br>
> 3. nosetests doesn't pick any of these. It just picks up the class and executes that one test_one function.<br>
><br>
> I thought this list would have some unittest + nosetests experts. Any help is appreciated.<br>
><br>
> Thanks,<br>
> MS<br>
><br>
</div>> _______________________________________________<br>
> testing-in-python mailing list<br>
> <a href="mailto:testing-in-python@lists.idyll.org">testing-in-python@lists.idyll.org</a><br>
> <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>