pytest-dev / pytest-dev/pytest

ExceptionInfo.value not available if pytest.raises fails

Open
#13,509 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

When pytest.raise fails, exc_info.value is not available.

def test():
    try:
        with pytest.raises(AssertionError) as exc_info:
            raise Exception()
    except Exception as e:
        exc_info.value
        pass

I get this error

test_testchecks.py:33: in test
    exc_info.value
/home/dani/.virtualenvs/sc-tools--PFlaRe2/lib/python3.13/site-packages/_pytest/_code/code.py:607: in value
    assert self._excinfo is not None, (
           ^^^^^^^^^^^^^^^^^^^^^^^^^
E   AssertionError: .value can only be used after the context manager exits
pytest            8.4.0
Python 3.13.3
Ubuntu 25.04

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 in _pytest/_code/code.py at ExceptionInfo.value, using the provided pytest.raises example to reproduce the failure. Trace how the context manager handles an unexpected exception and determine the expected availability of exc_info.value when pytest.raises does not match; add or update focused tests to verify the resulting behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.