summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Collins <robertc@robertcollins.net>2008-12-09 04:51:09 +1100
committerRobert Collins <robertc@robertcollins.net>2008-12-09 04:51:09 +1100
commit517ce1f70cb7a969b808a0445dd75c278f61ff69 (patch)
tree18a6f45d13d130b1144c5d6cb52392eb515f454e
parent9639c3861f117bd8e23a7198c0eea917d9cbe3f3 (diff)
parenta01df9b9b4750913c14858132055edcffb84f7cd (diff)
downloadtestresources-517ce1f70cb7a969b808a0445dd75c278f61ff69.tar.gz
Merge James Henstridges fix for resource leaks during transition to non-resourced tests.
-rw-r--r--NEWS3
-rw-r--r--lib/testresources/__init__.py13
-rw-r--r--lib/testresources/tests/test_optimising_test_suite.py26
3 files changed, 33 insertions, 9 deletions
diff --git a/NEWS b/NEWS
index 865112e..3692fce 100644
--- a/NEWS
+++ b/NEWS
@@ -26,6 +26,9 @@ IN DEVELOPMENT
* Calling getResource on a dirty resource now triggers a clean and re-make
of that resource. (Jonathan Lange)
+ * All resources are dropped when a test with no declared resources is run.
+ (James Henstridge)
+
API CHANGES:
* All methods on TestResource are now instance methods, and thus tests
diff --git a/lib/testresources/__init__.py b/lib/testresources/__init__.py
index 02900a4..2f317ea 100644
--- a/lib/testresources/__init__.py
+++ b/lib/testresources/__init__.py
@@ -105,13 +105,12 @@ class OptimisingTestSuite(unittest.TestSuite):
for test in self._tests:
if result.shouldStop:
break
- resources = getattr(test, 'resources', None)
- if resources is not None:
- new_resources = set()
- for name, resource in resources:
- new_resources.update(resource.neededResources())
- self.switch(current_resources, new_resources)
- current_resources = new_resources
+ resources = getattr(test, 'resources', [])
+ new_resources = set()
+ for name, resource in resources:
+ new_resources.update(resource.neededResources())
+ self.switch(current_resources, new_resources)
+ current_resources = new_resources
test(result)
self.switch(current_resources, set())
return result
diff --git a/lib/testresources/tests/test_optimising_test_suite.py b/lib/testresources/tests/test_optimising_test_suite.py
index 82bea7d..4bf06d3 100644
--- a/lib/testresources/tests/test_optimising_test_suite.py
+++ b/lib/testresources/tests/test_optimising_test_suite.py
@@ -50,9 +50,13 @@ class MakeCounter(testresources.TestResource):
class TestOptimisingTestSuite(testtools.TestCase):
- def makeTestCase(self):
+ def makeTestCase(self, test_running_hook=None):
"""Make a normal TestCase."""
- return unittest.TestCase('run')
+ class TestCaseForTesting(unittest.TestCase):
+ def runTest(self):
+ if test_running_hook:
+ test_running_hook()
+ return TestCaseForTesting('runTest')
def makeResourcedTestCase(self, resource_manager, test_running_hook):
"""Make a ResourcedTestCase."""
@@ -141,6 +145,24 @@ class TestOptimisingTestSuite(testtools.TestCase):
suite.run(None)
self.assertEqual(suite.sorted, True)
+ def testResourcesDroppedForNonResourcedTestCase(self):
+ sample_resource = MakeCounter()
+ def resourced_case_hook():
+ self.assertTrue(sample_resource._uses > 0)
+ self.optimising_suite.addTest(self.makeResourcedTestCase(
+ sample_resource, resourced_case_hook))
+
+ def normal_case_hook():
+ # The resource should not be acquired when the normal test
+ # runs.
+ self.assertEqual(sample_resource._uses, 0)
+ self.optimising_suite.addTest(self.makeTestCase(normal_case_hook))
+
+ result = unittest.TestResult()
+ self.optimising_suite.run(result)
+ self.assertEqual(result.testsRun, 2)
+ self.assertEqual(result.wasSuccessful(), True)
+
class TestSplitByResources(testtools.TestCase):
"""Tests for split_by_resources."""