Skip to main content

ztest

Process-supervised Python test runner in Zig: native process-group containment (pidfd on Linux, kqueue on macOS), a Zygote worker pool, 2.6× lower memory than pytest-xdist in the Linux benchmark, and --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 for Linux x86_64/aarch64 and Mach-O binaries for macOS x86_64/arm64 sit on GitHub Releases if you would rather not pipe a shell script. Darwin builds link the system libraries and carry an ad-hoc signature; a universal2 tarball is also published. Download a tarball and its SHA256SUMS for a manual installation, or let the installer select the asset and verify its checksum. The installer honors ZTEST_VERSION / ZTEST_INSTALL_DIR. ztest --version prints ztest 0.6.0 (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 containment. Linux uses pidfd identity and process waits; macOS uses kqueue process events. 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.

On Linux every worker runs in its own process group under PR_SET_PDEATHSIG, pidfd-pinned identity, waitid(WNOWAIT) zombie pinning, and a subreaper sweep. Darwin implements the same lifecycle layers with kqueue NOTE_EXIT / NOTE_TRACK, process groups and libproc inspection; see ADR-004 for its exact boundaries. 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 N parallel 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) + 1500 ms, or 1 h when --timeout is unset).
  • Race-free timeout kill: Linux uses pidfd_open and waitid(WNOWAIT) to pin worker identity; Darwin uses kqueue process-exit events and the same process-group registry. Group kills complete before the group is reaped.
  • Three-layer process-tree containment: parent-death notification, zygote EOF watch, and a native watchdog over a shared process-group registry, plus a per-file orphan sweep. Linux uses PR_SET_PDEATHSIG; Darwin uses kqueue.
  • Nonblocking IPC over file descriptor 199 with CRC32 framing and authenticated Unix-socket peer credentials (SO_PEERCRED on Linux, LOCAL_PEERCRED or LOCAL_PEERPID on Darwin).
  • Terminal progress view and --llm JSONL output ({"t":"fail",...}, {"t":"crash",...}, and {"t":"summary",...} with passed, failed, skipped, deselected, crashed, collection_errors, collected, duration_ms). Avoid combining --llm with -s if 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, and ztest exits non-zero (exit 2 when all tests otherwise passed).
  • pytest-compatible exit codes: 0 pass, 1 failure, 2 interrupted or collection error, 3 internal error, 4 usage error, 5 nothing collected.

Requirements

  • Linux (x86_64 and aarch64) or macOS (x86_64 and arm64). Linux uses pidfd_open(2), statx(2), prctl(PR_SET_PDEATHSIG), process groups and /proc; Darwin uses kqueue process events, process groups, libproc and /dev/fd. Windows and other architectures are not supported.
  • Python 3.11 or newer at runtime, discoverable on PATH or 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
--no-auto-preload Disable best-effort automatic preloading of the detected project package and optional heavy imports (also ZTEST_AUTO_PRELOAD=0)
--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.py discovery 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 a mocker fixture backed by a minimal unittest.mock integration. capsys captures Python-level writes to sys.stdout/sys.stderr; capfd captures at the file-descriptor level, so output from subprocesses and raw os.write calls lands in readouterr() as well.
  • pytest.raises, pytest.approx, pytest.mark.parametrize, pytest.mark.skip, pytest.mark.skipif, pytest.mark.xfail including strict, and pytest.fixture with scope, params, autouse, name, ids.
  • xfail and skip semantics, including a test that fails during teardown of an expected failure.
  • unittest.TestCase classes with real lifecycle delegation: setUp, tearDown, setUpClass, tearDownClass, addCleanup, subTest, and async test methods.
  • async def tests, run through asyncio.Runner with 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 through pytest_pycollect_makeitem and pytest_collect_file.

