diff options
-rw-r--r-- | NEWS | 3 | ||||
-rw-r--r-- | lib/testresources/__init__.py | 13 | ||||
-rw-r--r-- | lib/testresources/tests/test_optimising_test_suite.py | 26 |
3 files changed, 33 insertions, 9 deletions
@@ -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.""" |