Adds a skip method to identify useless skips
We have places in our test code that skips tests in a child class because the setup differs from the parent. This often leads to a situation where we remove the original test, but since the skip always passes we never remove it. This will make the skips fail if the original method no longer exists. Change-Id: I7f2049d21e1cbcbd3c74d85fd6c74680db2a245d
This commit is contained in:
parent
d18bb02741
commit
6c6484fc0b
|
@ -562,6 +562,19 @@ class BaseTestCase(testtools.TestCase):
|
||||||
if not os.environ.get(env_var):
|
if not os.environ.get(env_var):
|
||||||
self.skipTest('Env variable %s is not set.' % env_var)
|
self.skipTest('Env variable %s is not set.' % env_var)
|
||||||
|
|
||||||
|
def skip_test_overrides(self, *args, **kwargs):
|
||||||
|
if self._check_for_method_in_parents(self._testMethodName):
|
||||||
|
return super(BaseTestCase, self).skipTest(*args, **kwargs)
|
||||||
|
raise Exception('%r is not a previously defined test method'
|
||||||
|
% self._testMethodName)
|
||||||
|
|
||||||
|
def _check_for_method_in_parents(self, name):
|
||||||
|
# skip first to get to parents
|
||||||
|
for cls in self.__class__.__mro__[1:]:
|
||||||
|
if hasattr(cls, name):
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
class TestCase(BaseTestCase):
|
class TestCase(BaseTestCase):
|
||||||
|
|
||||||
|
|
|
@ -33,6 +33,41 @@ class BaseTestTestCase(unit.BaseTestCase):
|
||||||
matchers.raises(unit.UnexpectedExit))
|
matchers.raises(unit.UnexpectedExit))
|
||||||
|
|
||||||
|
|
||||||
|
class TestOverrideSkipping(unit.BaseTestCase):
|
||||||
|
|
||||||
|
class TestParent(unit.BaseTestCase):
|
||||||
|
def test_in_parent(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class TestChild(TestParent):
|
||||||
|
def test_in_parent(self):
|
||||||
|
self.skip_test_overrides('some message')
|
||||||
|
|
||||||
|
def test_not_in_parent(self):
|
||||||
|
self.skip_test_overrides('some message')
|
||||||
|
|
||||||
|
def test_skip_test_override_success(self):
|
||||||
|
# NOTE(dstanek): let's run the test and see what happens
|
||||||
|
test = self.TestChild('test_in_parent')
|
||||||
|
result = test.run()
|
||||||
|
|
||||||
|
# NOTE(dstanek): reach into testtools to ensure the test succeeded
|
||||||
|
self.assertEqual([], result.decorated.errors)
|
||||||
|
|
||||||
|
def test_skip_test_override_fails_for_missing_parent_test_case(self):
|
||||||
|
# NOTE(dstanek): let's run the test and see what happens
|
||||||
|
test = self.TestChild('test_not_in_parent')
|
||||||
|
result = test.run()
|
||||||
|
|
||||||
|
# NOTE(dstanek): reach into testtools to ensure the test failed
|
||||||
|
# the way we expected
|
||||||
|
observed_error = result.decorated.errors[0]
|
||||||
|
observed_error_msg = observed_error[1]
|
||||||
|
expected_error_msg = ("'test_not_in_parent' is not a previously "
|
||||||
|
"defined test method")
|
||||||
|
self.assertIn(expected_error_msg, observed_error_msg)
|
||||||
|
|
||||||
|
|
||||||
class TestTestCase(unit.TestCase):
|
class TestTestCase(unit.TestCase):
|
||||||
|
|
||||||
def test_bad_log(self):
|
def test_bad_log(self):
|
||||||
|
|
Loading…
Reference in New Issue