tests: add --do-not-clean

This commit is contained in:
Wojciech Zygmunt Porczyk 2015-03-05 13:40:37 +01:00
parent c5ffba8eb7
commit f61045178a
2 changed files with 34 additions and 1 deletions

View File

@ -116,6 +116,9 @@ class _AssertNotRaisesContext(object):
self.exception = exc_value # store for later retrieval self.exception = exc_value # store for later retrieval
class BeforeCleanExit(BaseException):
pass
class QubesTestCase(unittest.TestCase): class QubesTestCase(unittest.TestCase):
'''Base class for Qubes unit tests. '''Base class for Qubes unit tests.
''' '''
@ -135,6 +138,19 @@ class QubesTestCase(unittest.TestCase):
self._testMethodName) self._testMethodName)
def tearDown(self):
super(QubesTestCase, self).tearDown()
result = self._resultForDoCleanups
l = result.failures \
+ result.errors \
+ [(tc, None) for tc in result.unexpectedSuccesses]
if getattr(result, 'do_not_clean', False) \
and filter((lambda (tc, exc): tc is self), l):
raise BeforeCleanExit()
def assertNotRaises(self, excClass, callableObj=None, *args, **kwargs): def assertNotRaises(self, excClass, callableObj=None, *args, **kwargs):
"""Fail if an exception of class excClass is raised """Fail if an exception of class excClass is raised
by callableObj when invoked with arguments args and keyword by callableObj when invoked with arguments args and keyword

View File

@ -211,6 +211,10 @@ class QubesTestResult(unittest.TestResult):
self.stream.writeln('%s' % err) self.stream.writeln('%s' % err)
class QubesDNCTestResult(QubesTestResult):
do_not_clean = True
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
epilog='''When running only specific tests, write their names like in log, epilog='''When running only specific tests, write their names like in log,
in format: MODULE+"/"+CLASS+"/"+FUNCTION. MODULE should omit initial in format: MODULE+"/"+CLASS+"/"+FUNCTION. MODULE should omit initial
@ -223,6 +227,13 @@ parser.add_argument('--no-failfast',
action='store_false', dest='failfast', action='store_false', dest='failfast',
help='disable --failfast') help='disable --failfast')
parser.add_argument('--do-not-clean', '--dnc', '-D',
action='store_true', dest='do_not_clean',
help='do not execute tearDown on failed tests. Implies --failfast.')
parser.add_argument('--do-clean', '-C',
action='store_false', dest='do_not_clean',
help='do execute tearDown even on failed tests.')
parser.add_argument('--verbose', '-v', parser.add_argument('--verbose', '-v',
action='count', action='count',
help='increase console verbosity level') help='increase console verbosity level')
@ -274,6 +285,9 @@ parser.set_defaults(
def main(): def main():
args = parser.parse_args() args = parser.parse_args()
if args.do_not_clean:
args.failfast = True
logging.root.setLevel(args.loglevel) logging.root.setLevel(args.loglevel)
if args.logfile is not None: if args.logfile is not None:
@ -314,7 +328,10 @@ def main():
verbosity=(args.verbose-args.quiet), verbosity=(args.verbose-args.quiet),
failfast=args.failfast) failfast=args.failfast)
unittest.signals.installHandler() unittest.signals.installHandler()
runner.resultclass = QubesTestResult
runner.resultclass = QubesDNCTestResult \
if args.do_not_clean else QubesTestResult
return runner.run(suite).wasSuccessful() return runner.run(suite).wasSuccessful()