From 4547e7445d9ce65c69a5f69f74581a6f3aa0aa07 Mon Sep 17 00:00:00 2001 From: ApaDoctor Date: Wed, 11 Oct 2017 18:11:50 +0200 Subject: [PATCH] add ini option to disable string escape for parametrization --- AUTHORS | 1 + _pytest/python.py | 14 +++++++++++++- changelog/2482.feature | 1 + doc/en/parametrize.rst | 15 +++++++++++++++ 4 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 changelog/2482.feature diff --git a/AUTHORS b/AUTHORS index ca282870fb9..9c8215dc73f 100644 --- a/AUTHORS +++ b/AUTHORS @@ -163,5 +163,6 @@ Victor Uriarte Vidar T. Fauske Vitaly Lashmanov Vlad Dragos +Volodymyr Piskun Wouter van Ackooy Xuecong Liao diff --git a/_pytest/python.py b/_pytest/python.py index e10282a8cce..8b4b9bc8b16 100644 --- a/_pytest/python.py +++ b/_pytest/python.py @@ -81,6 +81,10 @@ def pytest_addoption(parser): parser.addini("python_functions", type="args", 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", default="prepend", choices=["prepend", "append"], dest="importmode", @@ -888,6 +892,14 @@ 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 _escape_strings(val) + + def _idval(val, argname, idx, idfn, config=None): if idfn: s = None @@ -909,7 +921,7 @@ def _idval(val, argname, idx, idfn, config=None): return hook_id if isinstance(val, STRING_TYPES): - return _escape_strings(val) + return _disable_escaping(val, config) elif isinstance(val, (float, int, bool, NoneType)): return str(val) elif isinstance(val, REGEX_TYPE): diff --git a/changelog/2482.feature b/changelog/2482.feature new file mode 100644 index 00000000000..897f4dae3fd --- /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. \ No newline at end of file diff --git a/doc/en/parametrize.rst b/doc/en/parametrize.rst index fdd963b1dbe..1b14e58a9ff 100644 --- a/doc/en/parametrize.rst +++ b/doc/en/parametrize.rst @@ -78,6 +78,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.