From 2b6622fdd3678c37cdd2fc13f404546ac1a76de6 Mon Sep 17 00:00:00 2001 From: Tony Narlock Date: Fri, 14 Oct 2022 16:10:21 -0500 Subject: [PATCH 1/4] :wrench: Negating --showlocals with --no-showlocals This is necessary for addopts=--showlocals where individual test runs need to not show locals. --- src/_pytest/terminal.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/_pytest/terminal.py b/src/_pytest/terminal.py index 9739a467a66..d967a3ee6f1 100644 --- a/src/_pytest/terminal.py +++ b/src/_pytest/terminal.py @@ -178,6 +178,12 @@ def pytest_addoption(parser: Parser) -> None: default=False, help="Show locals in tracebacks (disabled by default)", ) + group._addoption( + "--no-showlocals", + action="store_false", + dest="showlocals", + help="Hide locals in tracebacks (negate --showlocals passed through addopts)", + ) group._addoption( "--tb", metavar="style", From 3a8d401ac78f2c112a1d78160868f7d2ac09b4c7 Mon Sep 17 00:00:00 2001 From: Tony Narlock Date: Fri, 14 Oct 2022 16:20:33 -0500 Subject: [PATCH 2/4] test(--no-showlocals): Should hide locals when addopts=--showlocals --- testing/test_terminal.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/testing/test_terminal.py b/testing/test_terminal.py index 9a8dd4d9ac9..9de9a85f093 100644 --- a/testing/test_terminal.py +++ b/testing/test_terminal.py @@ -998,6 +998,22 @@ def test_showlocals(): ] ) + def test_noshowlocals_addopts_override(self, pytester: Pytester) -> None: + pytester.makeini("[pytest]\naddopts=--showlocals") + p1 = pytester.makepyfile( + """ + def test_noshowlocals(): + x = 3 + y = "x" * 5000 + assert 0 + """ + ) + + # Override global --showlocals for py.test via arg + result = pytester.runpytest(p1, "--no-showlocals") + result.stdout.no_fnmatch_line("x* = 3") + result.stdout.no_fnmatch_line("y* = 'xxxxxx*") + def test_showlocals_short(self, pytester: Pytester) -> None: p1 = pytester.makepyfile( """ From 7fada7127ed3b7c281164b0364429ce88df53b1a Mon Sep 17 00:00:00 2001 From: Tony Narlock Date: Fri, 14 Oct 2022 16:37:53 -0500 Subject: [PATCH 3/4] docs(output): Note --no-show-locals --- doc/en/how-to/output.rst | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/doc/en/how-to/output.rst b/doc/en/how-to/output.rst index dc3a86ba29d..85081a47c61 100644 --- a/doc/en/how-to/output.rst +++ b/doc/en/how-to/output.rst @@ -12,8 +12,9 @@ Examples for modifying traceback printing: .. code-block:: bash - pytest --showlocals # show local variables in tracebacks - pytest -l # show local variables (shortcut) + pytest --showlocals # show local variables in tracebacks + pytest -l # show local variables (shortcut) + pytest --no-showlocals # hide local variables (if addopts enables them) pytest --tb=auto # (default) 'long' tracebacks for the first and last # entry, but 'short' style for the other entries From 2a33e6ab613f6de1a2cf54f0c4f39dd198729720 Mon Sep 17 00:00:00 2001 From: Tony Narlock Date: Fri, 14 Oct 2022 16:33:45 -0500 Subject: [PATCH 4/4] docs: Update changelog for --no-show-locals --- changelog/10381.improvement.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog/10381.improvement.rst diff --git a/changelog/10381.improvement.rst b/changelog/10381.improvement.rst new file mode 100644 index 00000000000..f979a83b132 --- /dev/null +++ b/changelog/10381.improvement.rst @@ -0,0 +1 @@ +The ``--no-showlocals`` flag has been added. This can be passed directly to tests to override ``--showlocals`` declared through ``addopts``.