summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGustavo Niemeyer <gustavo@niemeyer.net>2010-06-20 15:04:54 -0300
committerGustavo Niemeyer <gustavo@niemeyer.net>2010-06-20 15:04:54 -0300
commit3a4c487d0d08f51dca83e31f35aae5ecb3affb82 (patch)
treefd641989efbaec56956bbadaa9cbe2fd7ce5e3c2
parent1a572f6c05cc6c7aa2b58e810684525cb8b66de7 (diff)
downloadmocker-3a4c487d0d08f51dca83e31f35aae5ecb3affb82.tar.gz
Fixed support for MockerTestCase.addCleanup() in Python 2.3,
by Anders F Björklund (#528657).
-rw-r--r--NEWS5
-rw-r--r--mocker.py9
-rwxr-xr-xtest.py38
3 files changed, 51 insertions, 1 deletions
diff --git a/NEWS b/NEWS
index 2022549..2b8fca3 100644
--- a/NEWS
+++ b/NEWS
@@ -11,10 +11,13 @@
certain cases because, even though that's *not* documented, Python
tries to use __length_hint__ in some cases.
+- Fixed support for MockerTestCase.addCleanup() in Python 2.3,
+ by Anders F Björklund (#528657).
+
- Implemented Expect helper, which allows creating a new expect()
"function" with an explicitly provided Mocker instance. This
helps in cases where the expression can't result in a Mock
- instance (e.g. expect(iter(mock))) (#196388).
+ instance (e.g. expect(iter(mock))) (#196388, #179072).
"function" with an explicitly provided Mocker instance.
diff --git a/mocker.py b/mocker.py
index 336e696..ae7970c 100644
--- a/mocker.py
+++ b/mocker.py
@@ -189,6 +189,15 @@ class MockerTestCase(unittest.TestCase):
super(MockerTestCase, self).__init__(methodName)
+ def __call__(self, *args, **kwargs):
+ # This is necessary for Python 2.3 only, because it didn't use run(),
+ # which is supported above.
+ try:
+ super(MockerTestCase, self).__call__(*args, **kwargs)
+ finally:
+ if sys.version_info < (2, 4):
+ self.__cleanup()
+
def __cleanup(self):
for path in self.__cleanup_paths:
if os.path.isfile(path):
diff --git a/test.py b/test.py
index 21c9d63..864bf7a 100755
--- a/test.py
+++ b/test.py
@@ -450,6 +450,44 @@ class MockerTestCaseTest(TestCase):
self.assertEquals(stash, [[], (1, 2), (3, 4)])
+ def test_cleanup_wrapper_in__call__for_2_3(self):
+ version_info = sys.version_info
+ __call__ = unittest.TestCase.__call__
+ try:
+ sys.version_info = (2, 3, 5)
+ stash = []
+ def call(self, *args, **kwargs):
+ self.addCleanup(lambda: stash.append(True))
+ unittest.TestCase.__call__ = call
+ class MyTest(MockerTestCase):
+ def test_method(self):
+ pass
+ MyTest("test_method")()
+ self.assertEquals(stash, [True])
+ finally:
+ unittest.TestCase.__call__ = __call__
+ sys.version_info = version_info
+
+ def test_cleanup_wrapper_in__call__for_2_4(self):
+ version_info = sys.version_info
+ __call__ = unittest.TestCase.__call__
+ try:
+ sys.version_info = (2, 4)
+ stash = []
+ def call(self, *args, **kwargs):
+ self.addCleanup(lambda: stash.append(True))
+ unittest.TestCase.__call__ = call
+ class MyTest(MockerTestCase):
+ def test_method(self):
+ pass
+ MyTest("test_method")()
+ # Python 2.4+ handles cleanup in run(), registered inside
+ # MockerTestCase.__init__, so this should *not* work.
+ self.assertEquals(stash, [])
+ finally:
+ unittest.TestCase.__call__ = __call__
+ sys.version_info = version_info
+
def test_twisted_trial_deferred_support(self):
calls = []
callbacks = []