Skip to content

Commit 6c602c2

Browse files
authored
Merge pull request #4995 from youknowone/disble_test_id_escaping
add ini option to disable string escape for parametrization
2 parents 69a55d3 + 3d9e68e commit 6c602c2

File tree

4 files changed

+35
-1
lines changed

4 files changed

+35
-1
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ Vidar T. Fauske
242242
Virgil Dupras
243243
Vitaly Lashmanov
244244
Vlad Dragos
245+
Volodymyr Piskun
245246
Wil Cooley
246247
William Lee
247248
Wim Glenn

changelog/2482.feature.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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.

doc/en/parametrize.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,21 @@ them in turn:
8181
test_expectation.py:8: AssertionError
8282
==================== 1 failed, 2 passed in 0.12 seconds ====================
8383
84+
.. note::
85+
86+
pytest by default escapes any non-ascii characters used in unicode strings
87+
for the parametrization because it has several downsides.
88+
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``:
89+
90+
.. code-block:: ini
91+
92+
[pytest]
93+
disable_test_id_escaping_and_forfeit_all_rights_to_community_support = True
94+
95+
Keep in mind however that this might cause unwanted side effects and
96+
even bugs depending on the OS used and plugins currently installed, so use it at your own risk.
97+
98+
8499
As designed in this example, only one pair of input/output values fails
85100
the simple test function. And as usual with test function arguments,
86101
you can see the ``input`` and ``output`` values in the traceback.

src/_pytest/python.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,13 @@ def pytest_addoption(parser):
102102
default=["test"],
103103
help="prefixes or glob names for Python test function and method discovery",
104104
)
105+
parser.addini(
106+
"disable_test_id_escaping_and_forfeit_all_rights_to_community_support",
107+
type="bool",
108+
default=False,
109+
help="disable string escape non-ascii characters, might cause unwanted "
110+
"side effects(use at your own risk)",
111+
)
105112

106113
group.addoption(
107114
"--import-mode",
@@ -1156,6 +1163,16 @@ def _find_parametrized_scope(argnames, arg2fixturedefs, indirect):
11561163
return "function"
11571164

11581165

1166+
def _ascii_escaped_by_config(val, config):
1167+
if config is None:
1168+
escape_option = False
1169+
else:
1170+
escape_option = config.getini(
1171+
"disable_test_id_escaping_and_forfeit_all_rights_to_community_support"
1172+
)
1173+
return val if escape_option else ascii_escaped(val)
1174+
1175+
11591176
def _idval(val, argname, idx, idfn, item, config):
11601177
if idfn:
11611178
try:
@@ -1177,7 +1194,7 @@ def _idval(val, argname, idx, idfn, item, config):
11771194
return hook_id
11781195

11791196
if isinstance(val, STRING_TYPES):
1180-
return ascii_escaped(val)
1197+
return _ascii_escaped_by_config(val, config)
11811198
elif isinstance(val, (float, int, bool, NoneType)):
11821199
return str(val)
11831200
elif isinstance(val, REGEX_TYPE):

0 commit comments

Comments
 (0)