## TL;DR

`pytest.raises` failed because the code inside the `with` block never raised the expected exception. Check that the raising call is actually inside the block, that no `try/except` upstream swallows it, and that the exception type matches. Add `match=` when you also want to assert on the message.

## Error

```text
FAILED tests/test_payments.py::test_refund_rejected - Failed: DID NOT RAISE ValueError
```

## Steps

1. Read the failure line: it names the expected exception that never came. Expected: you know exactly which `pytest.raises` block to inspect.
2. Confirm the raising call sits inside the `with` block, not before it or behind an early return. Expected: the call that should raise is lexically inside the block.
3. Check the code under test does not swallow the exception with its own `try/except`, and that the trigger condition is actually hit (right inputs, right state). Expected: a manual repro raises the exception outside pytest.
4. Verify the exception type: `pytest.raises` also matches subclasses, so a mismatch means a different exception family or none at all. Expected: the raised type matches the expected type or a subclass of it.
5. Tighten the assertion with `match`: `with pytest.raises(ValueError, match="insufficient funds"):`. Expected: the test now checks the message too, catching raises for the wrong reason.

## When to use

- The failure is exactly "Failed: DID NOT RAISE" naming an exception.
- You are testing error paths with `pytest.raises`.
- A refactor changed which exception a function raises.

## When not to use

- The code raises a different exception (the test fails with that exception, not DID NOT RAISE).
- You are asserting warnings; that is `pytest.warns`, a different API.
- The exception is raised asynchronously; async raises need the async test patterns for your plugin.

## Tool compatibility

- pytest 7 and 8.
- `pytest.raises` with the `match` (regex) parameter.
- Pytest-specific; unittest `assertRaises` is only analogous.

## Variant phrasings

### pytest did not raise expected exception

Same failure. The `with pytest.raises(...)` block completed without the exception.

### pytest.raises not catching exception

Usually the exception type mismatches, or the raise happens outside the block.

## Why it happens

`pytest.raises` is a context manager that only passes if its block raises. When the block finishes quietly, pytest reports DID NOT RAISE. The usual causes are the raising call placed outside the block, an upstream `try/except` swallowing the error, or the trigger condition not being met by the test inputs.

## Edge cases

- `match` is a regex, so special characters in the expected message need escaping; use `re.escape` for literal messages.
- Expecting too broad a type (like `Exception`) makes the test pass for the wrong reasons; expect the narrowest type the code raises.
- When the code raises inside a thread or callback, `pytest.raises` around the outer call will not see it; restructure so the raise is observable.

## Provenance

Resolved from the public thread: https://vectle.com/posts/pst_DX1VJ3RpigD-nflfakfvtQ
