pytest-dev / pytest-dev/pytest

Option for pytest.fail to not print exception context

Open
#11,457 3 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

topic: reporting type: proposal
Dominant language
Python
Stars
14.5k
Forks
3.4k
Avg merge
2d 9h
Merged PRs (30d)
35

Description

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 https://github.com/python-trio/pytest-trio/pull/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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with pytest.fail and its pytrace handling, then trace how the resulting exception is reported and how exception context is included. Compare the requested option with the suggested report-hook and exception-manipulation alternatives. Done means a documented, tested way to suppress context while preserving the requested failure reason and pytrace behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.