Repository: pytest-dev/pytest
File: testing/code/test_excinfo.py (Python)

Code before:
                return 1 / 0
            def func1():
                return 42 + div_by_zero()
            def entry():
                func1()
        """
        )
        excinfo = pytest.raises(ZeroDivisionError, mod.entry)
        p = FormattedExcinfo(style="short")
        reprtb = p.repr_traceback_entry(excinfo.traceback[-3])
        assert len(reprtb.lines) == 1
        assert reprtb.lines[0] == "    func1()"

        reprtb = p.repr_traceback_entry(excinfo.traceback[-2])
        assert len(reprtb.lines) == 2
        assert reprtb.lines[0] == "    return 42 + div_by_zero()"
        assert reprtb.lines[1] == "                ^^^^^^^^^^^^^"

        reprtb = p.repr_traceback_entry(excinfo.traceback[-1])
        assert len(reprtb.lines) == 2
        assert reprtb.lines[0] == "    return 1 / 0"
        assert reprtb.lines[1] == "           ^^^^^"

    def test_repr_tracebackentry_no(self, importasmod):
        mod = importasmod(
            """
            def func1():
                raise ValueError("hello")
            def entry():
                func1()
        """
        )
        excinfo = pytest.raises(ValueError, mod.entry)
        p = FormattedExcinfo(style="no")
        p.repr_traceback_entry(excinfo.traceback[-2])

        p = FormattedExcinfo(style="no")
        reprentry = p.repr_traceback_entry(excinfo.traceback[-1], excinfo)
        lines = reprentry.lines
        assert lines[0] == "E   ValueError: hello"
        assert not lines[1:]

    def test_repr_traceback_tbfilter(self, importasmod):
        mod = importasmod(
            """
            def f(x):
                raise ValueError(x)
            def entry():
                f(0)
        """
        )

Review comment (suggestion):
> It's not the full fledged python traceback.py implementation where we try to differentiate which parts contain operators.

That would be very nice to have, but I'd be happy to merge something without that and open a follow-up issue: this is already a nice improvement on the status quo!

Code after:
    def test_repr_traceback_entry_short_carets(self, importasmod) -> None:
        mod = importasmod(
            """
            def div_by_zero():
                return 1 / 0
            def func1():
                return 42 + div_by_zero()
            def entry():
                func1()
        """
        )
        excinfo = pytest.raises(ZeroDivisionError, mod.entry)
        p = FormattedExcinfo(style="short")
        reprtb = p.repr_traceback_entry(excinfo.traceback[-3])
        assert len(reprtb.lines) == 1
        assert reprtb.lines[0] == "    func1()"

        reprtb = p.repr_traceback_entry(excinfo.traceback[-2])
        assert len(reprtb.lines) == 2
        assert reprtb.lines[0] == "    return 42 + div_by_zero()"
        assert reprtb.lines[1] == "                ^^^^^^^^^^^^^"

        reprtb = p.repr_traceback_entry(excinfo.traceback[-1])
        assert len(reprtb.lines) == 2
        assert reprtb.lines[0] == "    return 1 / 0"
        assert reprtb.lines[1] == "           ^^^^^"

    def test_repr_tracebackentry_no(self, importasmod):
        mod = importasmod(
            """
            def func1():
                raise ValueError("hello")
            def entry():
                func1()
        """
        )
        excinfo = pytest.raises(ValueError, mod.entry)
        p = FormattedExcinfo(style="no")
        p.repr_traceback_entry(excinfo.traceback[-2])

        p = FormattedExcinfo(style="no")
        reprentry = p.repr_traceback_entry(excinfo.traceback[-1], excinfo)
        lines = reprentry.lines
        assert lines[0] == "E   ValueError: hello"
        assert not lines[1:]

    def test_repr_traceback_tbfilter(self, importasmod):
        mod = importasmod(
            """
            def f(x):
                raise ValueError(x)