diff --git a/queue_job/tests/common.py b/queue_job/tests/common.py index cdb2e4e8e3..e1e877b0a2 100644 --- a/queue_job/tests/common.py +++ b/queue_job/tests/common.py @@ -1,5 +1,8 @@ # Copyright 2019 Camptocamp # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). +import doctest +import logging +import sys from contextlib import contextmanager import mock @@ -96,3 +99,51 @@ def test_export(self): delayable = mock.MagicMock(name="DelayableBinding") delayable_cls.return_value = delayable yield delayable_cls, delayable + + +class OdooDocTestCase(doctest.DocTestCase): + """ + We need a custom DocTestCase class in order to: + - define test_tags to run as part of standard tests + - output a more meaningful test name than default "DocTestCase.runTest" + """ + + def __init__(self, doctest, optionflags=0, setUp=None, tearDown=None, checker=None): + super().__init__( + doctest._dt_test, + optionflags=optionflags, + setUp=setUp, + tearDown=tearDown, + checker=checker, + ) + + def setUp(self): + """Log an extra statement which test is started.""" + super(OdooDocTestCase, self).setUp() + logging.getLogger(__name__).info("Running tests for %s", self._dt_test.name) + + +def load_doctests(module): + """ + Generates a tests loading method for the doctests of the given module + https://docs.python.org/3/library/unittest.html#load-tests-protocol + """ + + def load_tests(loader, tests, ignore): + """ + Apply the 'test_tags' attribute to each DocTestCase found by the DocTestSuite. + Also extend the DocTestCase class trivially to fit the class teardown + that Odoo backported for its own test classes from Python 3.8. + """ + if sys.version_info < (3, 8): + doctest.DocTestCase.doClassCleanups = lambda: None + doctest.DocTestCase.tearDown_exceptions = [] + + for test in doctest.DocTestSuite(module): + odoo_test = OdooDocTestCase(test) + odoo_test.test_tags = {"standard", "at_install", "queue_job", "doctest"} + tests.addTest(odoo_test) + + return tests + + return load_tests diff --git a/queue_job/tests/test_runner_channels.py b/queue_job/tests/test_runner_channels.py index 93333fa490..d323d00683 100644 --- a/queue_job/tests/test_runner_channels.py +++ b/queue_job/tests/test_runner_channels.py @@ -1,13 +1,10 @@ # Copyright 2015-2016 Camptocamp SA # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html) -import doctest - # pylint: disable=odoo-addons-relative-import # we are testing, we want to test as we were an external consumer of the API from odoo.addons.queue_job.jobrunner import channels +from .common import load_doctests -def load_tests(loader, tests, ignore): - tests.addTests(doctest.DocTestSuite(channels)) - return tests +load_tests = load_doctests(channels) diff --git a/queue_job/tests/test_runner_runner.py b/queue_job/tests/test_runner_runner.py index 817ac6396e..c6486e27ef 100644 --- a/queue_job/tests/test_runner_runner.py +++ b/queue_job/tests/test_runner_runner.py @@ -1,13 +1,10 @@ # Copyright 2015-2016 Camptocamp SA # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html) -import doctest - # pylint: disable=odoo-addons-relative-import # we are testing, we want to test as we were an external consumer of the API from odoo.addons.queue_job.jobrunner import runner +from .common import load_doctests -def load_tests(loader, tests, ignore): - tests.addTests(doctest.DocTestSuite(runner)) - return tests +load_tests = load_doctests(runner)