From 8b6232ed25d8304ed813e7fdfd95fe1e462a7283 Mon Sep 17 00:00:00 2001 From: Raphael Collet Date: Wed, 10 Nov 2021 15:12:10 +0000 Subject: [PATCH] [FIX] tests: allow OdooSuite to handle unittest.TestCase without crashing closes odoo/odoo#79713 X-original-commit: 6ae762f1e4a7f3eb5320d28428310905961dfad3 Signed-off-by: Raphael Collet (rco) --- odoo/addons/base/tests/__init__.py | 1 + odoo/addons/base/tests/test_test_suite.py | 12 +++++++++ odoo/tests/common.py | 30 ++++++++++++----------- 3 files changed, 29 insertions(+), 14 deletions(-) create mode 100644 odoo/addons/base/tests/test_test_suite.py diff --git a/odoo/addons/base/tests/__init__.py b/odoo/addons/base/tests/__init__.py index 41312bff550..0c72603cb85 100644 --- a/odoo/addons/base/tests/__init__.py +++ b/odoo/addons/base/tests/__init__.py @@ -43,6 +43,7 @@ from . import test_res_partner from . import test_res_partner_bank from . import test_res_users from . import test_reports +from . import test_test_suite from . import test_tests_tags from . import test_form_create from . import test_cloc diff --git a/odoo/addons/base/tests/test_test_suite.py b/odoo/addons/base/tests/test_test_suite.py new file mode 100644 index 00000000000..893d94a04f8 --- /dev/null +++ b/odoo/addons/base/tests/test_test_suite.py @@ -0,0 +1,12 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from unittest import TestCase + +from odoo.tests import MetaCase + + +class TestTestSuite(TestCase, metaclass=MetaCase): + + def test_test_suite(self): + """ Check that OdooSuite handles unittest.TestCase correctly. """ diff --git a/odoo/tests/common.py b/odoo/tests/common.py index 4fcff4f0e82..2ac423f85a4 100644 --- a/odoo/tests/common.py +++ b/odoo/tests/common.py @@ -228,12 +228,13 @@ class OdooSuite(unittest.suite.TestSuite): finally: unittest.suite._call_if_exists(result, '_restoreStdout') if currentClass._classSetupFailed is True: - currentClass.doClassCleanups() - if len(currentClass.tearDown_exceptions) > 0: - for exc in currentClass.tearDown_exceptions: - self._createClassOrModuleLevelException( - result, exc[1], 'setUpClass', className, - info=exc) + if hasattr(currentClass, 'doClassCleanups'): + currentClass.doClassCleanups() + if len(currentClass.tearDown_exceptions) > 0: + for exc in currentClass.tearDown_exceptions: + self._createClassOrModuleLevelException( + result, exc[1], 'setUpClass', className, + info=exc) def _createClassOrModuleLevelException(self, result, exc, method_name, parent, info=None): errorName = f'{method_name} ({parent})' @@ -276,14 +277,15 @@ class OdooSuite(unittest.suite.TestSuite): className) finally: unittest.suite._call_if_exists(result, '_restoreStdout') - previousClass.doClassCleanups() - if len(previousClass.tearDown_exceptions) > 0: - for exc in previousClass.tearDown_exceptions: - className = unittest.util.strclass(previousClass) - self._createClassOrModuleLevelException(result, exc[1], - 'tearDownClass', - className, - info=exc) + if hasattr(previousClass, 'doClassCleanups'): + previousClass.doClassCleanups() + if len(previousClass.tearDown_exceptions) > 0: + for exc in previousClass.tearDown_exceptions: + className = unittest.util.strclass(previousClass) + self._createClassOrModuleLevelException(result, exc[1], + 'tearDownClass', + className, + info=exc) class MetaCase(type):