Functions
Approximate comparisons
Use karva.approx() when floating-point results should match within a tolerance.
It uses pytest's defaults: relative tolerance 1e-6 and absolute tolerance
1e-12.
| test.py | |
|---|---|
1 2 3 4 5 6 7 | |
Mappings, ordered sequences, complex numbers, and Decimal values are
supported. Set nan_ok=True to compare NaN values as equal. This is the direct
replacement for pytest.approx() when migrating tests:
| Python | |
|---|---|
1 2 3 4 5 | |
Skip
If you want to skip a test when its running, use karva.skip().
| test.py | |
|---|---|
1 2 3 4 | |
You can optionally provide a reason for skipping the test by passing it as an argument to karva.skip().
| test.py | |
|---|---|
1 2 3 4 5 6 7 | |
You can still use pytest.skip() to skip tests.
Fail
If you want to fail a test when its running, use karva.fail().
| test.py | |
|---|---|
1 2 3 4 | |
You can optionally provide a reason for failing the test by passing it as an argument to karva.fail().
| test.py | |
|---|---|
1 2 3 4 5 6 7 | |
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().
| test.py | |
|---|---|
1 2 3 4 5 | |
You can optionally provide a match parameter to match a regex pattern against the string representation of the exception.
| test.py | |
|---|---|
1 2 3 4 5 | |
You can access the exception info by using the as keyword. The returned object has type, value, and tb properties.
| test.py | |
|---|---|
1 2 3 4 5 6 7 8 9 | |
You can still use pytest.raises() to assert exceptions.