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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pytest_nats-0.0.2.dev2.tar.gz.
File metadata
- Download URL: pytest_nats-0.0.2.dev2.tar.gz
- Upload date:
- Size: 21.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
704bd725251df15d56440899b6e0d7f40baef33404a6eacd5fa3f64953fb47f1
|
|
| MD5 |
55eb0ae3f0cd47cc6175f852916dd3e4
|
|
| BLAKE2b-256 |
4e2f3476735e999e18e8e221663619781511d1fcf56dc7eac6d1cd26efb1b93c
|
Provenance
The following attestation bundles were made for pytest_nats-0.0.2.dev2.tar.gz:
Publisher:
release.yml on m3nowak/pytest-nats
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pytest_nats-0.0.2.dev2.tar.gz -
Subject digest:
704bd725251df15d56440899b6e0d7f40baef33404a6eacd5fa3f64953fb47f1 - Sigstore transparency entry: 2655313770
- Sigstore integration time:
-
Permalink:
m3nowak/pytest-nats@c6d202423ed7b1cacf4640ba4fdc88f823d7e65c -
Branch / Tag:
refs/tags/v0.0.2.dev2 - Owner: https://github.com/m3nowak
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@c6d202423ed7b1cacf4640ba4fdc88f823d7e65c -
Trigger Event:
release
-
Statement type:
File details
Details for the file pytest_nats-0.0.2.dev2-py3-none-any.whl.
File metadata
- Download URL: pytest_nats-0.0.2.dev2-py3-none-any.whl
- Upload date:
- Size: 14.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ce6128c0b604dc772b704289ee09e81802f227894e9658060dd5415f30454021
|
|
| MD5 |
ee095966fe278972ee1588e52291ec72
|
|
| BLAKE2b-256 |
fd6d144587b18e43cdf116e1aeb236a18189c6d3684dbde7d313e0a0f2ffa3dc
|
Provenance
The following attestation bundles were made for pytest_nats-0.0.2.dev2-py3-none-any.whl:
Publisher:
release.yml on m3nowak/pytest-nats
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pytest_nats-0.0.2.dev2-py3-none-any.whl -
Subject digest:
ce6128c0b604dc772b704289ee09e81802f227894e9658060dd5415f30454021 - Sigstore transparency entry: 2655313792
- Sigstore integration time:
-
Permalink:
m3nowak/pytest-nats@c6d202423ed7b1cacf4640ba4fdc88f823d7e65c -
Branch / Tag:
refs/tags/v0.0.2.dev2 - Owner: https://github.com/m3nowak
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@c6d202423ed7b1cacf4640ba4fdc88f823d7e65c -
Trigger Event:
release
-
Statement type: