gray/tests/__main__.py
Michele Guerini Rocco 09724908d1
tests/__main__.py: wrap TestProgram
Instead of parsing the arguments twice, wrap unittest.main
and inject our custom argument into the existing parser.
2025-03-21 16:54:48 +01:00

53 lines
1.6 KiB
Python

import unittest
import tests
def get_cases(suite):
'''
Get a list of TestCase from a TestSuite
'''
if hasattr(suite, '__iter__'):
res = set()
for x in suite:
res |= get_cases(x)
return res
mod = type(suite).__module__
cls = type(suite).__name__
return {f'{mod}.{cls}'}
class GrayTestProgram(unittest.TestProgram):
def _initArgParsers(self):
# add custom CLI arguments
super()._initArgParsers()
p = self._main_parser
p.add_argument('--keep-failed', action='store_true',
help='whether to keep output files of failed tests')
p.add_argument('--visual', action='store_true',
help='whether to show plots, helps with debugging')
p.add_argument('--update', action='store_true',
help='update the reference results with the'
'current ones')
p.add_argument('--binary', type=str, default='build/bin/gray',
help='the gray binary to be tested')
p.add_argument('--list-cases', action='store_true',
help='only list the test cases')
def parseArgs(self, argv):
super().parseArgs(argv)
# store args in the test classes
tests.GrayTest.options = self
tests.TestCase.options = self
# handle custom harguments
if self.list_cases:
print(*sorted(get_cases(self.test)), sep='\n')
exit()
if __name__ == '__main__':
# start the test runner
GrayTestProgram(module=None)