diff --git a/AUTHORS b/AUTHORS index 3289b9913..bd8ee5453 100644 --- a/AUTHORS +++ b/AUTHORS @@ -208,6 +208,7 @@ Omer Hadari Ondřej Súkup Oscar Benjamin Patrick Hayes +Pauli Virtanen Paweł Adamczak Pedro Algarvio Philipp Loose diff --git a/changelog/6384.improvement.rst b/changelog/6384.improvement.rst new file mode 100644 index 000000000..75d1e605d --- /dev/null +++ b/changelog/6384.improvement.rst @@ -0,0 +1 @@ +Make `--showlocals` work also with `--tb=short`. diff --git a/src/_pytest/_code/code.py b/src/_pytest/_code/code.py index d1a8ec2f1..9c8702d17 100644 --- a/src/_pytest/_code/code.py +++ b/src/_pytest/_code/code.py @@ -785,9 +785,7 @@ class FormattedExcinfo: message = excinfo and excinfo.typename or "" path = self._makepath(entry.path) filelocrepr = ReprFileLocation(path, entry.lineno + 1, message) - localsrepr = None - if not short: - localsrepr = self.repr_locals(entry.locals) + localsrepr = self.repr_locals(entry.locals) return ReprEntry(lines, reprargs, localsrepr, filelocrepr, style) if excinfo: lines.extend(self.get_exconly(excinfo, indent=4)) @@ -1044,6 +1042,8 @@ class ReprEntry(TerminalRepr): for line in self.lines: red = line.startswith("E ") tw.line(line, bold=True, red=red) + if self.reprlocals: + self.reprlocals.toterminal(tw, indent=" " * 8) return if self.reprfuncargs: self.reprfuncargs.toterminal(tw) @@ -1085,9 +1085,9 @@ class ReprLocals(TerminalRepr): def __init__(self, lines: Sequence[str]) -> None: self.lines = lines - def toterminal(self, tw) -> None: + def toterminal(self, tw, indent="") -> None: for line in self.lines: - tw.line(line) + tw.line(indent + line) class ReprFuncArgs(TerminalRepr): diff --git a/testing/test_terminal.py b/testing/test_terminal.py index c109d2c78..3b149d82a 100644 --- a/testing/test_terminal.py +++ b/testing/test_terminal.py @@ -670,6 +670,26 @@ class TestTerminalFunctional: ] ) + def test_showlocals_short(self, testdir): + p1 = testdir.makepyfile( + """ + def test_showlocals_short(): + x = 3 + y = "xxxx" + assert 0 + """ + ) + result = testdir.runpytest(p1, "-l", "--tb=short") + result.stdout.fnmatch_lines( + [ + "test_showlocals_short.py:*", + " assert 0", + "E assert 0", + " x = 3", + " y = 'xxxx'", + ] + ) + @pytest.fixture def verbose_testfile(self, testdir): return testdir.makepyfile(