[IMP] run-tests: add --slow-suite

* make --fast-suite, --sanity-checks and --slow-suite exclusive at
  argument parsing time
* split module directly in the argument parser

bzr revid: xmo@openerp.com-20131105135534-kzwph02kwxl77ivf
This commit is contained in:
Xavier Morel 2013-11-05 14:55:34 +01:00
parent baa3743420
commit 5b59611dd3
1 changed files with 60 additions and 52 deletions

View File

@ -2,9 +2,9 @@
Execute the unittest2 tests available in OpenERP addons. Execute the unittest2 tests available in OpenERP addons.
""" """
import os
import sys import sys
import types import types
import argparse
import common import common
@ -56,14 +56,14 @@ def get_test_modules(module, submodule, explode):
print ' ', x print ' ', x
sys.exit(1) sys.exit(1)
fast_suite = getattr(m, 'fast_suite', [])
checks = getattr(m, 'checks', [])
if submodule is None: if submodule is None:
# Use auto-discovered sub-modules. # Use auto-discovered sub-modules.
ms = submodules ms = submodules
elif submodule == '__fast_suite__': elif submodule == '__fast_suite__':
# Obtain the explicit test sub-modules list.
ms = getattr(sys.modules[module], 'fast_suite', None)
# `suite` was used before the 6.1 release instead of `fast_suite`. # `suite` was used before the 6.1 release instead of `fast_suite`.
ms = ms if ms else getattr(sys.modules[module], 'suite', None) ms = fast_suite if fast_suite else getattr(m, 'suite', None)
if ms is None: if ms is None:
if explode: if explode:
print 'The module `%s` has no defined test suite.' % (module,) print 'The module `%s` has no defined test suite.' % (module,)
@ -71,16 +71,18 @@ def get_test_modules(module, submodule, explode):
else: else:
ms = [] ms = []
elif submodule == '__sanity_checks__': elif submodule == '__sanity_checks__':
ms = getattr(sys.modules[module], 'checks', None) ms = checks
if ms is None: if ms is None:
if explode: if explode:
print 'The module `%s` has no defined sanity checks.' % (module,) print 'The module `%s` has no defined sanity checks.' % (module,)
show_submodules_and_exit() show_submodules_and_exit()
else: else:
ms = [] ms = []
elif submodule == '__slow_suite__':
ms = list(set(submodules).difference(fast_suite, checks))
else: else:
# Pick the command-line-specified test sub-module. # Pick the command-line-specified test sub-module.
m = getattr(sys.modules[module], submodule, None) m = getattr(m, submodule, None)
ms = [m] ms = [m]
if m is None: if m is None:
@ -109,10 +111,6 @@ def run(args):
args.addons = args.addons.split(':') args.addons = args.addons.split(':')
else: else:
args.addons = [] args.addons = []
if args.sanity_checks and args.fast_suite:
print 'Only at most one of `--sanity-checks` and `--fast-suite` ' \
'can be specified.'
sys.exit(1)
import logging import logging
openerp.netsvc.init_alternative_logger() openerp.netsvc.init_alternative_logger()
@ -121,43 +119,19 @@ def run(args):
# Install the import hook, to import openerp.addons.<module>. # Install the import hook, to import openerp.addons.<module>.
openerp.modules.module.initialize_sys_path() openerp.modules.module.initialize_sys_path()
# Extract module, submodule from the command-line args. module = args.module
if args.module is None: submodule = args.submodule
module, submodule = None, None
else:
splitted = args.module.split('.')
if len(splitted) == 1:
module, submodule = splitted[0], None
elif len(splitted) == 2:
module, submodule = splitted
else:
print 'The `module` argument must have the form ' \
'`module[.submodule]`.'
sys.exit(1)
# Import the necessary modules and get the corresponding suite. # Import the necessary modules and get the corresponding suite.
if module is None: if module is None:
# TODO # TODO
modules = common.get_addons_from_paths(args.addons, []) # TODO openerp.addons.base is not included ? modules = common.get_addons_from_paths(args.addons, []) # TODO openerp.addons.base is not included ?
test_modules = [] test_modules = []
for module in ['openerp'] + modules: for module in ['openerp'] + modules:
if args.fast_suite: test_modules.extend(
submodule = '__fast_suite__' get_test_modules(module, submodule, explode=False))
if args.sanity_checks:
submodule = '__sanity_checks__'
test_modules.extend(get_test_modules(module,
submodule, explode=False))
else: else:
if submodule and args.fast_suite: test_modules = get_test_modules(module, submodule, explode=True)
print "Submodule name `%s` given, ignoring `--fast-suite`." % (submodule,)
if submodule and args.sanity_checks:
print "Submodule name `%s` given, ignoring `--sanity-checks`." % (submodule,)
if not submodule and args.fast_suite:
submodule = '__fast_suite__'
if not submodule and args.sanity_checks:
submodule = '__sanity_checks__'
test_modules = get_test_modules(module,
submodule, explode=True)
# Run the test suite. # Run the test suite.
if not args.dry_run: if not args.dry_run:
@ -181,21 +155,55 @@ def add_parser(subparsers):
parser.add_argument('-p', '--port', metavar='PORT', parser.add_argument('-p', '--port', metavar='PORT',
help='the port used for WML-RPC tests') help='the port used for WML-RPC tests')
common.add_addons_argument(parser) common.add_addons_argument(parser)
parser.add_argument('-m', '--module', metavar='MODULE',
default=None, parser.add_argument(
help='the module to test in `module[.submodule]` notation. ' '-m', '--module', metavar='MODULE', action=ModuleAction, default=None,
'Use `openerp` for the core OpenERP tests. ' help="the module to test in `module[.submodule]` notation. "
'Leave empty to run every declared tests. ' "Use `openerp` for the core OpenERP tests. "
'Give a module but no submodule to run all the module\'s declared ' "Leave empty to run every declared tests. "
'tests. If both the module and the submodule are given, ' "Give a module but no submodule to run all the module's declared "
'the sub-module can be run even if it is not declared in the module.') "tests. If both the module and the submodule are given, "
parser.add_argument('--fast-suite', action='store_true', "the sub-module can be run even if it is not declared in the module.")
help='run only the tests explicitely declared in the fast suite (this ' group = parser.add_mutually_exclusive_group()
group.add_argument(
'--fast-suite',
dest='submodule', action=GuardAction, nargs=0, const='__fast_suite__',
help='run only the tests explicitly declared in the fast suite (this '
'makes sense only with the bare `module` notation or no module at ' 'makes sense only with the bare `module` notation or no module at '
'all).') 'all).')
parser.add_argument('--sanity-checks', action='store_true', group.add_argument(
'--sanity-checks',
dest='submodule', action=GuardAction, nargs=0, const='__sanity_checks__',
help='run only the sanity check tests') help='run only the sanity check tests')
group.add_argument(
'--slow-suite',
dest='submodule', action=GuardAction, nargs=0, const='__slow_suite__',
help="Only run slow tests (tests which are neither in the fast nor in"
" the sanity suite)")
parser.add_argument('--dry-run', action='store_true', parser.add_argument('--dry-run', action='store_true',
help='do not run the tests') help='do not run the tests')
parser.set_defaults(run=run) parser.set_defaults(run=run, submodule=None)
class ModuleAction(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
split = values.split('.')
if len(split) == 1:
module, submodule = values, None
elif len(split) == 2:
module, submodule = split
else:
raise argparse.ArgumentError(
option_string,
"must have the form 'module[.submodule]', got '%s'" % values)
setattr(namespace, self.dest, module)
if submodule is not None:
setattr(namespace, 'submodule', submodule)
class GuardAction(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
if getattr(namespace, self.dest, None):
print "%s value provided, ignoring '%s'" % (self.dest, option_string)
return
setattr(namespace, self.dest, self.const)