<br><br><div class="gmail_quote">On Wed, Jan 20, 2010 at 16:38, Mark Roddy <span dir="ltr"><<a href="mailto:markroddy@gmail.com">markroddy@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">

Earlier this week on the Testing In Python list, there was a<br>
discussion on how to execute a setup and/or teardown for a single test<br>
class instead of for each test fixture on the class (see the 'setUp<br>
and tearDown behavior' thread).  I have had to deal with situation<br>
myself before, and I am obviously not the only one (since I did not<br>
initiate the thread).  As such I'd like to propose adding a class<br>
level setup and tear down method the unittest TestCase class.<br>
<br></blockquote><div><br></div><div>Maybe I'm missing something, but why can't you simply use __init__ and __del__ for this purpose?</div><div><br></div><div>-Brett</div><div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">


Rationale:<br>
Test cases can at times require the setup of expensive resources.<br>
This is often the case when implementing integration testing.  Having<br>
to perform this setup for each fixture can prohibited for large number<br>
of fixtures and/or for resources that are expensive to setup.  For<br>
example, I have several hundred integration tests that hit a live<br>
database.  If I create the connection object for each fixture, most of<br>
the tests fail due to the maximum connection limit being reached.  As<br>
a work around, I create a connection object once for each test case.<br>
Without such functionality built in, the common idiom runs along these<br>
lines:<br>
<br>
class MyTest(TestCase):<br>
<br>
    def setUp(self):<br>
        if not self.ClassIsSetup:<br>
            self.setupClass()<br>
            self.ClassIsSetup=True<br>
<br>
While this achieves the desired functionality, it is unclear due to<br>
conditional setup code and is also error prone as the same code<br>
segment would need to appear in every TestCase requiring the<br>
functionality.  Having a class wide setup and teardown function that<br>
implementers of test cases can override would make the code/intent<br>
more clear and alleviate the need to implement test case functionality<br>
when the user should be focusing on writing tests.<br>
<br>
I emailed Michael Foord about some of his comments in the TIP thread<br>
and to ask if he would be interested in a patch adding this<br>
functionality, and I have included his response below.  I would like<br>
to hear people's comments/suggestions/ideas before I start working on<br>
said patch.<br>
<br>
Thanks,<br>
-Mark<br>
<br>
<br>
Michael Foord's Email:<br>
=======================================<br>
I would certainly be interested in adding this to unittest.<br>
<br>
It needs a discussion of the API and the semantics:<br>
<br>
* What should the methods be called? setup_class and teardown_class or<br>
setupClass and teardownClass? For consistency with existing methods<br>
the camelCase should probably be used.<br>
* If the setupClass fails how should the error be reported? The<br>
*easiest* way is for the failure to be reported as part of the first<br>
test<br>
* Ditto for teardownClass - again the easiest way is for it to be<br>
reported as a failure in the last test<br>
* If setupClass fails then should all the tests in that class be<br>
skipped? I think yes.<br>
<br>
Also details like ensuring that even if just a single test method from<br>
a class is run the setupClass and teardownClass are still run. It<br>
probably needs to go to python-dev or python-ideas for discussion.<br>
<br>
All the best,<br>
<br>
Michael<br>
_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a><br>
<a href="http://mail.python.org/mailman/listinfo/python-ideas" target="_blank">http://mail.python.org/mailman/listinfo/python-ideas</a><br>
</blockquote></div><br>