Qix-/better-exceptions

How to use better-exceptions in pytest?

Opened this issue · 0 comments

Pytest is the most popular testing framework in Python. I want to enhance its error logging, just like #76.

Here's my code:

import better_exceptions
from pytest import ExceptionInfo

def patch(exc_info, exprinfo):
    tb = exc_info[2]
    # max_traceback_limit(tb)
    # traceback is staring from the root cause. deal it in the end.
    # rich.print(tb)
    # breakpoint()
    cls = ExceptionInfo
    textlist = better_exceptions.format_exception(
        exc=exc_info[0], value=exc_info[1], tb=tb)
    # textlist = better_exceptions.format_exception(*exc_info)
    text = "".join(textlist)
    keyword = "in pytest_pyfunc_call"
    text = text.split("\n")
    last_index = -20
    for i, t in enumerate(text):
        if keyword in t:
            last_index = i
            break
    text = text[last_index:]
    text = "\n".join(text)
    print()
    print(text)  # great. this could be the hook.
    return cls(exc_info, text, _ispytest=True)

ExceptionInfo.from_exc_info = patch

This code will not replace the original formatting of exceptions in Pytest, just print its own error info side by side. I want to know if there are better ways to do so, also builtin hook supports?