From ee79c18d80251ea475977af3db7746973a3dbad2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Sun, 15 Dec 2024 14:45:15 +0100 Subject: [PATCH] Support TEST_PYTHONWARNINGS in doctest We restore the doctest_setup to setup the python warnings based on TEST_PYTHONWARNINGS. --- trytond/doc/ref/tests.rst | 10 +--------- trytond/trytond/tests/test_tryton.py | 11 +++++++++-- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/trytond/doc/ref/tests.rst b/trytond/doc/ref/tests.rst index 86286356d4..c762dddf56 100644 --- a/trytond/doc/ref/tests.rst +++ b/trytond/doc/ref/tests.rst @@ -140,17 +140,9 @@ doctest helpers .. function:: doctest_setup - Prepare the run of the `doctest`_ by creating a database and dropping it - beforehand if necessary. + Prepare the run of the doctest_. This function should be used as the ``setUp`` parameter. - .. deprecated:: 4.2 - - The ``doctest_setup`` function should not be used anymore to set up - :py:func:`~doctest.DocFileSuite`. - New modules should use :func:`~trytond.tests.tools.activate_modules` - instead. - .. function:: doctest_teardown() Clean up after the run of the doctest_ by dropping the database. diff --git a/trytond/trytond/tests/test_tryton.py b/trytond/trytond/tests/test_tryton.py index df14f06371..e639a91861 100644 --- a/trytond/trytond/tests/test_tryton.py +++ b/trytond/trytond/tests/test_tryton.py @@ -1208,11 +1208,17 @@ def drop_create(name=DB_NAME, lang='en'): def doctest_setup(test): - return drop_create() + if pythonwarnings := os.getenv('TEST_PYTHONWARNINGS'): + cm = warnings.catch_warnings() + cm.__enter__() + test._tryton_cleanup = [(cm.__exit__, cm, None, None, None)] + TestCase.setUpClassWarning(pythonwarnings.split(',')) def doctest_teardown(test): unittest.mock.patch.stopall() + for cleanup in getattr(test, '_tryton_cleanup', []): + cleanup[0](cleanup[1:]) return drop_db() @@ -1266,7 +1272,8 @@ def load_doc_tests(name, path, loader, tests, pattern, skips=None): for globs in configs: tests.addTests(doctest.DocFileSuite( scenario, package=name, globs=globs, - tearDown=doctest_teardown, encoding='utf-8', + setUp=doctest_setup, tearDown=doctest_teardown, + encoding='utf-8', checker=doctest_checker, optionflags=s_optionflags)) finally: