Merge remote-tracking branch 'woju/wip-tests'

This commit is contained in:
Marek Marczykowski-Górecki 2015-03-07 02:48:21 +01:00
commit 304ed1dec5
2 changed files with 69 additions and 17 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.
''' '''
@ -129,12 +132,25 @@ class QubesTestCase(unittest.TestCase):
def __str__(self): def __str__(self):
return '{}.{}.{}'.format( return '{}/{}/{}'.format(
'.'.join(self.__class__.__module__.split('.')[2:]), '.'.join(self.__class__.__module__.split('.')[2:]),
self.__class__.__name__, self.__class__.__name__,
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,11 +211,26 @@ 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
"qubes.tests.". Example: basic/TC_00_Basic/test_000_create''') "qubes.tests.". Example: basic/TC_00_Basic/test_000_create''')
parser.add_argument('--verbose', '-v',
action='count',
help='increase console verbosity level')
parser.add_argument('--quiet', '-q',
action='count',
help='decrease console verbosity level')
parser.add_argument('--list', '-l',
action='store_true', dest='list',
help='list all available tests and exit')
parser.add_argument('--failfast', '-f', parser.add_argument('--failfast', '-f',
action='store_true', dest='failfast', action='store_true', dest='failfast',
help='stop on the first fail, error or unexpected success') help='stop on the first fail, error or unexpected success')
@ -223,12 +238,12 @@ parser.add_argument('--no-failfast',
action='store_false', dest='failfast', action='store_false', dest='failfast',
help='disable --failfast') help='disable --failfast')
parser.add_argument('--verbose', '-v', parser.add_argument('--do-not-clean', '--dnc', '-D',
action='count', action='store_true', dest='do_not_clean',
help='increase console verbosity level') help='do not execute tearDown on failed tests. Implies --failfast.')
parser.add_argument('--quiet', '-q', parser.add_argument('--do-clean', '-C',
action='count', action='store_false', dest='do_not_clean',
help='decrease console verbosity level') help='do execute tearDown even on failed tests.')
parser.add_argument('--loglevel', '-L', metavar='LEVEL', parser.add_argument('--loglevel', '-L', metavar='LEVEL',
action='store', choices=tuple(k action='store', choices=tuple(k
@ -271,9 +286,36 @@ parser.set_defaults(
quiet=0) quiet=0)
def list_test_cases(suite):
for test in suite:
if isinstance(test, unittest.TestSuite):
#yield from
for i in list_test_cases(test):
yield i
else:
yield test
def main(): def main():
args = parser.parse_args() args = parser.parse_args()
suite = unittest.TestSuite()
loader = unittest.TestLoader()
if args.names:
suite.addTests(loader.loadTestsFromNames(
('qubes.tests.' + name.replace('/', '.') for name in args.names)))
else:
suite.addTests(loader.loadTestsFromName('qubes.tests'))
if args.list:
for test in list_test_cases(suite):
print(str(test))
return True
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:
@ -301,20 +343,14 @@ def main():
ha_kmsg.setLevel(logging.CRITICAL) ha_kmsg.setLevel(logging.CRITICAL)
logging.root.addHandler(ha_kmsg) logging.root.addHandler(ha_kmsg)
suite = unittest.TestSuite()
loader = unittest.TestLoader()
if args.names:
suite.addTests(loader.loadTestsFromNames(
('qubes.tests.' + name.replace('/', '.') for name in args.names)))
else:
suite.addTests(loader.loadTestsFromName('qubes.tests'))
runner = unittest.TextTestRunner(stream=sys.stdout, runner = unittest.TextTestRunner(stream=sys.stdout,
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()