Not supported in the default native engine (use --engine=pytest when required):

  • Arbitrary third-party pluggy plugins (pytest11 entry points). The default native engine implements the pytest API surface directly without booting pluggy or requiring pytest in the environment. When a suite depends on third-party plugins (pytest-django, pytest-asyncio, hypothesis, custom pytest11 hooks), pass --engine=pytest (or ZTEST_ENGINE=pytest): workers fork from the zygote and run upstream pytest through an in-worker ZTST binary IPC bridge while preserving pidfd supervision, process-group containment, and --llm JSONL output.
  • Unsupported API fails loud instead of degrading. Touching a pytest attribute the shim does not implement raises ZtestUnsupportedApiError (an AttributeError subclass, so hasattr probes still behave). pytest.hookimpl accepts the wrapper and ordering flags so a conftest that uses them still loads; the shim calls hooks as plain functions, so ordering flags such as tryfirst and trylast are 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.ini and project configuration generally: only the options the shim reads are honored, so keep per-project configuration thin (python_files and norecursedirs accept up to 256 patterns; exceeding 256 exits 4). Configuration is discovered by walking up from the target paths (a pyproject.toml counts 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 and architectures other than Linux x86_64/aarch64 and macOS x86_64/arm64.

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.xfail on 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.py exits 2 (collection error), where pytest 9 exits 4. A syntax-broken test module exits 2 under both.
  • On SIGINT ztest 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 --llm summary's collected field still reflects what was collected.

Design

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

  • Windows and architectures outside the four targets in Requirements are not supported.
  • A descendant that calls setsid and 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.6.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distributions (wheels)

Table of built distributions (wheels) for ztest-runner 0.6.0
File Actions
ztest_runner-0.6.0-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.musllinux_1_1_x86_64.whl Python 3 none Linux musl 1.1+ x86-64, Linux glibc 2.17+ x86-64 Details for ztest_runner-0.6.0-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.musllinux_1_1_x86_64.whl
ztest_runner-0.6.0-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.6.0-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.musllinux_1_1_aarch64.whl
ztest_runner-0.6.0-py3-none-macosx_11_0_arm64.whl Python 3 none macOS 11.0+ ARM64 Details for ztest_runner-0.6.0-py3-none-macosx_11_0_arm64.whl
ztest_runner-0.6.0-py3-none-macosx_10_13_x86_64.whl Python 3 none macOS 10.13+ x86-64 Details for ztest_runner-0.6.0-py3-none-macosx_10_13_x86_64.whl

Total release size: 1.5 MB

Release files / ztest_runner-0.6.0-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.musllinux_1_1_x86_64.whl

Download URL ztest_runner-0.6.0-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.musllinux_1_1_x86_64.whl
Size 387.4 kB
Tags Linux glibc 2.17+ x86-64 Linux musl 1.1+ x86-64 Python 3
SHA-256 checksum
How to use checksums
a09206c1e02afcf911dfea8b0ae9e364dcdec17a1cdabb354422ae1f8ca3ff28
BLAKE2b-256 checksum
How to use checksums
9888dce3985e4fbd8bdb0df6f2effd44a8fd1c356577c154dfdf48163c01ddc0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.5 {"installer":{"name":"uv","version":"0.13.5","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.6.0-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.musllinux_1_1_aarch64.whl

Download URL ztest_runner-0.6.0-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.musllinux_1_1_aarch64.whl
Size 375.1 kB
Tags Linux glibc 2.17+ ARM64 Linux musl 1.1+ ARM64 Python 3
SHA-256 checksum
How to use checksums
3c9155176ec629a745945898677c9371defd86605311750311c62b41604ef961
BLAKE2b-256 checksum
How to use checksums
df4fe3a55d0559f51ec96bc49939975d2f81fb3907bf5e1127ce110fcca308a5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.5 {"installer":{"name":"uv","version":"0.13.5","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.6.0-py3-none-macosx_11_0_arm64.whl

Download URL ztest_runner-0.6.0-py3-none-macosx_11_0_arm64.whl
Size 371.1 kB
Tags Python 3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
d5aa21a3f1f3099c179d9c7f6a27807db5d68dde1df9f163017745f4205c2bfd
BLAKE2b-256 checksum
How to use checksums
aa362993cf60050844aee0e8891f0ae17bf6a73e9046abbb6e974793748c48f9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.5 {"installer":{"name":"uv","version":"0.13.5","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.6.0-py3-none-macosx_10_13_x86_64.whl

Download URL ztest_runner-0.6.0-py3-none-macosx_10_13_x86_64.whl
Size 401.4 kB
Tags Python 3 macOS 10.13+ x86-64
SHA-256 checksum
How to use checksums
48d4d27b28f5d1cf61e74abe76705a7074f22ea8108dd2f278c1de3fb954fd1b
BLAKE2b-256 checksum
How to use checksums
d7e7cd0bdd8ec8f57372f258d1d2bf474877d3924ce41234d89ba819b9910ecf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.5 {"installer":{"name":"uv","version":"0.13.5","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.6.0

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.

Release history Release notifications | RSS feed

This release

0.6.0 This release

4 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page