changeset 4d762925e2e2 in trytond:default
details: https://hg.tryton.org/trytond?cmd=changeset&node=4d762925e2e2
description:
        Support testNamePatterns for doc tests

        issue11423
        review382481002
diffstat:

 doc/ref/tests.rst            |   8 ++++++++
 trytond/tests/test_tryton.py |  27 ++++++++++++++++++++++++++-
 2 files changed, 34 insertions(+), 1 deletions(-)

diffs (69 lines):

diff -r 171cdfb43730 -r 4d762925e2e2 doc/ref/tests.rst
--- a/doc/ref/tests.rst Fri Apr 22 18:32:25 2022 +0200
+++ b/doc/ref/tests.rst Mon Apr 25 18:11:21 2022 +0200
@@ -81,6 +81,14 @@
 
    A specialized doctest checker to ensure the Python compatibility.
 
+
+.. function:: load_doc_tests(name, path, loader, tests, pattern)
+
+   An helper that follows the ``load_tests`` protocol to load as
+   :py:class:`~doctest.DocTest` all ``*.rst`` files in ``directory``,
+   with the module ``name`` and the ``path`` to the module file from which the
+   doc tests are registered.
+
 .. function:: suite()
 
    A function returning a subclass of ``unittest.TestSuite`` that drops the
diff -r 171cdfb43730 -r 4d762925e2e2 trytond/tests/test_tryton.py
--- a/trytond/tests/test_tryton.py      Fri Apr 22 18:32:25 2022 +0200
+++ b/trytond/tests/test_tryton.py      Mon Apr 25 18:11:21 2022 +0200
@@ -12,6 +12,7 @@
 import unittest
 import unittest.mock
 from configparser import ConfigParser
+from fnmatch import fnmatchcase
 from functools import reduce, wraps
 from itertools import chain
 
@@ -37,7 +38,7 @@
 
 __all__ = ['DB_NAME', 'USER', 'CONTEXT',
     'activate_module', 'ModuleTestCase', 'with_transaction',
-    'doctest_setup', 'doctest_teardown', 'doctest_checker']
+    'doctest_setup', 'doctest_teardown', 'doctest_checker', 'load_doc_tests']
 
 Pool.start()
 USER = 1
@@ -994,6 +995,30 @@
 doctest_checker = doctest.OutputChecker()
 
 
+def load_doc_tests(name, path, loader, tests, pattern):
+    def shouldIncludeScenario(path):
+        return (
+            loader.testNamePatterns is None
+            or any(
+                fnmatchcase(path, pattern)
+                for pattern in loader.testNamePatterns))
+    directory = os.path.dirname(path)
+    # TODO: replace by glob root_dir in Python 3.10
+    cwd = os.getcwd()
+    try:
+        os.chdir(directory)
+        for scenario in filter(
+                shouldIncludeScenario, glob.glob('*.rst')):
+            tests.addTests(doctest.DocFileSuite(
+                    scenario, package=name,
+                    tearDown=doctest_teardown, encoding='utf-8',
+                    checker=doctest_checker,
+                    optionflags=doctest.REPORT_ONLY_FIRST_FAILURE))
+    finally:
+        os.chdir(cwd)
+    return tests
+
+
 class TestSuite(unittest.TestSuite):
     def run(self, *args, **kwargs):
         while True:

Reply via email to