Builtin
Karva provides a set of built-in fixtures that can be used in your tests without any setup. They are all compatible with their pytest counterparts, so existing pytest tests can use them unchanged.
We will try to add more built-in fixtures from pytest in the future.
Temporary Directory
This fixture provides the user with a pathlib.Path object that points to a temporary directory.
You can use any of the following fixture names:
tmp_path(from pytest)tmpdir(from pytest)temp_path(from karva)temp_dir(from karva)
| test.py | |
|---|---|
1 2 | |
Temporary Directory Factory
A session-scoped factory for creating temporary directories. Use this when you need to allocate a temporary directory from a session, package, or module-scoped fixture — tmp_path itself is function-scoped and cannot be consumed by longer-lived fixtures.
You can use any of the following fixture names:
tmp_path_factory(from pytest) — returnspathlib.Pathobjects.tmpdir_factory(from pytest) — returnspy.path.localobjects.
The factory has two methods: mktemp(name) creates a fresh numbered subdirectory under the session's base temp directory, and getbasetemp() returns that base directory.
| test.py | |
|---|---|
1 2 3 4 5 6 7 8 9 10 | |
Mock Environment
This fixture allows you to safely modify environment variables, and the system path during tests. All changes are automatically undone after the test completes.
You can use any of the following fixture names:
monkeypatch(from pytest)
This fixture is compatible with pytest's monkeypatch fixture.
| test.py | |
|---|---|
1 2 3 4 5 6 7 8 9 | |
The fixture provides all of these helper methods:
| Python | |
|---|---|
1 2 3 4 5 6 7 8 | |
The raising parameter determines whether or not a KeyError or AttributeError is raised when the attribute or item does not exist when trying to set / delete it.
Simple Example
Consider a scenario where you are working with user configuration and you need to mock their cache directory.
| test.py | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 | |
Reusing Mocks
we can share mocks across multiple functions without having to rerun the mocking functions by using fixture.
See this example where instead of requesting the monkeypatch fixture, we can reuse the mock_response fixture.
This lets us move the patching logic to another function and reuse the mock_response fixture across multiple tests.
| Python | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | |
Mocking Environment Variables
If you are working with environment variables, you often need to modify them when testing.
See the example on how this could be useful.
| Python | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | |
See the pytest documentation for more information.
Capturing Log Records
The caplog fixture captures log records emitted during a test. It is function-scoped and resets between tests, so each test sees a clean slate.
Use caplog.at_level(level) as a context manager to enable capture at a given level for a block, or caplog.set_level(level) to enable capture for the remainder of the test. Captured records are exposed as caplog.records (a list of logging.LogRecord), caplog.messages (the formatted messages only), caplog.record_tuples (tuples of (logger_name, levelno, message)), and caplog.text (the full formatted text). Call caplog.clear() to drop any records captured so far.
| test.py | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 | |
| test.py | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
Capturing Standard Output and Standard Error
The capsys fixture captures writes to sys.stdout and sys.stderr at the Python level. Call capsys.readouterr() to retrieve everything written since the last call; the return value has .out and .err string attributes, and the buffers are reset after each read.
| test.py | |
|---|---|
1 2 3 4 5 | |
| test.py | |
|---|---|
1 2 3 4 5 6 7 8 | |
Log messages emitted during the test are also routed through the captured streams, so you can assert on them from captured.err:
| test.py | |
|---|---|
1 2 3 4 5 6 7 | |
Use capsys.disabled() as a context manager to temporarily restore the real sys.stdout and sys.stderr inside a test — anything written while capture is disabled goes straight to the terminal instead of being captured:
| test.py | |
|---|---|
1 2 3 4 5 6 7 | |
Capturing File Descriptors
The capfd fixture is identical in shape to capsys, but it captures output at the file-descriptor level (file descriptors 1 and 2). Use capfd when the code under test writes directly to the underlying file descriptors — for example via a C extension or a subprocess — rather than through Python's sys.stdout/sys.stderr objects.
| test.py | |
|---|---|
1 2 3 4 5 | |
| test.py | |
|---|---|
1 2 3 4 5 6 7 | |
Binary Capture
capsysbinary and capfdbinary behave like capsys and capfd, but readouterr() returns bytes instead of str. Reach for them when you need to assert on raw bytes or when the code under test writes binary data directly to the output streams.
| test.py | |
|---|---|
1 2 3 4 5 | |
| test.py | |
|---|---|
1 2 3 4 5 6 7 | |
Capturing Warnings
The recwarn fixture captures every warning raised during the test. It behaves like a list of warnings.WarningMessage objects — you can index into it, iterate it, and take its length.
| test.py | |
|---|---|
1 2 3 4 5 6 7 8 | |
Use recwarn.pop(category) to remove and return the first warning matching a given category — it raises AssertionError if no matching warning was recorded. Call recwarn.clear() to drop everything captured so far.
| test.py | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |