Skip to content

pytest Integration

inline-tests is a pytest plugin. Everything pytest offers works here.

Fixtures

from inline_tests import test

@test
def writes_file(tmp_path):
    f = tmp_path / "test.txt"
    f.write_text("hello")
    assert f.read_text() == "hello"

All built-in fixtures work: tmp_path, capsys, monkeypatch, request, etc.

Custom fixtures

Define fixtures in conftest.py as usual:

# conftest.py
import pytest

@pytest.fixture
def db_connection():
    conn = create_connection()
    yield conn
    conn.close()
# models.py
from inline_tests import test

@test
def queries_database(db_connection):
    result = db_connection.execute("SELECT 1")
    assert result == 1

Parametrize

import pytest
from inline_tests import test

@test
@pytest.mark.parametrize("x,expected", [
    (1, 1),
    (2, 4),
    (3, 9),
])
def squares_correctly(x, expected):
    assert x * x == expected

Markers

import pytest
from inline_tests import test

@test
@pytest.mark.slow
def long_running_test():
    ...

@test
@pytest.mark.skip(reason="not implemented")
def future_test():
    ...

Run with markers:

itest -m "not slow"

Async

With pytest-asyncio (included in inline-tests[async]):

from inline_tests import test

@test
async def async_test():
    result = await some_async_function()
    assert result is not None

Plugins

All pytest plugins work. Popular ones:

  • pytest-mock - mocker fixture for mocking
  • pytest-cov - Coverage reporting
  • pytest-xdist - Parallel test execution
  • hypothesis - Property-based testing

Install via extras:

uv tool install inline-tests[full]