changeset 7752:3929ff3f21c6

tests: introduce doctest_mock_ugettext to allow doctests of localized code Future doctests will require some extra mocking, as the code-under-test uses translation (ugettext aka '_') and its provider TurboGears2 needs a context. Avoid this complexity by mocking ugettext as the identity function. This is done by providing a pytest fixture 'doctest_mock_ugettext' that will mock uggettext in the module that uses the fixture.
author Thomas De Schampheleire <thomas.de_schampheleire@nokia.com>
date Tue, 11 Jun 2019 20:53:33 +0200
parents d4bcbe1b06f4
children d89217cca11a
files conftest.py
diffstat 1 files changed, 13 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/conftest.py	Mon Jun 24 21:42:02 2019 +0200
+++ b/conftest.py	Tue Jun 11 20:53:33 2019 +0200
@@ -22,3 +22,16 @@
     )
     if str(path).endswith(kallithea_ignore_paths):
         return True
+
+@pytest.fixture()
+def doctest_mock_ugettext(request):
+    """Mock ugettext ('_') in the module using this fixture.
+
+    Intended to be used for doctests.
+
+    In a doctest, enable this fixture using:
+        >>> getfixture('doctest_mock_ugettext')
+    """
+    m = __import__(request.module.__name__, globals(), locals(), [None], 0)
+    with mock.patch.object(m, '_', lambda s: s):
+        yield