From 5c795352c9a49249149bfa41caf34c21ad5b0e1a Mon Sep 17 00:00:00 2001 From: Aimylios Date: Fri, 17 Jan 2020 21:54:25 +0100 Subject: [PATCH] Allow excluding individual tests from test run --- setup/test.py | 17 ++++++++++++----- src/calibre/utils/run_tests.py | 8 ++++++++ 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/setup/test.py b/setup/test.py index 950c119ad2..2ded632a16 100644 --- a/setup/test.py +++ b/setup/test.py @@ -57,12 +57,12 @@ class TestImports(unittest.TestCase): self.assertGreater(self.base_check(base, exclude_packages, exclude_modules), 1000) -def find_tests(which_tests=None): +def find_tests(which_tests=None, exclude_tests=None): ans = [] a = ans.append def ok(x): - return not which_tests or x in which_tests + return (not which_tests or x in which_tests) and (not exclude_tests or x not in exclude_tests) if ok('build'): from calibre.test_build import find_tests @@ -154,17 +154,24 @@ class Test(Command): description = 'Run the calibre test suite' def add_options(self, parser): + parser.add_option('--test-verbosity', type=int, default=4, help='Test verbosity (0-4)') parser.add_option('--test-module', '--test-group', default=[], action='append', type='choice', choices=sorted(map(str, TEST_MODULES)), help='The test module to run (can be specified more than once for multiple modules). Choices: %s' % ', '.join(sorted(TEST_MODULES))) - parser.add_option('--test-verbosity', type=int, default=4, help='Test verbosity (0-4)') parser.add_option('--test-name', default=[], action='append', help='The name of an individual test to run. Can be specified more than once for multiple tests. The name of the' ' test is the name of the test function without the leading test_. For example, the function test_something()' ' can be run by specifying the name "something".') + parser.add_option('--exclude-test-module', default=[], action='append', type='choice', choices=sorted(map(str, TEST_MODULES)), + help='A test module to be excluded from the test run (can be specified more than once for multiple modules).' + ' Choices: %s' % ', '.join(sorted(TEST_MODULES))) + parser.add_option('--exclude-test-name', default=[], action='append', + help='The name of an individual test to be excluded from the test run. Can be specified more than once for multiple tests.') def run(self, opts): - from calibre.utils.run_tests import run_cli, filter_tests_by_name - tests = find_tests(which_tests=frozenset(opts.test_module)) + from calibre.utils.run_tests import run_cli, filter_tests_by_name, remove_tests_by_name + tests = find_tests(which_tests=frozenset(opts.test_module), exclude_tests=frozenset(opts.exclude_test_module)) if opts.test_name: tests = filter_tests_by_name(tests, *opts.test_name) + if opts.exclude_test_name: + tests = remove_tests_by_name(tests, *opts.exclude_test_name) run_cli(tests, verbosity=opts.test_verbosity) diff --git a/src/calibre/utils/run_tests.py b/src/calibre/utils/run_tests.py index a6cee9bfdd..6e2bb90151 100644 --- a/src/calibre/utils/run_tests.py +++ b/src/calibre/utils/run_tests.py @@ -113,6 +113,14 @@ def filter_tests_by_name(suite, *names): return filter_tests(suite, q) +def remove_tests_by_name(suite, *names): + names = {x if x.startswith('test_') else 'test_' + x for x in names} + + def q(test): + return test._testMethodName not in names + return filter_tests(suite, q) + + def filter_tests_by_module(suite, *names): names = frozenset(names)