2015-08-12 01:59:29 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
import logging
|
|
|
|
|
2015-08-12 21:21:32 +00:00
|
|
|
try:
|
|
|
|
from mock import patch
|
|
|
|
except ImportError:
|
|
|
|
from unittest.mock import patch
|
2015-08-12 01:59:29 +00:00
|
|
|
try:
|
|
|
|
# Python 2.6
|
|
|
|
import unittest2 as unittest
|
|
|
|
except ImportError:
|
|
|
|
# Python >= 2.7
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
class TestCase(unittest.TestCase):
|
2015-08-12 21:21:32 +00:00
|
|
|
patch_these = []
|
2015-08-12 01:59:29 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
# disable logging while testing
|
|
|
|
logging.disable(logging.CRITICAL)
|
2015-08-12 21:21:32 +00:00
|
|
|
|
|
|
|
self.patched = {}
|
|
|
|
if hasattr(self, 'patch_these'):
|
|
|
|
for patch_this in self.patch_these:
|
|
|
|
namespace = patch_this[0] if isinstance(patch_this, (list, set)) else patch_this
|
|
|
|
|
|
|
|
patcher = patch(namespace)
|
|
|
|
mocked = patcher.start()
|
|
|
|
self.patched[namespace] = mocked
|
|
|
|
|
|
|
|
if isinstance(patch_this, (list, set)) and len(patch_this) > 0:
|
|
|
|
retval = patch_this[1]
|
|
|
|
if callable(retval):
|
|
|
|
retval = retval()
|
|
|
|
mocked.return_value = retval
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
patch.stopall()
|