On 12/02/2010 19:48, Guido van Rossum wrote:
[snip...]
Here's a current minimal example of using Test Resources. It could be
simplified further with helper functions and by some of the functionality
moving into unittest itself. OptimisingTestSuite here ensures that the
resource is created before first use (MyTempDir.make is called) and disposed
of when finished with (MyTempDir.clean is called).

import shutil
import tempfile
import testresources

def load_tests(loader, tests, pattern):
# this step could be built into the standard loader
return testresources.OptimisingTestSuite(tests)

class MyTempDir(testresources.TestResource):
def make(self, dependency_resources):
return tempfile.mkdtemp()

def clean(self, resource):
shutil.rmtree(resource)

class MyTest(testresources.ResourcedTestCase):
resources = [('workdir', MyTempDir())]
def test_foo(self):
print self.workdir
def test_bar(self):
print self.workdir
This came out with all leading indentation removed, but I think I can
guess what you meant to write.
For goodness sake. Sorry about that.

However from this example I *cannot* guess whether those resources are
set up and torn down per test or per test class.
This particular example is the equivalent of setUpClass - so by declaring the resource as a class attribute it will created before the first test for the class is run and disposed of after the last test for the class.

You could *also* create a single resource and share it between several test classes, or even across classes in several modules, and have it created and disposed of at the right point. I've copied Rob Collins in on this email in case I've misunderstood.
Also the notation

   resources = [('workdir', MyTempDir())]

looks pretty ugly -- if 'workdir' ends up being an instance attribute,
why not make it a dict instead of a list of tuples? Or even better, a
could each resource become a class variable?

I guess we could introspect the class for every attribute that is a resource, but I prefer some way of explicitly declaring which resources a TestCase is using.

Michael Foord

--
http://www.ironpythoninaction.com/
http://www.voidspace.org.uk/blog

READ CAREFULLY. By accepting and reading this email you agree, on behalf of 
your employer, to release me from all obligations and waivers arising from any 
and all NON-NEGOTIATED agreements, licenses, terms-of-service, shrinkwrap, 
clickwrap, browsewrap, confidentiality, non-disclosure, non-compete and 
acceptable use policies (”BOGUS AGREEMENTS”) that I have entered into with your 
employer, its partners, licensors, agents and assigns, in perpetuity, without 
prejudice to my ongoing rights and privileges. You further represent that you 
have the authority to release me from any BOGUS AGREEMENTS on behalf of your 
employer.


_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to