Skip to content

Commit 36b6384

Browse files
authored
Merge pull request #10384 from tony/showlocals-negation
2 parents 31df38f + 2a33e6a commit 36b6384

File tree

4 files changed

+26
-2
lines changed

4 files changed

+26
-2
lines changed

changelog/10381.improvement.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The ``--no-showlocals`` flag has been added. This can be passed directly to tests to override ``--showlocals`` declared through ``addopts``.

doc/en/how-to/output.rst

+3-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ Examples for modifying traceback printing:
1212

1313
.. code-block:: bash
1414
15-
pytest --showlocals # show local variables in tracebacks
16-
pytest -l # show local variables (shortcut)
15+
pytest --showlocals # show local variables in tracebacks
16+
pytest -l # show local variables (shortcut)
17+
pytest --no-showlocals # hide local variables (if addopts enables them)
1718
1819
pytest --tb=auto # (default) 'long' tracebacks for the first and last
1920
# entry, but 'short' style for the other entries

src/_pytest/terminal.py

+6
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,12 @@ def pytest_addoption(parser: Parser) -> None:
178178
default=False,
179179
help="Show locals in tracebacks (disabled by default)",
180180
)
181+
group._addoption(
182+
"--no-showlocals",
183+
action="store_false",
184+
dest="showlocals",
185+
help="Hide locals in tracebacks (negate --showlocals passed through addopts)",
186+
)
181187
group._addoption(
182188
"--tb",
183189
metavar="style",

testing/test_terminal.py

+16
Original file line numberDiff line numberDiff line change
@@ -998,6 +998,22 @@ def test_showlocals():
998998
]
999999
)
10001000

1001+
def test_noshowlocals_addopts_override(self, pytester: Pytester) -> None:
1002+
pytester.makeini("[pytest]\naddopts=--showlocals")
1003+
p1 = pytester.makepyfile(
1004+
"""
1005+
def test_noshowlocals():
1006+
x = 3
1007+
y = "x" * 5000
1008+
assert 0
1009+
"""
1010+
)
1011+
1012+
# Override global --showlocals for py.test via arg
1013+
result = pytester.runpytest(p1, "--no-showlocals")
1014+
result.stdout.no_fnmatch_line("x* = 3")
1015+
result.stdout.no_fnmatch_line("y* = 'xxxxxx*")
1016+
10011017
def test_showlocals_short(self, pytester: Pytester) -> None:
10021018
p1 = pytester.makepyfile(
10031019
"""

0 commit comments

Comments
 (0)