ztest
Process-supervised Python test runner in Zig: pidfd/killpg containment, a
Zygote worker pool, 2.6× lower memory than pytest-xdist, --llm JSONL for AI
agents.
Install in 1 second
pip install ztest-runner # PyPI wheel, no toolchain
uvx --from ztest-runner ztest --version # zero-install, one command
curl -LsSf https://raw.githubusercontent.com/alexchen-sys/ztest/main/scripts/install.sh | sh
curl -LsSf https://raw.githubusercontent.com/alexchen-sys/ztest/main/scripts/install.sh | sh -s -- --system
Prebuilt static musl binaries (stripped, x86_64 and aarch64) sit on
GitHub Releases if you would
rather not pipe a shell script; the installer verifies SHA256SUMS and honors
ZTEST_VERSION / ZTEST_INSTALL_DIR. ztest --version prints
ztest 0.5.1 (Zig 0.17.0). Building from source needs Zig 0.17.0 and is
covered at the end of this file.
Why ztest
1. For AI agents: ztest --llm
pytest starts in 600–900 ms and hands an agent 5,000–25,000 tokens of
banners, progress bars, PASSED lines and multi-page tracebacks. ztest --llm
starts in ~1 ms, prints nothing for a passing test, and emits one JSON line per
failure plus a final summary — measured 85.7 % fewer bytes than pytest -v on a
42-test probe, and the gap widens as tracebacks deepen. A test that hangs inside
a C extension holding the GIL cannot take the agent's session with it: the
kernel kills the process group, not a Python-level timer. Numbers and method:
BENCHMARKS.md §1.
2. Containment and timeouts that hold (ADR-004)
pytest-timeout's signal method cannot interrupt a C extension that holds the
GIL: the handler runs when the call returns, which is how a 1 s deadline became
a 17.6 s wait, and how a call that never returns turns a one-minute CI step into
a sixty-minute job. Its thread method uses os._exit, which leaves every child
the test created alive and holding the job's pipes. ztest pins worker identity
with pidfd_open, holds the zombie with waitid(WNOWAIT), kills with
kill(-pgid, SIGKILL), sweeps setsid escapees under a child subreaper, and
runs a native watchdog over a MAP_SHARED process-group registry — a hung
worker dies on schedule and leaves zero processes behind (measured: 1.28 s,
0 leaked processes). Rationale and accepted limits:
ADR-004.
3. A drop-in answer to pytest-xdist
pytest -n4 re-collects the suite in every worker over execnet, errors out
whenever collection order differs between workers, and pays a full interpreter
plus import graph per worker. In a nine-suite cross-check it failed on 3 of 9
(33 %) and peaked at 1.70×–4.94× (median 2.60×) the memory of ztest -j4 —
1.38 GiB against 462 MiB on dateparser. ztest forks workers from one pre-warmed
zygote, schedules by file with LPT, recycles workers per file, and holds a flat
memory ceiling on suites where a monolithic pytest is OOM-killed.
What is ztest
ztest runs pytest suites under kernel-level process-group and pidfd
containment. It keeps the Python test surface — conftest.py, fixtures,
markers, pytest.raises, unittest.TestCase — and moves supervision,
parallelism, and timeout enforcement into a native Zig supervisor.
Every worker runs in its own process group under PR_SET_PDEATHSIG,
pidfd-pinned identity, waitid(WNOWAIT) zombie pinning, and a subreaper
sweep. The supervisor starts in ~1.2 ms (--version) without booting a master
Python interpreter or pluggy plugin chain, and workers fork from a pre-warmed
Python zygote to amortize interpreter startup across files.
For CI and automation, --llm emits JSON Lines: one object per failure event
and a stable summary record at the end.
The compatibility shim is compiled into the binary. No Python package or
pytest installation is required at runtime: the shim registers the pytest
and _pytest modules directly.
Features
-j Nparallel workers (default 4, accepts 1–64, clamped to 16) with file-batched LPT scheduling.- Hard per-test timeout (
--timeout=<sec>, up to 86400). The worker arms an in-process timer while the supervisor enforces an outside inactivity window (max(1500, sec*1000) + 1500ms, or 1 h when--timeoutis unset). - Race-free timeout kill:
pidfd_openpins worker identity whilewaitid(WNOWAIT)holds the zombie untilkill(-pgid, SIGKILL)completes. - Three-layer process-tree containment:
PR_SET_PDEATHSIGcascade, zygote EOF watch, and a native watchdog over a shared process-group registry, plus a per-file orphan sweep. - Nonblocking IPC over file descriptor 199 with CRC32 framing and
SO_PEERCREDpeer authentication. - Terminal progress view and
--llmJSONL output ({"t":"fail",...},{"t":"crash",...}, and{"t":"summary",...}withpassed,failed,skipped,deselected,crashed,collection_errors,collected,duration_ms). Avoid combining--llmwith-sif tests write to stdout. If a downstream stdout reader stalls past the 1 MiB nonblocking backlog (or the 2 s final drain deadline), whole unbuffered records are dropped, the drop count is reported to stderr, andztestexits non-zero (exit2when all tests otherwise passed). - pytest-compatible exit codes:
0pass,1failure,2interrupted or collection error,3internal error,4usage error,5nothing collected.
Requirements
- Linux (
x86_64andaarch64). The supervisor relies onpidfd_open(2),statx(2),prctl(PR_SET_PDEATHSIG), process groups and/proc. - Python 3.11 or newer at runtime, discoverable on
PATHor passed with--python=<path>. - No build toolchain unless you build from source, which needs Zig 0.17.0.
Build from source
Prebuilt binaries and PyPI wheels are the normal path (see Install in 1 second). Contributors build from a checkout:
git clone https://github.com/alexchen-sys/ztest
cd ztest
zig build -Doptimize=ReleaseSafe
The binary lands at zig-out/bin/ztest. Installing it is optional:
sudo install -m 0755 zig-out/bin/ztest /usr/local/bin/ztest
ReleaseSafe keeps the overflow and bounds checks that matter for a program whose job
is killing process trees. Debug builds are fine for development; ReleaseFast gains
little here because run time is dominated by the tests, not by the supervisor.
Quickstart
Run the example suite shipped in this repository:
ztest examples/
The summary line reads 8 passed. Point ztest at a directory or a set of
files:
ztest examples/
ztest examples/test_calc.py examples/test_text.py
With no arguments, ztest runs the default target tests/:
ztest
Eight workers, and a 30 second ceiling on any single test:
ztest -j8 --timeout=30 tests/
Filter by name or by marker, using pytest expression syntax:
ztest -k "login or logout and not slow" tests/
ztest -m "integration" tests/
List what would run, without running it:
ztest --collect-only tests/
Machine-readable output for agents and CI:
ztest --llm --timeout=60 tests/
Stop early, first failure or after N failures:
ztest -x tests/
ztest --maxfail=5 tests/
Options
| Option | Meaning |
|---|---|
-k <expr> |
Filter tests by substring or boolean expression (matches test names, parametrized ids and marker names) |
-m <expr> |
Filter tests by marker expression |
-x, --exitfirst |
Stop on the first failure |
--maxfail=<N> |
Stop after N failures |
--timeout=<sec> |
Per-test timer; also widens the supervisor's inactivity window (0 < sec <= 86400; user timer default: off, window then 1 h) |
-j <N>, -n <N> |
Parallel workers, 1 to 64 (clamped to 16, default 4) |
--llm |
JSONL output for agents and tooling |
--engine=native|pytest |
Test execution engine: built-in zero-dependency shim (native, default) or in-worker upstream pytest bridge (pytest) for third-party pluggy plugins (also read from ZTEST_ENGINE) |
--mode=zygote|exec |
Worker start strategy (default: zygote) |
--preload=<list> |
Comma-separated modules to pre-import once into the zygote (opt-in; default: stdlib only; engages above the 8-file small-suite threshold, never in single-file clean-spawn). Also read from ZTEST_PRELOAD_PKGS |
--python=<path> |
Explicit Python 3.11+ interpreter |
--collect-only, --co |
Collect and print test node IDs only |
-s, --no-capture |
Do not capture stdout/stderr |
-v, --verbose |
Verbose output |
-q, --quiet |
Quiet output |
-h, --help |
Usage |
-V, --version |
Version |
Interpreter resolution order without --python: $VIRTUAL_ENV/bin/python
(when the variable is set and the file exists) → ./.venv/bin/python →
./venv/bin/python → the first python3 found on PATH → /usr/bin/python3.
A python3 resolved from PATH outside /usr/bin is used without comment —
that is the deliberate toolchain of pyenv/conda/mise users. Landing on
/usr/bin/python3 itself, whether reached through PATH or as the last
fallback, prints a one-line warning on stderr, so a run without a project
virtualenv is never silently executed against the system Python.
For command-line compatibility, the parser accepts pytest display-formatting
flags (--tb=..., --tb <mode>, --color=..., -ra, -rf, -rA) as no-ops.
Unimplemented semantic flags (-W, -o, -p, -c, --import-mode,
--rootdir, --strict-markers, --disable-warnings) are rejected with exit
code 4 rather than silently ignored.
Compatibility
Supported:
conftest.pydiscovery with the usual directory scoping.- Fixtures as a dependency graph, including transitive parameterization identity — the same parameterized fixture is one instance per parameter set, not one per consumer.
- Builtin fixtures:
tmp_path,tmp_path_factory,tmpdir,tmpdir_factory,monkeypatch,caplog,capsys,capsysbinary,capteesys,capfd,capfdbinary,recwarn,doctest_namespace,record_property,request,pytestconfig,cache,benchmark(lightweight pass-through stub), and amockerfixture backed by a minimalunittest.mockintegration.capsyscaptures Python-level writes tosys.stdout/sys.stderr;capfdcaptures at the file-descriptor level, so output from subprocesses and rawos.writecalls lands inreadouterr()as well. pytest.raises,pytest.approx,pytest.mark.parametrize,pytest.mark.skip,pytest.mark.skipif,pytest.mark.xfailincludingstrict, andpytest.fixturewithscope,params,autouse,name,ids.xfailandskipsemantics, including a test that fails during teardown of an expected failure.unittest.TestCaseclasses with real lifecycle delegation:setUp,tearDown,setUpClass,tearDownClass,addCleanup,subTest, and async test methods.async deftests, run throughasyncio.Runnerwith per-test event loop isolation and validation of the coroutine result.- Assertion rewriting with source context, cached in
.ztest_cache/by file content and interpreter build. - Conftest hooks, invoked as plain functions:
pytest_addoption,pytest_configure,pytest_unconfigure,pytest_collection_modifyitems,pytest_generate_tests,pytest_sessionstart,pytest_sessionfinish,pytest_runtest_setup,pytest_runtest_teardown, and custom collectors/items throughpytest_pycollect_makeitemandpytest_collect_file.
Not supported in the default native engine (use --engine=pytest when required):
- Arbitrary third-party
pluggyplugins (pytest11entry points). The defaultnativeengine implements the pytest API surface directly without bootingpluggyor requiringpytestin the environment. When a suite depends on third-party plugins (pytest-django,pytest-asyncio,hypothesis, custompytest11hooks), pass--engine=pytest(orZTEST_ENGINE=pytest): workers fork from the zygote and run upstreampytestthrough an in-worker ZTST binary IPC bridge while preservingpidfdsupervision, process-group containment, and--llmJSONL output. - Unsupported API fails loud instead of degrading. Touching a
pytestattribute the shim does not implement raisesZtestUnsupportedApiError(anAttributeErrorsubclass, sohasattrprobes still behave).pytest.hookimplaccepts the wrapper and ordering flags so a conftest that uses them still loads; the shim calls hooks as plain functions, so ordering flags such astryfirstandtrylastare accepted and ignored, and a wrapper-marked generator hook is driven through its pre-yield and post-yield halves back to back rather than composed around other hooks. A generator or coroutine hook without the wrapper marking still fails closed when the shim would invoke it; the reporting metadata hooks are recognized and never called. pytest.iniand project configuration generally: only the options the shim reads are honored, so keep per-project configuration thin (python_filesandnorecursedirsaccept up to 256 patterns; exceeding 256 exits 4). Configuration is discovered by walking up from the target paths (apyproject.tomlcounts only when it carries a[tool.pytest.ini_options]section), so running ztest from outside the project directory still picks up the project's ini options.- Windows, macOS, and Linux architectures other than
x86_64andaarch64.
Known differences from pytest, by design:
- A test whose body passes but whose finalizer fails with an unexpected error is
counted once, as failed (the finalizer failure dominates, unless a single
finalizer exception matches a declared
@pytest.mark.xfailon the item). pytest reports an unexpected finalizer error after a passing body as one passed plus one error; the exit code matches. - A collection failure caused by a broken
conftest.pyexits 2 (collection error), where pytest 9 exits 4. A syntax-broken test module exits 2 under both. - On
SIGINTztest prints a one-line partial tally to stderr (ztest: interrupted (...)) and exits 2, instead of pytest's banner. - A crashed worker does not lose counts: tests that finished before the crash
stay counted, the crashed file adds one failure, and the
--llmsummary'scollectedfield still reflects what was collected.
Design
- docs/INVARIANTS.md — the invariants the runner must not break, keyed by the stable identifiers the code cites at each guard.
- docs/adr/ADR-004-process-containment.md — why containment is three layers instead of one, what each layer covers, and where they overlap on purpose.
- docs/adr/ADR-005-unittest-delegation.md —
the contract for delegating
unittest.TestCaselifecycle to the real runner instead of imitating it. - docs/adr/ADR-006-layout.md — why the shim is one
file and how
main.zigis split. - docs/adr/README.md — ADR index and numbering policy.
Development
zig build test
This runs the native suite: 48 tests (43 in src/main.zig, 3 in src/cli.zig,
2 in src/spawn_arm.zig). The
suite covers framing and CRC rejection, IPC peer authentication, the containment
watchdog and its registry, orphan-sweep exclusions, CLI parsing and its bounds,
interpreter resolution, exit-code parity, and an anti-drift test that compiles
the embedded shim under
python3 so the binary cannot ship a shim that does not parse.
The behavioral gate lives in tests/: 132 unittest cases that run the real binary
against 120 fixture trees, each reproducing a compatibility bug confirmed against an
OSS suite (see BENCHMARKS.md). It needs the built binary and skips
cleanly without one:
ZTEST_BIN=zig-out/bin/ztest python3 -m pytest tests/test_oss_regressions.py -q
The binary under test is $ZTEST_BIN when set, else this repo's zig-out
build, else the first ztest on PATH.
ztest --version prints the version: ztest 0.5.1 (Zig 0.17.0).
Benchmarks
Full matrix, method, and the containment probes: BENCHMARKS.md.
Over 100 real PyPI suites on 4 vCPU / 8 GiB: wall median 1.465× faster than
serial pytest across the 90 full-parity suites, 90/96 (93.8 %) FULL-PARITY
on the native engine and 94/96 (97.9 %) with --engine=pytest; against
pytest -n4 (xdist), ztest -j4 uses 1.70×–4.94× less peak memory (median
2.60×) and passes the 3 of 9 suites where xdist fails. Every number is
reproducible from the manifest and the harness in benchmarks/.
Limitations
- Linux (
x86_64andaarch64) only (see Requirements). - A descendant that calls
setsidand outlives the supervisor escapes group-based containment. While the supervisor runs, the orphan sweep finds and kills such processes; after it exits, nothing can. ADR-004 records this as an accepted limitation; closing it would require cgroup scoping per run. - The per-file isolation model re-executes conftests and re-imports packages per file in each worker; suites dominated by heavy per-file import graphs with little per-test work (pygments) can run slower than serial pytest. BENCHMARKS.md measures both directions.
License
MIT. See LICENSE.
Metadata
Release files for ztest-runner 0.5.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distributions (wheels)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| ztest_runner-0.5.1-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.musllinux_1_1_x86_64.whl | Python 3 | none | Linux glibc 2.17+ x86-64, Linux musl 1.1+ x86-64 | Details for ztest_runner-0.5.1-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.musllinux_1_1_x86_64.whl |
| ztest_runner-0.5.1-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.musllinux_1_1_aarch64.whl | Python 3 | none | Linux glibc 2.17+ ARM64, Linux musl 1.1+ ARM64 | Details for ztest_runner-0.5.1-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.musllinux_1_1_aarch64.whl |
Total release size: 746.0 kB
Release files / ztest_runner-0.5.1-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.musllinux_1_1_x86_64.whl
| Download URL | ztest_runner-0.5.1-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.musllinux_1_1_x86_64.whl |
|---|---|
| Size | 378.6 kB |
| Tags | Linux glibc 2.17+ x86-64 Linux musl 1.1+ x86-64 Python 3 |
|
SHA-256 checksum How to use checksums |
a5eb090b402303000967b9361de4a1d2b33419250625849d951bf4801f429344
|
|
BLAKE2b-256 checksum How to use checksums |
2bec4d71365c45ac33d63b2708996349a3adc104b8e0438b74449d04ba4989b2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Release files / ztest_runner-0.5.1-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.musllinux_1_1_aarch64.whl
| Download URL | ztest_runner-0.5.1-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.musllinux_1_1_aarch64.whl |
|---|---|
| Size | 367.4 kB |
| Tags | Linux glibc 2.17+ ARM64 Linux musl 1.1+ ARM64 Python 3 |
|
SHA-256 checksum How to use checksums |
12f04231861af0dd2465bc405843ac5d30c88750039f62c2ddabb8c2147a5585
|
|
BLAKE2b-256 checksum How to use checksums |
bd8a57fe2de7816ad872e2b0bad45584a2685c5b079c662ea92560382f73c399
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Security for ztest-runner 0.5.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.