diff options
| author | Xavier Morel <xmo@odoo.com> | 2020-07-28 12:59:54 +0000 |
|---|---|---|
| committer | Xavier Morel <xmo@odoo.com> | 2020-08-19 14:08:21 +0000 |
| commit | a3ec3229938b122c66c4a3cc859b72726f4874a2 (patch) | |
| tree | 93ad9de9e02164795176bce0981d70a9bcb4120f /odoo/tests/loader.py | |
| parent | c1c43bbe3808c3685e018148b6b6ea242743683c (diff) | |
[IMP] core: tests reporting
* 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>
Diffstat (limited to 'odoo/tests/loader.py')
| -rw-r--r-- | odoo/tests/loader.py | 54 |
1 files changed, 21 insertions, 33 deletions
diff --git a/odoo/tests/loader.py b/odoo/tests/loader.py index 99df113a82e..2d6c2d8275d 100644 --- a/odoo/tests/loader.py +++ b/odoo/tests/loader.py @@ -3,17 +3,14 @@ import inspect import itertools import logging import threading -import time import unittest -import odoo from .. import tools from .common import TagsSelector, OdooSuite -from .runner import OdooTestRunner, OdooTestResult +from .runner import OdooTestResult -# backwards compatibility -_logger = logging.getLogger('odoo.modules.module') +_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() """ @@ -53,45 +50,36 @@ def _get_tests_modules(path, module): 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 -def run_unit_tests(module_name, position='at_install'): - """ - :returns: ``True`` if all of ``module_name``'s tests succeeded, ``False`` - if any of them failed, ``None`` if no tests were ran. - :rtype: bool | None + :param str module_name: module to load tests from + :param str position: "at_install" or "post_install" """ - from ..modules import module + 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 - mods = get_test_modules(module_name) threading.currentThread().testing = True - config_tags = TagsSelector(tools.config['test_tags']) - position_tag = TagsSelector(position) results = OdooTestResult() - for m in mods: - tests = unwrap_suite(unittest.TestLoader().loadTestsFromModule(m)) - suite = OdooSuite(t for t in tests if position_tag.check(t) and config_tags.check(t)) - - if suite.countTestCases(): - t0 = time.time() - t0_sql = odoo.sql_db.sql_counter - _logger.info('%s running tests.', m.__name__) - result = OdooTestRunner().run(suite) - results.update(result) - log_level = logging.INFO - if time.time() - t0 > 5: - log_level = logging.RUNBOT - _logger.log(log_level, "%s ran %s tests in %.2fs, %s queries", m.__name__, result.testsRun, time.time() - t0, odoo.sql_db.sql_counter - t0_sql) - if not result.wasSuccessful(): - _logger.error("Module %s: %d failures, %d errors", module_name, len(result.failures), len(result.errors)) + suite(results) - module.current_test = None 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 |
