* remove useless OdooTestRunner * don't log results & time per-file, log a module-level tally instead * add number of tests to post-test results * generate a single test suite per module (see note) * use the previous item to split out the at_install test-running in two steps: generating the suite for the module then running that suite, this way for modules which have no test, or for which all tests have been deselected by test tags, we can avoid some of the setup necessary to prepare for running tests but possibly quite expensive (e.g. `setup_models`) Note: single test suite per module I wanted to stop creating a test result for (essentially) every file in the module, however because of the class-level ``addCleanup``, a TestResult can't be reused by independent suites: In order to run class-level cleanup, the test suite checks between tests if the test it's *preparing* to run is in the same class as the last test it ran, and if not applies the class-level cleanup. The problem is that the "previous test class" is stored on the result object, which is never cleaned up, and the "between tests" check is really performed *before each test*. This means when reusing results across suites it will run the class-level cleanup at the end of one suite and immediately at the start of the next, which will cause issues if class-level cleanups are not idempotent (thankfully ``TestTestCursor`` has a non-idempotent ``tearDownClass` which let me discover the error). Possible fixes are: * don't reuse results * clear the relevant states / attributes between suites * put individual suites in a Big Suite for running The latter seems simpler: just create a single suite for the entire odoo-level module instead of creating one suite per test module. Note to the note: the case of nested suite is taken in account, the "end of suite" cleanup only runs at the end of the top-level suite, so technically we don't have to unwrap suites for *that* purpose, we're doing so in order to filter the test cases inside the suites. But maybe we could integrate this feature to the suites themselves... closes odoo/odoo#55185 Signed-off-by: Xavier Morel (xmo) <xmo@odoo.com>
107 lines
3.4 KiB
Python
107 lines
3.4 KiB
Python
import importlib
|
|
import inspect
|
|
import itertools
|
|
import logging
|
|
import threading
|
|
import unittest
|
|
|
|
from .. import tools
|
|
from .common import TagsSelector, OdooSuite
|
|
from .runner import OdooTestResult
|
|
|
|
|
|
_logger = logging.getLogger(__name__)
|
|
def get_test_modules(module):
|
|
""" Return a list of module for the addons potentially containing tests to
|
|
feed unittest.TestLoader.loadTestsFromModule() """
|
|
# Try to import the module
|
|
results = _get_tests_modules('odoo.addons', module)
|
|
|
|
try:
|
|
importlib.import_module('odoo.upgrade.%s' % module)
|
|
except ImportError:
|
|
pass
|
|
else:
|
|
results += _get_tests_modules('odoo.upgrade', module)
|
|
|
|
return results
|
|
|
|
|
|
def _get_tests_modules(path, module):
|
|
modpath = '%s.%s' % (path, module)
|
|
try:
|
|
mod = importlib.import_module('.tests', modpath)
|
|
except ImportError as e: # will also catch subclass ModuleNotFoundError of P3.6
|
|
# Hide ImportErrors on `tests` sub-module, but display other exceptions
|
|
if e.name == modpath + '.tests' and e.msg.startswith('No module named'):
|
|
return []
|
|
_logger.exception('Can not `import %s`.', module)
|
|
return []
|
|
except Exception as e:
|
|
_logger.exception('Can not `import %s`.', module)
|
|
return []
|
|
if hasattr(mod, 'fast_suite') or hasattr(mod, 'checks'):
|
|
_logger.warning(
|
|
"Found deprecated fast_suite or checks attribute in test module "
|
|
"%s. These have no effect in or after version 8.0.",
|
|
mod.__name__)
|
|
|
|
result = [mod_obj for name, mod_obj in inspect.getmembers(mod, inspect.ismodule)
|
|
if name.startswith('test_')]
|
|
return result
|
|
|
|
def make_suite(module_name, position='at_install'):
|
|
mods = get_test_modules(module_name)
|
|
""" Creates a test suite for all the tests in the specified module,
|
|
filtered by the provided ``position`` and the current test tags
|
|
|
|
:param str module_name: module to load tests from
|
|
:param str position: "at_install" or "post_install"
|
|
"""
|
|
config_tags = TagsSelector(tools.config['test_tags'])
|
|
position_tag = TagsSelector(position)
|
|
return OdooSuite(
|
|
t
|
|
for m in mods
|
|
for t in unwrap_suite(unittest.TestLoader().loadTestsFromModule(m))
|
|
if position_tag.check(t) and config_tags.check(t)
|
|
)
|
|
|
|
def run_suite(suite, module_name):
|
|
# avoid dependency hell
|
|
from ..modules import module
|
|
module.current_test = module_name
|
|
threading.currentThread().testing = True
|
|
|
|
results = OdooTestResult()
|
|
suite(results)
|
|
|
|
threading.currentThread().testing = False
|
|
module.current_test = None
|
|
return results
|
|
|
|
def unwrap_suite(test):
|
|
"""
|
|
Attempts to unpack testsuites (holding suites or cases) in order to
|
|
generate a single stream of terminals (either test cases or customized
|
|
test suites). These can then be checked for run/skip attributes
|
|
individually.
|
|
|
|
An alternative would be to use a variant of @unittest.skipIf with a state
|
|
flag of some sort e.g. @unittest.skipIf(common.runstate != 'at_install'),
|
|
but then things become weird with post_install as tests should *not* run
|
|
by default there
|
|
"""
|
|
if isinstance(test, unittest.TestCase):
|
|
yield test
|
|
return
|
|
|
|
subtests = list(test)
|
|
# custom test suite (no test cases)
|
|
if not len(subtests):
|
|
yield test
|
|
return
|
|
|
|
for item in itertools.chain.from_iterable(unwrap_suite(t) for t in subtests):
|
|
yield item
|