Skip to content

Other 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
import karva

def test_total():
    assert 0.1 + 0.2 == karva.approx(0.3)

def test_coordinates():
    assert [0.1000001, 0.2] == karva.approx([0.1, 0.2], rel=1e-5)

Mappings, ordered sequences, complex numbers, and Decimal values are supported. Nonnumeric values use strict equality, including inside mappings and ordered sequences. Set nan_ok=True to compare NaN values as equal. Use it as a direct replacement for pytest.approx() when migrating tests:

Python
1
2
3
4
5
# pytest
assert value == pytest.approx(expected)

# Karva
assert value == karva.approx(expected)

Skip

If you want to skip a test when its running, use karva.skip().

test.py
1
2
3
4
import karva

def test_function():
    karva.skip()

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
import karva

def test_function():
    karva.skip("This test is not ready yet")

def test_function2():
    karva.skip(reason="This test is not ready yet")

You can still use pytest.skip() to skip tests.

Import or skip

Use karva.importorskip() for tests that need an optional dependency. It returns the imported module when available and skips the module when the dependency is missing.

test.py
1
2
3
4
5
6
import karva

numpy = karva.importorskip("numpy")

def test_array():
    assert numpy.array([1, 2]).tolist() == [1, 2]

Pass reason to control the skip diagnostic. Missing modules are skipped by default; import errors raised inside an installed module remain failures unless exc_type=ImportError is passed.

Fail

If you want to fail a test when its running, use karva.fail().

test.py
1
2
3
4
import karva

def test_function():
    karva.fail()

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
import karva

def test_function():
    karva.fail("This test is not ready yet")

def test_function2():
    karva.fail(reason="This test is not ready yet")

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
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.

test.py
1
2
3
4
5
import karva

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

Use check to validate structured exception attributes after the type and match checks pass. The callback receives the raised exception and must return True for the context to succeed.

test.py
1
2
3
4
5
6
import errno
import karva

def test_permission_error():
    with karva.raises(OSError, check=lambda error: error.errno == errno.EACCES):
        raise OSError(errno.EACCES, "permission denied")

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
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.