Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions docs/usage/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,41 @@ def test_function2():
Then running `uv run karva test` will result in two test fails.

You can still use `pytest.fail()` to fail tests.

## Raises

If you want to assert that a block of code raises a specific exception, use `karva.raises()`.

```python title="test.py"
import karva

def test_function():
with karva.raises(ValueError):
raise ValueError("something went wrong")
```

You can optionally provide a `match` parameter to match a regex pattern against the string representation of the exception.

```python title="test.py"
import karva

def test_function():
with karva.raises(ValueError, match="something"):
raise ValueError("something went wrong")
```

You can access the exception info by using the `as` keyword. The returned object has `type`, `value`, and `tb` properties.

```python title="test.py"
import karva

def test_function():
with karva.raises(ValueError) as exc_info:
raise ValueError("something went wrong")

assert exc_info.type is ValueError
assert str(exc_info.value) == "something went wrong"
assert exc_info.tb is not None
```

You can still use `pytest.raises()` to assert exceptions.
Loading