Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use better-exceptions in pytest? #128

Open
James4Ever0 opened this issue Jul 9, 2023 · 0 comments
Open

How to use better-exceptions in pytest? #128

James4Ever0 opened this issue Jul 9, 2023 · 0 comments

Comments

@James4Ever0
Copy link

James4Ever0 commented Jul 9, 2023

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant