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..1a85e8997 --- /dev/null +++ b/changelog/7126.bugfix.rst @@ -0,0 +1,2 @@ +``--setup-show`` now doesn't raise an error when a bytes value is used as a ``parametrize`` +parameter when Python is called with the ``-bb`` flag. diff --git a/src/_pytest/setuponly.py b/src/_pytest/setuponly.py index c9cc589ff..9e4cd9519 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,7 @@ def _show_fixture_action(fixturedef, msg): tw.write(" (fixtures used: {})".format(", ".join(deps))) if hasattr(fixturedef, "cached_param"): - tw.write("[{}]".format(fixturedef.cached_param)) + tw.write("[{}]".format(saferepr(fixturedef.cached_param, maxsize=42))) tw.flush() diff --git a/testing/test_setuponly.py b/testing/test_setuponly.py index e26a33dee..221c32a31 100644 --- a/testing/test_setuponly.py +++ b/testing/test_setuponly.py @@ -1,3 +1,5 @@ +import sys + import pytest from _pytest.config import ExitCode @@ -146,10 +148,10 @@ def test_show_fixtures_with_parameters(testdir, mode): result.stdout.fnmatch_lines( [ - "SETUP S arg_same?foo?", - "TEARDOWN S arg_same?foo?", - "SETUP S arg_same?bar?", - "TEARDOWN S arg_same?bar?", + "SETUP S arg_same?'foo'?", + "TEARDOWN S arg_same?'foo'?", + "SETUP S arg_same?'bar'?", + "TEARDOWN S arg_same?'bar'?", ] ) @@ -179,7 +181,7 @@ def test_show_fixtures_with_parameter_ids(testdir, mode): assert result.ret == 0 result.stdout.fnmatch_lines( - ["SETUP S arg_same?spam?", "SETUP S arg_same?ham?"] + ["SETUP S arg_same?'spam'?", "SETUP S arg_same?'ham'?"] ) @@ -198,7 +200,9 @@ def test_show_fixtures_with_parameter_ids_function(testdir, mode): result = testdir.runpytest(mode, p) assert result.ret == 0 - result.stdout.fnmatch_lines(["*SETUP F foobar?FOO?", "*SETUP F foobar?BAR?"]) + result.stdout.fnmatch_lines( + ["*SETUP F foobar?'FOO'?", "*SETUP F foobar?'BAR'?"] + ) def test_dynamic_fixture_request(testdir): @@ -292,3 +296,20 @@ def test_setup_show_with_KeyboardInterrupt_in_test(testdir): ] ) assert result.ret == ExitCode.INTERRUPTED + + +def test_show_fixture_action_with_bytes(testdir): + # Issue 7126, BytesWarning when using --setup-show with bytes parameter + 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