# HG changeset patch # User Thomas De Schampheleire # Date 2019-06-11 20:53:33 # Node ID 3929ff3f21c6ab30f248770afd94762c8b10da68 # Parent d4bcbe1b06f4431212ea7bf3fd84b6ac8ea0426c 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. diff --git a/conftest.py b/conftest.py --- a/conftest.py +++ b/conftest.py @@ -22,3 +22,16 @@ def pytest_ignore_collect(path): ) 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