From cf6e2ceafd8cd6d295d4d6eb379558f8b9f10db0 Mon Sep 17 00:00:00 2001 From: ApaDoctor Date: Wed, 11 Oct 2017 18:11:50 +0200 Subject: [PATCH 1/3] add ini option to disable string escape for parametrization --- AUTHORS | 1 + changelog/2482.feature | 1 + doc/en/parametrize.rst | 15 +++++++++++++++ src/_pytest/python.py | 19 ++++++++++++++++++- 4 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 changelog/2482.feature diff --git a/AUTHORS b/AUTHORS index d6f6ce828..ea6fc5cac 100644 --- a/AUTHORS +++ b/AUTHORS @@ -242,6 +242,7 @@ Vidar T. Fauske Virgil Dupras Vitaly Lashmanov Vlad Dragos +Volodymyr Piskun Wil Cooley William Lee Wim Glenn diff --git a/changelog/2482.feature b/changelog/2482.feature new file mode 100644 index 000000000..37d5138bf --- /dev/null +++ b/changelog/2482.feature @@ -0,0 +1 @@ +Include new ``disable_test_id_escaping_and_forfeit_all_rights_to_community_support`` option to disable ascii-escaping in parametrized values. This may cause a series of problems and as the name makes clear, use at your own risk. diff --git a/doc/en/parametrize.rst b/doc/en/parametrize.rst index 9a237dcd7..912ae1898 100644 --- a/doc/en/parametrize.rst +++ b/doc/en/parametrize.rst @@ -81,6 +81,21 @@ them in turn: test_expectation.py:8: AssertionError ==================== 1 failed, 2 passed in 0.12 seconds ==================== +.. note:: + + pytest by default escapes any non-ascii characters used in unicode strings + for the parametrization because it has several downsides. + If however you would like to use unicode strings in parametrization, use this option in your ``pytest.ini``: + + .. code-block:: ini + + [pytest] + disable_test_id_escaping_and_forfeit_all_rights_to_community_support = True + + to disable this behavior, but keep in mind that this might cause unwanted side effects and + even bugs depending on the OS used and plugins currently installed, so use it at your own risk. + + As designed in this example, only one pair of input/output values fails the simple test function. And as usual with test function arguments, you can see the ``input`` and ``output`` values in the traceback. diff --git a/src/_pytest/python.py b/src/_pytest/python.py index 5b289c7c8..8532837a5 100644 --- a/src/_pytest/python.py +++ b/src/_pytest/python.py @@ -102,6 +102,13 @@ def pytest_addoption(parser): default=["test"], help="prefixes or glob names for Python test function and method discovery", ) + parser.addini( + "disable_test_id_escaping_and_forfeit_all_rights_to_community_support", + type="bool", + default=False, + help="disable string escape non-ascii characters, might cause unwanted " + "side effects(use at your own risk)", + ) group.addoption( "--import-mode", @@ -1156,6 +1163,16 @@ def _find_parametrized_scope(argnames, arg2fixturedefs, indirect): return "function" +def _disable_escaping(val, config=None): + if config is None: + escape_option = False + else: + escape_option = config.getini( + "disable_test_id_escaping_and_forfeit_all_rights_to_community_support" + ) + return val if escape_option else ascii_escaped(val) + + def _idval(val, argname, idx, idfn, item, config): if idfn: try: @@ -1177,7 +1194,7 @@ def _idval(val, argname, idx, idfn, item, config): return hook_id if isinstance(val, STRING_TYPES): - return ascii_escaped(val) + return _disable_escaping(val) elif isinstance(val, (float, int, bool, NoneType)): return str(val) elif isinstance(val, REGEX_TYPE): From 8b0b7156d90ed7e3872f7da2f979665d6d47cf0e Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Sun, 24 Mar 2019 20:44:48 +0900 Subject: [PATCH 2/3] Fix glitches of original patch of disable-test-id-escaping --- changelog/{2482.feature => 2482.feature.rst} | 0 src/_pytest/python.py | 4 ++-- 2 files changed, 2 insertions(+), 2 deletions(-) rename changelog/{2482.feature => 2482.feature.rst} (100%) diff --git a/changelog/2482.feature b/changelog/2482.feature.rst similarity index 100% rename from changelog/2482.feature rename to changelog/2482.feature.rst diff --git a/src/_pytest/python.py b/src/_pytest/python.py index 8532837a5..9e4392264 100644 --- a/src/_pytest/python.py +++ b/src/_pytest/python.py @@ -1163,7 +1163,7 @@ def _find_parametrized_scope(argnames, arg2fixturedefs, indirect): return "function" -def _disable_escaping(val, config=None): +def _ascii_escaped_by_config(val, config): if config is None: escape_option = False else: @@ -1194,7 +1194,7 @@ def _idval(val, argname, idx, idfn, item, config): return hook_id if isinstance(val, STRING_TYPES): - return _disable_escaping(val) + return _ascii_escaped_by_config(val, config) elif isinstance(val, (float, int, bool, NoneType)): return str(val) elif isinstance(val, REGEX_TYPE): From 3d9e68ecfd05dff5efd62627cdbb858f9bc63ec7 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Thu, 28 Mar 2019 00:05:33 +0900 Subject: [PATCH 3/3] Update doc/en/parametrize.rst --- doc/en/parametrize.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/en/parametrize.rst b/doc/en/parametrize.rst index 912ae1898..70a35ac44 100644 --- a/doc/en/parametrize.rst +++ b/doc/en/parametrize.rst @@ -85,14 +85,14 @@ them in turn: pytest by default escapes any non-ascii characters used in unicode strings for the parametrization because it has several downsides. - If however you would like to use unicode strings in parametrization, use this option in your ``pytest.ini``: + If however you would like to use unicode strings in parametrization and see them in the terminal as is (non-escaped), use this option in your ``pytest.ini``: .. code-block:: ini [pytest] disable_test_id_escaping_and_forfeit_all_rights_to_community_support = True - to disable this behavior, but keep in mind that this might cause unwanted side effects and + Keep in mind however that this might cause unwanted side effects and even bugs depending on the OS used and plugins currently installed, so use it at your own risk.