Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

pytest-nats

Pytest helpers for starting isolated test NATS servers.

Installation

pip install pytest-nats

pytest-nats does not register a global pytest plugin. Declare each server fixture explicitly in conftest.py; the variable name becomes the fixture name:

from pytest_nats import nats_server_fixture

nats_server = nats_server_fixture()

Tests receive a read-only NatsServer with the client URL, host, dynamic port, resolved NATS version, JetStream state, and live stdout and stderr snapshots:

from pytest_nats import NatsServer


def test_messaging(nats_server: NatsServer) -> None:
    assert nats_server.url == f"nats://127.0.0.1:{nats_server.port}"
    assert not nats_server.jetstream_enabled

The fixture binds its unauthenticated client and internal monitoring listeners only to 127.0.0.1, selects dynamic ports, and waits for both a NATS protocol exchange and the health endpoint before yielding. It terminates the server and removes generated configuration and data at the end of the selected scope.

JetStream

Enable JetStream when declaring the fixture. One server supports both memory- and file-backed streams; client code remains responsible for creating streams and consumers.

from pytest_nats import nats_server_fixture

jetstream_server = nats_server_fixture(
    jetstream=True,
    max_memory_store=512 * 1024 * 1024,
    max_file_store=2 * 1024 * 1024 * 1024,
)

The default aggregate limits are 256 MiB of memory and 1 GiB of file storage. File data is isolated per server and removed during teardown.

Fixture Scope

Function scope is the default. Module and session scopes retain server and JetStream state for their normal pytest lifetime:

module_nats = nats_server_fixture(scope="module")
session_nats = nats_server_fixture(scope="session", jetstream=True)

Supported scopes are function, module, and session.

NATS executable selection

By default, pytest-nats finds nats-server on setup-time PATH and validates that it reports a NATS 2.x semantic version. Use Local to select another command name or path. Relative paths containing a directory are resolved from pytest's root path.

from pathlib import Path

from pytest_nats import GitHub, Local, Mise, Provision, nats_server_fixture

default_local_nats = nats_server_fixture()
alternate_local_nats = nats_server_fixture(Local("nats-server-another"))
local_path_nats = nats_server_fixture(Local(Path("tools/nats-server")))
latest_nats = nats_server_fixture(Provision())
mise_nats = nats_server_fixture(Mise("2.12"))
github_nats = nats_server_fixture(GitHub("2.12.15", cache_dir=Path(".cache/nats")))

Local, Provision, Mise, and GitHub are immutable source values. Raw strings and paths are not accepted as the fixture's binary argument. Provision prefers Mise when it is available on setup-time PATH and uses GitHub otherwise. Automatic provisioning accepts latest, major, major-minor, and exact stable NATS 2.x selectors that can select releases starting at 2.2.0. startup_timeout sets the positive setup deadline in seconds and defaults to 10 seconds.

mise

mise is a development-tool version manager. Mise asks the mise executable on PATH to install and locate the selector through its GitHub backend. The selector is passed directly to Mise. Successful acquisition is reused for the rest of the pytest process, while failures remain retryable.

GitHub

GitHub downloads official NATS Server releases, verifies their published checksums, and atomically stores executables in the selected cache directory. Existing regular executable cache entries are trusted without running or rehashing them. Set the optional GITHUB_TOKEN environment variable to authenticate GitHub API and download requests, which can avoid anonymous API rate limits. See GitHub's personal access token documentation for token creation and handling guidance.

Executable lookup, version resolution, and provisioning failures raise NatsExecutableError. Its category is an ExecutableErrorCategory value. Server startup and lifecycle failures raise NatsServerError; its returncode, stdout, and stderr attributes retain available diagnostics.

Development

Install mise, then install the project tools and dependencies:

mise run install
mise run setup

Run tests and checks:

mise run test
mise run check

Apply automatic lint and formatting fixes:

mise run fix

Releasing

Releases run from .github/workflows/release.yml. The workflow accepts a stable, canonical PEP 440 version, runs the complete CI workflow, builds and validates the source distribution and wheel, publishes them to PyPI, then publishes the draft GitHub Release. Run it from the main branch in the GitHub Actions UI. The first intended version is 0.0.1, which creates tag v0.0.1.

Before the first release, create a pending Trusted Publisher on PyPI with these values:

  • PyPI project: pytest-nats
  • GitHub owner: m3nowak
  • GitHub repository: pytest-nats
  • Workflow filename: release.yml
  • Environment: pypi

Create the pypi environment in the GitHub repository without required reviewers. The workflow requests an OIDC token only in the PyPI publication job; no PyPI API token is needed.

Download files

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

Source Distribution

pytest_nats-0.0.2.dev1.tar.gz (21.0 kB view details)

Uploaded Source

Built Distribution

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

pytest_nats-0.0.2.dev1-py3-none-any.whl (14.6 kB view details)

Uploaded Python 3

File details

Details for the file pytest_nats-0.0.2.dev1.tar.gz.

File metadata

  • Download URL: pytest_nats-0.0.2.dev1.tar.gz
  • Upload date:
  • Size: 21.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for pytest_nats-0.0.2.dev1.tar.gz
Algorithm Hash digest
SHA256 da8d94364f1f9c7f8f0ebd4ca191dd9eb8816a8c03d72dd56a2149fa426fae11
MD5 44f3a3814c4d062776f80e0a2848d3cc
BLAKE2b-256 d3969a515c164f050922304163a5521fa35cf15577cb6a072085a0ddac075ca1

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_nats-0.0.2.dev1.tar.gz:

Publisher: release.yml on m3nowak/pytest-nats

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pytest_nats-0.0.2.dev1-py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_nats-0.0.2.dev1-py3-none-any.whl
Algorithm Hash digest
SHA256 2c7620402994d491c4c90ed62500a33008ce2884efd67930a2d9c23977cd5a1d
MD5 de677a693b4c1a9e8030cb04d35a090f
BLAKE2b-256 c9866859f1f77a60645a48b937f5435ee4a68dc9518a722209e38d43f9669d06

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_nats-0.0.2.dev1-py3-none-any.whl:

Publisher: release.yml on m3nowak/pytest-nats

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

0.0.2

2 files

This release

0.0.2.dev1 This release

2 files

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