From b79935612f2b7510a9f9e10c574a04d85754d422 Mon Sep 17 00:00:00 2001 From: Sorin Sbarnea Date: Thu, 15 Dec 2022 09:04:05 +0000 Subject: [PATCH] Allow users to override the hardcoded repr maxsize value This change should enable users to increase the size of the error messages without being forced to increase the verbosity level, which has lost of side-effects. --- changelog/10589.feature.rst | 2 ++ doc/en/reference/reference.rst | 5 +++++ src/_pytest/_io/saferepr.py | 3 ++- 3 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 changelog/10589.feature.rst diff --git a/changelog/10589.feature.rst b/changelog/10589.feature.rst new file mode 100644 index 000000000..b4828f1e0 --- /dev/null +++ b/changelog/10589.feature.rst @@ -0,0 +1,2 @@ +Allow users to override repr maxsize value (240) by defining +:envvar:`PYTEST_DEFAULT_REPR_MAX_SIZE` environment variable. diff --git a/doc/en/reference/reference.rst b/doc/en/reference/reference.rst index 25d76568a..2a380d33f 100644 --- a/doc/en/reference/reference.rst +++ b/doc/en/reference/reference.rst @@ -1061,6 +1061,11 @@ processes can inspect it, see :ref:`pytest current test env` for more informatio When set, pytest will print tracing and debug information. +.. envvar:: PYTEST_DEFAULT_REPR_MAX_SIZE + +Setting this would override the implicit 240 chars limit used for truncating +longer error messages. + .. envvar:: PYTEST_DISABLE_PLUGIN_AUTOLOAD When set, disables plugin auto-loading through setuptools entrypoints. Only explicitly specified plugins will be diff --git a/src/_pytest/_io/saferepr.py b/src/_pytest/_io/saferepr.py index c70187223..de7b7f6ab 100644 --- a/src/_pytest/_io/saferepr.py +++ b/src/_pytest/_io/saferepr.py @@ -1,3 +1,4 @@ +import os import pprint import reprlib from typing import Any @@ -96,7 +97,7 @@ def safeformat(obj: object) -> str: # Maximum size of overall repr of objects to display during assertion errors. -DEFAULT_REPR_MAX_SIZE = 240 +DEFAULT_REPR_MAX_SIZE = int(os.environ.get("PYTEST_DEFAULT_REPR_MAX_SIZE", 240)) def saferepr(