From f61045178a97bd01e60f15506dfc165ef2523b91 Mon Sep 17 00:00:00 2001 From: Wojciech Zygmunt Porczyk Date: Thu, 5 Mar 2015 13:40:37 +0100 Subject: [PATCH] tests: add --do-not-clean --- tests/__init__.py | 16 ++++++++++++++++ tests/run.py | 19 ++++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/tests/__init__.py b/tests/__init__.py index 6c5f6b44..155b4720 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -116,6 +116,9 @@ class _AssertNotRaisesContext(object): self.exception = exc_value # store for later retrieval +class BeforeCleanExit(BaseException): + pass + class QubesTestCase(unittest.TestCase): '''Base class for Qubes unit tests. ''' @@ -135,6 +138,19 @@ class QubesTestCase(unittest.TestCase): 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): """Fail if an exception of class excClass is raised by callableObj when invoked with arguments args and keyword diff --git a/tests/run.py b/tests/run.py index 7c03a71d..cf4c5ebd 100755 --- a/tests/run.py +++ b/tests/run.py @@ -211,6 +211,10 @@ class QubesTestResult(unittest.TestResult): self.stream.writeln('%s' % err) +class QubesDNCTestResult(QubesTestResult): + do_not_clean = True + + parser = argparse.ArgumentParser( epilog='''When running only specific tests, write their names like in log, in format: MODULE+"/"+CLASS+"/"+FUNCTION. MODULE should omit initial @@ -223,6 +227,13 @@ parser.add_argument('--no-failfast', action='store_false', dest='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', action='count', help='increase console verbosity level') @@ -274,6 +285,9 @@ parser.set_defaults( def main(): args = parser.parse_args() + if args.do_not_clean: + args.failfast = True + logging.root.setLevel(args.loglevel) if args.logfile is not None: @@ -314,7 +328,10 @@ def main(): verbosity=(args.verbose-args.quiet), failfast=args.failfast) unittest.signals.installHandler() - runner.resultclass = QubesTestResult + + runner.resultclass = QubesDNCTestResult \ + if args.do_not_clean else QubesTestResult + return runner.run(suite).wasSuccessful()