Skip to content

Add karva.raises context manager for asserting exceptions#430

Merged
MatthewMckee4 merged 1 commit intomainfrom
add-karva-raises-context-manager
Feb 7, 2026
Merged

Add karva.raises context manager for asserting exceptions#430
MatthewMckee4 merged 1 commit intomainfrom
add-karva-raises-context-manager

Conversation

@MatthewMckee4
Copy link
Member

Summary

  • Add karva.raises(expected_exception, *, match=None) context manager as a native pytest.raises-equivalent
  • Supports exception type matching (including subclasses), optional regex match parameter, and ExceptionInfo access (type, value, tb)
  • Raises FailError for "DID NOT RAISE" and pattern mismatch failures; propagates non-matching exception types

Test plan

  • Matching exception passes test
  • No exception raised fails with "DID NOT RAISE" message
  • match parameter that matches passes
  • match parameter that doesn't match fails
  • Wrong exception type propagates (test fails with traceback)
  • ExceptionInfo fields accessible after block
  • Exception subclass matching works
  • All 406 tests pass (just test)
  • Pre-commit checks pass (prek run -a)

Closes #429

🤖 Generated with Claude Code

Provides a native `pytest.raises`-equivalent so users can assert that code
raises a specific exception without depending on pytest.
@github-actions
Copy link

github-actions bot commented Feb 7, 2026

Project Diff Results

No changes between this PR and main.

@MatthewMckee4 MatthewMckee4 added the test-running Related to running tests with karva label Feb 7, 2026
@MatthewMckee4 MatthewMckee4 merged commit e9668ea into main Feb 7, 2026
16 of 18 checks passed
@MatthewMckee4 MatthewMckee4 deleted the add-karva-raises-context-manager branch February 7, 2026 13:12
@codspeed-hq
Copy link

codspeed-hq bot commented Feb 7, 2026

CodSpeed Performance Report

Merging this PR will not alter performance

Comparing add-karva-raises-context-manager (ab6a0b4) with main (602c87b)

Summary

✅ 1 untouched benchmark

MatthewMckee4 added a commit that referenced this pull request Feb 7, 2026
## Summary
- Add a `## Raises` section to `docs/usage/functions.md` documenting the
`karva.raises` context manager added in #430
- Covers basic usage, the `match` parameter for regex matching,
accessing exception info, and pytest compatibility

## Test plan
- [x] `prek run -a` passes
- [ ] Verify docs render correctly on the docs site
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

test-running Related to running tests with karva

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add karva.raises like pytest.raises

1 participant