Fix #7126 - saferepr for bytes params

bytes parametrize parameters cause error when --setup-show is used
and Python is called with -bb flag
This commit is contained in:
Pavel Karateev 2020-05-09 13:57:17 +03:00
parent 741a8b8023
commit 903e2ab6ee
4 changed files with 28 additions and 1 deletions

View File

@ -216,6 +216,7 @@ Ondřej Súkup
Oscar Benjamin
Patrick Hayes
Pauli Virtanen
Pavel Karateev
Paweł Adamczak
Pedro Algarvio
Philipp Loose

View File

@ -0,0 +1,3 @@
Use ``saferepr`` to format bytes ``parametrize`` parameters for ``--setup-show``
output to prevent errors when Python is called with ``-bb`` to catch bytearray with
unicode comparison.

View File

@ -1,4 +1,5 @@
import pytest
from _pytest._io.saferepr import saferepr
def pytest_addoption(parser):
@ -66,7 +67,11 @@ def _show_fixture_action(fixturedef, msg):
tw.write(" (fixtures used: {})".format(", ".join(deps)))
if hasattr(fixturedef, "cached_param"):
tw.write("[{}]".format(fixturedef.cached_param))
if isinstance(fixturedef.cached_param, bytes):
param = saferepr(fixturedef.cached_param, maxsize=42)
else:
param = fixturedef.cached_param
tw.write("[{}]".format(param))
tw.flush()

View File

@ -1,3 +1,5 @@
import sys
import pytest
from _pytest.config import ExitCode
@ -292,3 +294,19 @@ def test_setup_show_with_KeyboardInterrupt_in_test(testdir):
]
)
assert result.ret == ExitCode.INTERRUPTED
def test_parametrize_no_comparing_bytearray_error(testdir):
test_file = testdir.makepyfile(
"""
import pytest
@pytest.mark.parametrize('data', [b'Hello World'])
def test_data(data):
pass
"""
)
result = testdir.run(
sys.executable, "-bb", "-m", "pytest", "--setup-show", str(test_file)
)
assert result.ret == 0