From 097fa42329b86a6cb5ebb3fd005410c62cd0994f Mon Sep 17 00:00:00 2001 From: Michele Guerini Rocco Date: Fri, 12 Apr 2024 17:31:53 +0200 Subject: [PATCH] Makefile: run tests in parallel --- Makefile | 12 +++++++++--- tests/__main__.py | 28 +++++++++++++++++++++++++--- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index 6f53af7..9c98d6d 100644 --- a/Makefile +++ b/Makefile @@ -79,6 +79,9 @@ FFLAGS += -J$(OBJDIR) -I$(INCDIR) -ffree-line-length-none -fPIC -frecursive LDFLAGS += -L$(LIBDIR) CPPFLAGS += -DREVISION=\"$(GIT_REV)$(GIT_DIRTY)\" -DPREFIX=\"$(PREFIX)\" +# Sync the output from jobs running in parallel +MAKEFLAGS += --output-sync + ifndef DETERMINISTIC ifdef AR_DEFAULT_DETERMINISTIC # Write timestamps to allow partial updates @@ -122,9 +125,12 @@ all: $(BINARIES) $(LIBRARIES) clean: rm -r $(BUILDDIR) -# Run tests -check: $(GRAY) - python -Bm tests --binary $^ +# Run all tests +check: $(shell python -Bm tests --list-cases) + +# Run a test case +tests.%: $(GRAY) + python -Bm tests '$@' --binary '$(GRAY)' # Install libraries, binaries and documentation install: $(BINARIES) $(LIBRARIES) $(SHAREDIR)/doc $(MANPAGES) diff --git a/tests/__main__.py b/tests/__main__.py index 10193b0..e84da0b 100644 --- a/tests/__main__.py +++ b/tests/__main__.py @@ -3,9 +3,24 @@ 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}'} + + if __name__ == '__main__': # handle custom CLI arguments - parser = argparse.ArgumentParser(add_help=False) + parser = argparse.ArgumentParser(prog='tests.main') parser.add_argument('--keep-failed', action='store_true', help='whether to keep output files of failed tests') parser.add_argument('--visual', action='store_true', @@ -15,8 +30,15 @@ if __name__ == '__main__': 'current ones') parser.add_argument('--binary', type=str, default='build/bin/gray', help='the gray binary to be tested') + parser.add_argument('--list-cases', action='store_true', + help='only list the test cases') options, other_args = parser.parse_known_args() tests.options = options - # start the test runner - unittest.main(module=None, argv=["tests.main"] + other_args) + if options.list_cases: + # list all test cases + suite = unittest.TestLoader().discover('.') + print(*sorted(get_cases(suite)), sep='\n') + else: + # start the test runner + unittest.main(module=None, argv=["tests.main"] + other_args)