From e759e4f4a9a7eb05107287aead5164e5290aa509 Mon Sep 17 00:00:00 2001 From: Casey Brooks Date: Thu, 25 Dec 2025 20:58:24 +0000 Subject: [PATCH] fix(setuponly): use saferepr for cached param Refs #24 --- src/_pytest/setuponly.py | 4 +++- testing/test_setup_show_bytes_param.py | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 testing/test_setup_show_bytes_param.py diff --git a/src/_pytest/setuponly.py b/src/_pytest/setuponly.py index c9cc589ffee..0b17e890c7d 100644 --- a/src/_pytest/setuponly.py +++ b/src/_pytest/setuponly.py @@ -1,5 +1,7 @@ import pytest +from _pytest._io.saferepr import saferepr + def pytest_addoption(parser): group = parser.getgroup("debugconfig") @@ -66,7 +68,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=80))) tw.flush() diff --git a/testing/test_setup_show_bytes_param.py b/testing/test_setup_show_bytes_param.py new file mode 100644 index 00000000000..3cf5ec2e311 --- /dev/null +++ b/testing/test_setup_show_bytes_param.py @@ -0,0 +1,21 @@ +import sys + + +def test_setup_show_parametrized_fixture_bytes_no_byteswarning(testdir): + p = testdir.makepyfile( + """ + import pytest + + @pytest.fixture(params=[b"Hello World"]) + def data(request): + return request.param + + def test_data(data): + pass + """ + ) + # Run pytest via the Python interpreter to pass -bb so BytesWarning becomes an error. + result = testdir.run(sys.executable, "-bb", "-m", "pytest", "--setup-show", p) + assert result.ret == 0 + # Verify that the bytes value is displayed using a repr-like format. + result.stdout.fnmatch_lines(["*SETUP F data?b'Hello World'?*"])