Skip to content

Surround with pytest.raises()

Surround with pytest.raises() wraps selected test statements in a with pytest.raises(Exception): block.

How to use it

Select one or more statements in a pytest test, then use the Python surround action and choose pytest.raises().

When it helps

Use this surrounder when converting a failing statement into an explicit exception assertion while keeping indentation and imports handled for you.

Notes

  • BetterPy adds a pytest import when needed.
  • The surrounder is available only in pytest test functions and avoids wrapping code already inside pytest.raises.
  • The feature is gated by the Surround with pytest.raises() setting under Settings | BetterPy | Testing (Pytest) | Assertions & Test Editing.