Skip to main content

pytest-systemd-slice

Run every pytest session — together with all its pytest-xdist workers — in its own systemd scope inside a user slice (pytest.slice by default).

Why: pytest -n 16 on a big Django test suite can eat tens of gigabytes. When it runs in your terminal's cgroup, it competes with your desktop, browser and editor, and when memory runs out the OOM killer (or a frozen desktop) picks the victim. Put the tests in their own slice and you can:

  • cap them: MemoryMax=32G hits the tests, not KWin/GNOME Shell;
  • let systemd-oomd kill the test run under memory pressure;
  • give them a lower CPUWeight= so the desktop stays responsive;
  • watch them with systemd-cgtop / systemctl --user status pytest.slice.

How it works

Early at startup — before pytest captures output and before xdist starts its workers — the plugin re-executes the very same command line under

systemd-run --user --scope --slice=pytest.slice -- <python> <original argv>

systemd-run --scope registers a transient scope and then execs the command, so the PID, terminal, environment, working directory, Ctrl+C and the exit code stay the same: uv run pytest, make test, tox and IDE runners keep working. The header of the run shows where it landed:

systemd: running in pytest.slice (run-p123456-i789.scope)

Nothing happens — pytest runs exactly as without the plugin — when:

  • not on Linux, systemd-run is missing or there is no systemd user instance (the user bus is looked up in $XDG_RUNTIME_DIR, falling back to /run/user/<uid>, so it also works from a tmux server run as a system service);
  • CI is set (unless a slice is requested explicitly by option or environment);
  • it is an xdist worker or the run is already in the slice;
  • pytest was called programmatically (pytest.main([...])) or with --help/--version;
  • it is disabled (see below).

Docker containers started by the tests (testcontainers etc.) are not moved: they belong to the Docker daemon's cgroup.

Installation

Per project:

uv add --dev pytest-systemd-slice      # or: pip install pytest-systemd-slice

For all projects on your machine without touching them, install it into a directory of its own and put that on PYTHONPATH (pytest finds the plugin via its entry point there; it has no dependencies besides pytest itself):

uv pip install --no-deps --target ~/.local/lib/pytest-systemd-slice pytest-systemd-slice
echo 'export PYTHONPATH="$HOME/.local/lib/pytest-systemd-slice${PYTHONPATH:+:$PYTHONPATH}"' >> ~/.bashrc

The module is Python 3.9+ syntax-compatible, so it is harmless in older venvs.

Configuring the slice

The slice is created on demand. Give it limits with a unit file, e.g. ~/.config/systemd/user/pytest.slice:

[Unit]
Description=pytest runs (pytest-systemd-slice)

[Slice]
MemoryHigh=30G
MemoryMax=32G
MemorySwapMax=2G
CPUWeight=50
IOWeight=50
# Under sustained memory pressure systemd-oomd kills a whole test run
# (one scope), not your desktop.
ManagedOOMMemoryPressure=kill
ManagedOOMMemoryPressureLimit=40%

then systemctl --user daemon-reload.

Options

Command line Environment ini Meaning
--systemd-slice=NAME PYTEST_SYSTEMD_SLICE=NAME systemd_slice = NAME slice to use (default pytest.slice; .slice is appended if missing)
--no-systemd-slice PYTEST_SYSTEMD_SLICE=0 systemd_slice = off do not re-execute
--systemd-slice-property=K=V (repeatable) systemd_slice_properties (lines) extra systemd-run --property for the scope of this run, e.g. MemoryMax=8G

Precedence: command line, then environment, then ini. -p no:pytest_systemd_slice disables the plugin entirely.

License

MIT

Metadata

Release files for pytest-systemd-slice 0.1.0

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

Source distribution (sdist)

Source distribution for pytest-systemd-slice 0.1.0
File Size Uploaded Provenance Actions
pytest_systemd_slice-0.1.0.tar.gz 21.7 kB Without provenance Details for pytest_systemd_slice-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pytest-systemd-slice 0.1.0
File Interpreter ABI Platform Actions
pytest_systemd_slice-0.1.0-py3-none-any.whl Python 3 none any Details for pytest_systemd_slice-0.1.0-py3-none-any.whl

Total release size: 29.1 kB

Release files / pytest_systemd_slice-0.1.0.tar.gz

Download URL pytest_systemd_slice-0.1.0.tar.gz
Size 21.7 kB
Tags Source
SHA-256 checksum
How to use checksums
55fbaa7ba54154e172ae57c1f3cf098fc6c256a8489e9066d6d5c0840fb9303b
BLAKE2b-256 checksum
How to use checksums
eb2d681916b0431ad133dda4c6517f277447217c194ee445fbf2d2ccf32f9843
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.8 {"installer":{"name":"uv","version":"0.13.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / pytest_systemd_slice-0.1.0-py3-none-any.whl

Download URL pytest_systemd_slice-0.1.0-py3-none-any.whl
Size 7.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
93ee6d3d421d06981a5a532c734737c6a50f2678d708d4cef57aa673d5697b90
BLAKE2b-256 checksum
How to use checksums
00b05282ede02b4c59416271a60bfda4a449be0f7b3452f3868706ffb125f96f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.8 {"installer":{"name":"uv","version":"0.13.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Security for pytest-systemd-slice 0.1.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.1.0 This release

2 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