Merge remote-tracking branch 'woju/wip-tests'
This commit is contained in:
commit
304ed1dec5
@ -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.
|
||||
'''
|
||||
@ -129,12 +132,25 @@ class QubesTestCase(unittest.TestCase):
|
||||
|
||||
|
||||
def __str__(self):
|
||||
return '{}.{}.{}'.format(
|
||||
return '{}/{}/{}'.format(
|
||||
'.'.join(self.__class__.__module__.split('.')[2:]),
|
||||
self.__class__.__name__,
|
||||
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
|
||||
|
68
tests/run.py
68
tests/run.py
@ -211,11 +211,26 @@ 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
|
||||
"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',
|
||||
action='store_true', dest='failfast',
|
||||
help='stop on the first fail, error or unexpected success')
|
||||
@ -223,12 +238,12 @@ parser.add_argument('--no-failfast',
|
||||
action='store_false', dest='failfast',
|
||||
help='disable --failfast')
|
||||
|
||||
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('--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('--loglevel', '-L', metavar='LEVEL',
|
||||
action='store', choices=tuple(k
|
||||
@ -271,9 +286,36 @@ parser.set_defaults(
|
||||
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():
|
||||
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)
|
||||
|
||||
if args.logfile is not None:
|
||||
@ -301,20 +343,14 @@ def main():
|
||||
ha_kmsg.setLevel(logging.CRITICAL)
|
||||
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,
|
||||
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()
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user