pytest-dev / pytest-dev/pytest

Feature request: Add original exception object to pytest_exception_interact

Open
#3,626 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

type: enhancement type: feature-branch
Dominant language
Python
Stars
14.5k
Forks
3.4k
Avg merge
2d 9h
Merged PRs (30d)
35

Description

I'd really like to have a generic exception handler, maybe via a hook. I use a test application and I'd like to raise custom exception and just catch them in one place like so:

def pytest_exception_handler(item, exc):
    if isinstance(exc, KeyError):
        pytest.skip('I knew this was gonna happen!)
    if isinstance(exc, MyException) and 'foo' in item.name:
        raise AnotherGenericException('That foo test is a problem!)

I searched before opening this issue, and couldn't find a way to do this, or even a discussion about this suggestion. If such exist, I apologize.

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 by locating the existing pytest_exception_interact hook and reviewing how the exception argument is currently represented. Compare that entry point with the requested generic handler example, then determine what behavior would make the original exception available and how completion could be verified.

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.