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

Option for pytest.fail to not print exception context #11457

Open
VincentVanlaer opened this issue Sep 20, 2023 · 0 comments · May be fixed by #11669
Open

Option for pytest.fail to not print exception context #11457

VincentVanlaer opened this issue Sep 20, 2023 · 0 comments · May be fixed by #11669
Labels
topic: reporting related to terminal output and user-facing messages and errors type: proposal proposal for a new feature, often to gather opinions or design the API around the new feature

Comments

@VincentVanlaer
Copy link

Context

Currently, one of the ways to use pytest.fail is to include all of the necessary information in the reason field and disabling the traceback with pytrace = False. While this does suppress tracebacks of the exception itself and any exceptions in the context, it still prints the context. I use this in python-trio/pytest-trio#136 for example to report full tracebacks when a timeout happens (capturing all async tasks). Currently, this implementation prints the set of tracebacks twice as an internal exception (which also contains the tracebacks) is converted to a test failure with pytest.fail.

Feature request

I would like to have a way to suppress this context as well, either by default if pytrace = False is set, or with another option. This is the behavior I would have expected from pytrace in the first place.

Alternatives

It is possible to do some juggling with the exceptions to remove the context as follows:

import pytest


def test_fail():
    try:
        import some_missing_package
    except ImportError:
        try:
            pytest.fail(reason="You need to install some_missing_package for this test to succeed", pytrace=False)
        except BaseException as e:
            e.__cause__ = None
            raise

I think this would be reasonable to implement in a plugin for example, but might be a bit to cumbersome/obscure for use in tests. But that is not where my use case lies, so this might a case of the XY problem. Another solution would be to use a report hook to customize the reporting of certain special exceptions.

It might be that these alternatives are acceptable (I presume that this case is not a common occurrence) and adding this feature is not worth the maintenance/testing/change in behavior. But I believe it is still worth considering at least.

@Zac-HD Zac-HD added type: proposal proposal for a new feature, often to gather opinions or design the API around the new feature topic: reporting related to terminal output and user-facing messages and errors labels Oct 1, 2023
@seanpourgoutzidis seanpourgoutzidis linked a pull request Dec 5, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: reporting related to terminal output and user-facing messages and errors type: proposal proposal for a new feature, often to gather opinions or design the API around the new feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants