diff --git a/AUTHORS b/AUTHORS index b59ebc2a2..68f156862 100644 --- a/AUTHORS +++ b/AUTHORS @@ -216,6 +216,7 @@ Ondřej Súkup Oscar Benjamin Patrick Hayes Pauli Virtanen +Pavel Karateev Paweł Adamczak Pedro Algarvio Philipp Loose diff --git a/changelog/7126.bugfix.rst b/changelog/7126.bugfix.rst new file mode 100644 index 000000000..6817ee211 --- /dev/null +++ b/changelog/7126.bugfix.rst @@ -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. diff --git a/src/_pytest/setuponly.py b/src/_pytest/setuponly.py index c9cc589ff..e18208e59 100644 --- a/src/_pytest/setuponly.py +++ b/src/_pytest/setuponly.py @@ -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() diff --git a/testing/test_setuponly.py b/testing/test_setuponly.py index e26a33dee..779c17fbd 100644 --- a/testing/test_setuponly.py +++ b/testing/test_setuponly.py @@ -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