diff options
author | Robert Collins <robertc@robertcollins.net> | 2009-06-17 19:32:04 +1000 |
---|---|---|
committer | Robert Collins <robertc@robertcollins.net> | 2009-06-17 19:32:04 +1000 |
commit | 3d7ddb2e1db611df34b28ba9c34953ac4d084c32 (patch) | |
tree | 9c397bd5e3a9965bd0095e7ae252cd2ed4f2062a /lib | |
parent | b571a618c617fb02805731eab238baa75c5a4179 (diff) | |
parent | 513b06646e608174d3863e2083db50dbd78f5a93 (diff) | |
download | testresources-3d7ddb2e1db611df34b28ba9c34953ac4d084c32.tar.gz |
Merge bug fix for lp:271257
Diffstat (limited to 'lib')
-rw-r--r-- | lib/testresources/__init__.py | 2 | ||||
-rw-r--r-- | lib/testresources/tests/test_optimising_test_suite.py | 57 | ||||
-rw-r--r-- | lib/testresources/tests/test_test_resource.py | 5 |
3 files changed, 53 insertions, 11 deletions
diff --git a/lib/testresources/__init__.py b/lib/testresources/__init__.py index 5a5aa15..820d62b 100644 --- a/lib/testresources/__init__.py +++ b/lib/testresources/__init__.py @@ -235,8 +235,6 @@ class TestResource(object): if self._uses == 0: self.clean_all(resource) self._setResource(None) - elif self._dirty: - self._resetResource(resource) def getResource(self): """Get the resource for this class and record that it's being used. diff --git a/lib/testresources/tests/test_optimising_test_suite.py b/lib/testresources/tests/test_optimising_test_suite.py index fddbb5f..6be88e4 100644 --- a/lib/testresources/tests/test_optimising_test_suite.py +++ b/lib/testresources/tests/test_optimising_test_suite.py @@ -49,13 +49,17 @@ class MakeCounter(testresources.TestResource): testresources.TestResource.__init__(self) self.cleans = 0 self.makes = 0 + self.calls = [] def clean(self, resource): self.cleans += 1 + self.calls.append(('clean', resource)) def make(self, dependency_resources): self.makes += 1 - return "boo" + resource = "boo %d" % self.makes + self.calls.append(('make', resource)) + return resource class TestOptimisingTestSuite(testtools.TestCase): @@ -65,14 +69,14 @@ class TestOptimisingTestSuite(testtools.TestCase): class TestCaseForTesting(unittest.TestCase): def runTest(self): if test_running_hook: - test_running_hook() + test_running_hook(self) return TestCaseForTesting('runTest') def makeResourcedTestCase(self, resource_manager, test_running_hook): """Make a ResourcedTestCase.""" class ResourcedTestCaseForTesting(testresources.ResourcedTestCase): def runTest(self): - test_running_hook() + test_running_hook(self) test_case = ResourcedTestCaseForTesting('runTest') test_case.resources = [('_default', resource_manager)] return test_case @@ -135,7 +139,7 @@ class TestOptimisingTestSuite(testtools.TestCase): def testSingleCaseResourceAcquisition(self): sample_resource = MakeCounter() - def getResourceCount(): + def getResourceCount(test): self.assertEqual(sample_resource._uses, 2) case = self.makeResourcedTestCase(sample_resource, getResourceCount) self.optimising_suite.addTest(case) @@ -147,7 +151,7 @@ class TestOptimisingTestSuite(testtools.TestCase): def testResourceReuse(self): make_counter = MakeCounter() - def getResourceCount(): + def getResourceCount(test): self.assertEqual(make_counter._uses, 2) case = self.makeResourcedTestCase(make_counter, getResourceCount) case2 = self.makeResourcedTestCase(make_counter, getResourceCount) @@ -182,21 +186,58 @@ class TestOptimisingTestSuite(testtools.TestCase): def testResourcesDroppedForNonResourcedTestCase(self): sample_resource = MakeCounter() - def resourced_case_hook(): + def resourced_case_hook(test): self.assertTrue(sample_resource._uses > 0) self.optimising_suite.addTest(self.makeResourcedTestCase( sample_resource, resourced_case_hook)) - - def normal_case_hook(): + def normal_case_hook(test): # 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.failures) + self.assertEqual([], result.errors) + self.assertEqual(result.wasSuccessful(), True) + + def testDirtiedResourceNotRecreated(self): + make_counter = MakeCounter() + def dirtyResource(test): + make_counter.dirtied(test._default) + case = self.makeResourcedTestCase(make_counter, dirtyResource) + self.optimising_suite.addTest(case) + result = unittest.TestResult() + self.optimising_suite.run(result) + self.assertEqual(result.testsRun, 1) + self.assertEqual(result.wasSuccessful(), True) + # The resource should only have been made once. + self.assertEqual(make_counter.makes, 1) + def testDirtiedResourceCleanedUp(self): + make_counter = MakeCounter() + def testOne(test): + make_counter.calls.append('test one') + make_counter.dirtied(test._default) + def testTwo(test): + make_counter.calls.append('test two') + case1 = self.makeResourcedTestCase(make_counter, testOne) + case2 = self.makeResourcedTestCase(make_counter, testTwo) + self.optimising_suite.addTest(case1) + self.optimising_suite.addTest(case2) result = unittest.TestResult() self.optimising_suite.run(result) self.assertEqual(result.testsRun, 2) self.assertEqual(result.wasSuccessful(), True) + # Two resources should have been created and cleaned up + self.assertEqual(make_counter.calls, + [('make', 'boo 1'), + 'test one', + ('clean', 'boo 1'), + ('make', 'boo 2'), + 'test two', + ('clean', 'boo 2')]) class TestSplitByResources(testtools.TestCase): diff --git a/lib/testresources/tests/test_test_resource.py b/lib/testresources/tests/test_test_resource.py index 754ecd3..9c33d86 100644 --- a/lib/testresources/tests/test_test_resource.py +++ b/lib/testresources/tests/test_test_resource.py @@ -197,13 +197,16 @@ class TestTestResource(testtools.TestCase): resource_manager.dirtied(resource) self.assertEqual(True, resource_manager._dirty) - def testDirtyingResourceTriggersClean(self): + def testDirtyingResourceTriggersCleanOnGet(self): resource_manager = MockResource() resource1 = resource_manager.getResource() resource2 = resource_manager.getResource() resource_manager.dirtied(resource2) resource_manager.finishedWith(resource2) + self.assertEqual(0, resource_manager.cleans) + resource3 = resource_manager.getResource() self.assertEqual(1, resource_manager.cleans) + resource_manager.finishedWith(resource3) resource_manager.finishedWith(resource1) self.assertEqual(2, resource_manager.cleans) |