By extending or mixing-in this class, tests can have necessary resources automatically allocated and disposed or recycled.
ResourceTestCase can be used as a base class for tests, and when that is done tests will have their resources attribute automatically checked for resources by both OptimisingTestSuite and their own setUp() and tearDown() methods. (This allows tests to remain functional without needing this specific TestSuite as a container). Alternatively, you can call setUpResources(self, resources, test_result) and tearDownResources(self, resources, test_result) from your own classes setUp and tearDown and the same behaviour will be activated.
To declare the use of a resource, set the resources attribute to a list of tuples of (attribute_name, resource_manager).
During setUp, for each declared requirement, the test gains an attribute pointing to an allocated resource, which is the result of calling resource_manager.getResource(). finishedWith will be called on each resource during tearDown().
class TestLog(testresources.ResourcedTestCase): resources = [('branch', BzrPopulatedBranch())] def test_log(self): show_log(self.branch, ...)
A TestResourceManager is an object that tests can use to create resources. It can be overridden to manage different types of resources. Normally test code doesn’t need to call any methods on it, as this will be arranged by the testresources machinery.
When implementing a new TestResourceManager subclass you should consider overriding these methods:
Must be overridden in every concrete subclass.
Returns a new instance of the resource object (the actual resource, not the TestResourceManager). Doesn’t need to worry about reuse, which is taken care of separately. This method is only called when a new resource is definitely needed.
make is called by getResource; you should not normally need to override the latter.
- Cleans up an existing resource instance, eg by deleting a directory or closing a network connection. By default this does nothing, which may be appropriate for resources that are automatically garbage collected.
- Reset a no-longer-used dirty resource to a clean state. By default this just discards it and creates a new one, but for some resources there may be a faster way to reset them.
- Check whether an existing resource is dirty. By default this just reports whether TestResourceManager.dirtied has been called or any of the dependency resources are dirty.
class TemporaryDirectoryResource(TestResourceManager): def clean(self, resource): shutil.rmtree(resource) def make(self): return tempfile.mkdtemp() def isDirty(self, resource): # Can't detect when the directory is written to, so assume it # can never be reused. We could list the directory, but that might # not catch it being open as a cwd etc. return True
The resources list on the TestResourceManager object is used to declare dependencies. For instance, a DataBaseResource that needs a TemporaryDirectory might be declared with a resources list:
class DataBaseResource(TestResourceManager): resources = [("scratchdir", TemporaryDirectoryResource())]
Most importantly, two getResources to the same TestResourceManager with no finishedWith call in the middle, will return the same object as long as it is not dirty.
When a Test has a dependency and that dependency successfully completes but returns None, the framework does not consider this an error: be sure to always return a valid resource, or raise an error. Error handling hasn’t been heavily exercised, but any bugs in this area will be promptly dealt with.
A sample TestResourceManager can be found in the doc/ folder.
See pydoc testresources.TestResourceManager for details.
Glue to adapt testresources to an existing resource-like class.
Glue to adapt testresources to the simpler fixtures.Fixture API. Long term testresources is likely to consolidate on that simpler API as the recommended method of writing resources.
This TestSuite will introspect all the test cases it holds directly and if they declare needed resources, will run the tests in an order that attempts to minimise the number of setup and tear downs required. It attempts to achieve this by callling getResource() and finishedWith() around the sequence of tests that use a specific resource.
Tests are added to an OptimisingTestSuite as normal. Any standard library TestSuite objects will be flattened, while any custom TestSuite subclasses will be distributed across their member tests. This means that any custom logic in test suites should be preserved, at the price of some level of optimisation.
Because the test suite does the optimisation, you can control the amount of optimising that takes place by adding more or fewer tests to a single OptimisingTestSuite. You could add everything to a single OptimisingTestSuite, getting global optimisation or you could use several smaller OptimisingTestSuites.
This is a trivial TestLoader that creates OptimisingTestSuites by default.
testresources will log activity about resource creation and destruction to the result object tests are run with. 6 extension methods are looked for: startCleanResource, stopCleanResource, startMakeResource, stopMakeResource, startResetResource and finally stopResetResource. testresources.tests.ResultWithResourceExtensions is an example of a TestResult with these methods present.