Skip to main content

Colocate your Python tests, Rust-style

Project description

inline-tests

"Just itest it."
"Does it pass the (eye) itest?" 👁️

inline-tests

PyPI Python License CI


The Problem

Tests live in one place. Code lives in another. You change a function and forget to update its test. Or you write the test later. Or never.

The test file mirrors the source file. src/auth/login.py becomes tests/auth/test_login.py. Two parallel hierarchies drifting apart.

Rust solved this years ago. Tests live next to the code they test. You see them together. You change them together. They can't drift because they're in the same file.

Python never had this. Until now.

The Solution

# auth/login.py
from inline_tests import test

def authenticate(user, password):
    if not user or not password:
        return None
    return verify_credentials(user, password)

@test
def rejects_empty_credentials():
    assert authenticate("", "pass") is None
    assert authenticate("user", "") is None

@test
def accepts_valid_credentials():
    result = authenticate("admin", "secret")
    assert result is not None
itest

Install

uv tool install inline-tests

This gives you itest everywhere. For one-off use: uvx inline-tests.

Other install methods
# As a project dependency
uv add inline-tests --group dev
pip install inline-tests

# With extras
uv tool install inline-tests[full]        # everything
uv tool install inline-tests[essentials]  # async, mock, coverage

Why This Works

Tests can't go stale. When you change the function, the test is right there. You can't miss it.

Testing becomes part of writing code. Not a separate phase. Not something you do after. You think about behavior while you're defining it.

You see the relationship. Test and implementation side by side. You notice when tests check implementation details instead of behavior. You notice missing edge cases.

No ceremony. No mirroring directory structures. No hunting for the right test file. No context switching. Just @test and you're done.

Features

Everything pytest offers, because this is pytest.

from inline_tests import test, it

# BDD style
@it
def should_handle_empty_input():
    assert process("") == []

# Async
@test
async def fetches_data():
    result = await fetch("https://api.example.com")
    assert result.status == 200

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

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

# Test classes (no decorator needed on class)
class ValidationSuite:
    @test
    def rejects_negative(self):
        assert validate(-1) is False

Extras

Extra What you get
async pytest-asyncio, anyio, pyleak
mock pytest-mock
cov pytest-cov
parallel pytest-xdist
bench pytest-benchmark
property hypothesis
http pytest-httpx
data faker
essentials async + mock + cov
full all of the above

How It Works

The @test decorator marks functions with a hidden attribute. When you run itest, the plugin scans Python files for this marker using AST parsing. No imports happen until a file actually contains tests. Then pytest collects and runs them normally.

Standard test_*.py files work as usual. This is additive.

Production

Just ship it. The @test decorator is a no-op at runtime. Tests never execute unless you run itest.

Or strip tests for minimal deployments:

itest strip src/ -o dist/
uv build dist/

Removes @test functions via AST. Original files untouched.

Development

git clone https://github.com/dedalus-labs/inline-tests-python
cd inline-tests
uv sync
uv run pytest

Built on a modern Python stack. See CONTRIBUTING.md.

License

MIT

Links


Dedalus Labs © 2026.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

inline_tests-0.0.1.tar.gz (9.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

inline_tests-0.0.1-py3-none-any.whl (10.4 kB view details)

Uploaded Python 3

File details

Details for the file inline_tests-0.0.1.tar.gz.

File metadata

  • Download URL: inline_tests-0.0.1.tar.gz
  • Upload date:
  • Size: 9.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.8

File hashes

Hashes for inline_tests-0.0.1.tar.gz
Algorithm Hash digest
SHA256 9a5db67234d2e11cfe1f273c4be4e556fe9990e096d4c00f0571b4e90a4c09ee
MD5 0b57900aaa0586fcfc34ad5f44364772
BLAKE2b-256 c728ecd36ab55586bcbfccaccfa1422dd6e22844a054ab368650a522ea230370

See more details on using hashes here.

File details

Details for the file inline_tests-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for inline_tests-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8fb177f6960d015e97ce7384151ac52afbeb986e1ea99d14ee8f7ed72dfb4ed4
MD5 20d1d2e1ddb1d7081631167a983a3828
BLAKE2b-256 4daa7e8bc1ddcdb6b933caf8b4f06513e2e6aca8d7a2df321941dbe9012d362a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page