提交 8017d883 编写于 作者: L Larry Hamel 提交者: Marbin Tan

Fix unit test pollution when calling apply patches

* When mocking, we may setup patches that are not cleaned up properly
  and causes test pollutions. Ensure that we do tearDown properly and
  raise a condition if we are not.
Signed-off-by: NMarbin Tan <mtan@pivotal.io>
上级 a86e4901
......@@ -6,18 +6,36 @@ class GpTestCase(unittest.TestCase):
super(GpTestCase, self).__init__(methodName)
self.patches = []
self.mock_objs = []
self.__class__.apply_patches_counter = 0
self.__class__.tear_down_counter = 0
def apply_patches(self, patches):
if self.patches:
raise Exception('Test class is already patched!')
self.patches = patches
self.mock_objs = [p.start() for p in self.patches]
self.__class__.apply_patches_counter += 1
# if you have a tearDown() in your test class,
# be sure to call this using super.tearDown()
# be sure to call this using super(<child class name>, self).tearDown()
def tearDown(self):
[p.stop() for p in self.patches]
self.mock_objs = []
self.__class__.tear_down_counter += 1
@classmethod
def setUpClass(cls):
cls.apply_patches_counter = 0
cls.tear_down_counter = 0
@classmethod
def tearDownClass(cls):
if cls.apply_patches_counter > 0 and cls.apply_patches_counter != cls.tear_down_counter:
raise Exception("Unequal call for apply patches: %s, teardown: %s. "
"You probably need to add a super(<child class>, "
"self).tearDown() in your tearDown()" % (cls.apply_patches_counter,
cls.tear_down_counter))
def add_setup(setup=None, teardown=None):
"""decorate test functions to add additional setup/teardown contexts"""
......
......@@ -68,6 +68,7 @@ class GpConfig(GpTestCase):
def tearDown(self):
shutil.rmtree(self.temp_dir)
super(GpConfig, self).tearDown()
def createGpArrayWith2Primary2Mirrors(self):
master = GpDB.initFromString(
......
......@@ -153,6 +153,8 @@ class GpTransfer(GpTestCase):
def tearDown(self):
shutil.rmtree(self.TEMP_DIR)
super(GpTransfer, self).tearDown()
@patch('gptransfer.TableValidatorFactory', return_value=Mock())
def test__get_distributed_by_quotes_column_name(self, mock1):
......
......@@ -104,6 +104,7 @@ class RepairTestCase(GpTestCase):
def tearDown(self):
shutil.rmtree(self.repair_dir_path)
super(RepairTestCase, self).tearDown()
if __name__ == '__main__':
run_tests()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册