summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIhar Hrachyshka <ihrachys@redhat.com>2014-05-26 22:06:43 +0200
committerIhar Hrachyshka <ihrachys@redhat.com>2014-05-28 11:12:28 +0200
commit3f6b7424ffb6e576b50346f338d64cc5e8289e6a (patch)
treedeb25662bc8d65104a1d6d5e2398ae1f71892e13
parent5a22a6e8ce94c88e9ec77f0aaaa54c114237213c (diff)
downloadoslotest-3f6b7424ffb6e576b50346f338d64cc5e8289e6a.tar.gz
Cleanup mock patches on BaseTestCase tearDown()
Instead of cleaning up any remaining mock patches in each unit test that uses them, do it once on BaseTestCase tearDown(). Change-Id: I121ba554e16d83fc5908bb85afee4940f42dc4eb Closes-Bug: 1323214
-rw-r--r--oslotest/base.py2
-rw-r--r--tests/unit/test_base.py21
2 files changed, 23 insertions, 0 deletions
diff --git a/oslotest/base.py b/oslotest/base.py
index f81fae6..f84e810 100644
--- a/oslotest/base.py
+++ b/oslotest/base.py
@@ -20,6 +20,7 @@ import os
import tempfile
import fixtures
+import mock
import testtools
_TRUE_VALUES = ('True', 'true', '1', 'yes')
@@ -35,6 +36,7 @@ class BaseTestCase(testtools.TestCase):
self._fake_logs()
self.useFixture(fixtures.NestedTempfile())
self.useFixture(fixtures.TempHomeDir())
+ self.addCleanup(mock.patch.stopall)
def _set_timeout(self):
test_timeout = os.environ.get('OS_TEST_TIMEOUT', 0)
diff --git a/tests/unit/test_base.py b/tests/unit/test_base.py
index 8317d28..8b76cc9 100644
--- a/tests/unit/test_base.py
+++ b/tests/unit/test_base.py
@@ -13,6 +13,7 @@
# under the License.
import logging
+import unittest
import mock
import testtools
@@ -71,3 +72,23 @@ class TestBaseTestCase(testtools.TestCase):
env_get_mock.assert_any_call('OS_LOG_CAPTURE')
env_get_mock.assert_any_calls('OS_DEBUG')
self.assertEqual(fixture_mock.call_count, 1)
+
+ def test_mock_patch_cleanup_on_teardown(self):
+ # create an object and save its reference
+ class Sub(object):
+ pass
+
+ obj = Sub()
+ obj.value = obj.backup = object()
+
+ # patch the object
+ mock.patch.object(obj, 'value').start()
+ self.assertNotEqual(obj.value, obj.backup)
+
+ # run a test case
+ loader = unittest.defaultTestLoader
+ suite = loader.loadTestsFromTestCase(self.FakeTestCase)
+ suite.run(unittest.TestResult())
+
+ # check that mock patches are cleaned up
+ self.assertEqual(obj.value, obj.backup)