API reference¶
pytest_servex ¶
A pytest plugin that starts the Docker containers your tests need, one database per worker.
POSTGRES
module-attribute
¶
POSTGRES = Container(
image="postgres:17-alpine",
ports=(PORT,),
ready=_ready,
env={
"POSTGRES_USER": USER,
"POSTGRES_PASSWORD": PASSWORD,
},
command=(
"postgres",
"-c",
"fsync=off",
"-c",
"synchronous_commit=off",
"-c",
"full_page_writes=off",
),
mounts=(Tmpfs("/var/lib/postgresql/data"),),
)
Postgres with durability off and data on tmpfs.
Docker empties tmpfs when the container stops, so a restart starts a new empty database. Use
replace(POSTGRES, mounts=()) to keep data across a restart (#3).
REDIS
module-attribute
¶
Redis with the same settings. Use it where production runs Redis, not Valkey.
VALKEY
module-attribute
¶
VALKEY = Container(
image="valkey/valkey:8-alpine",
ports=(PORT,),
command=("valkey-server", *_NO_DISK),
ready=_ready,
)
Valkey with persistence off and 128 databases.
Conflict ¶
Bases: DockerError
The name is taken, or the object is still in use (HTTP 409).
Source code in src/pytest_servex/errors.py
DockerError ¶
Bases: ServexError
The Docker API answered with an error status.
| PARAMETER | DESCRIPTION |
|---|---|
status
|
The HTTP status code.
TYPE:
|
message
|
Docker's own error message.
TYPE:
|
Source code in src/pytest_servex/errors.py
__init__ ¶
DockerUnavailable ¶
Bases: ServexError, ConnectionError
The Docker API socket is missing or refuses connections (is the daemon running?).
ImageNotFound ¶
NotFound ¶
Bases: DockerError, LookupError
Docker has no such container, network, volume, or image (HTTP 404).
Source code in src/pytest_servex/errors.py
NotReady ¶
Bases: ServexError, TimeoutError
A container did not pass its ready check: it timed out, or it exited first.
The container is removed before this is raised. logs holds its last lines.
Source code in src/pytest_servex/errors.py
__init__ ¶
Keep the image and the container's last log lines.
ServexError ¶
PostgresDatabase
dataclass
¶
One database on the run's server, owned by one worker.
Source code in src/pytest_servex/postgres.py
sqlalchemy_url ¶
Return url with a SQLAlchemy driver in the scheme (postgresql+psycopg://).
PostgresServer
dataclass
¶
A Postgres server for this run, as a URL to its admin (postgres) database.
Only strings: safe to hand to forked or spawned workers.
Source code in src/pytest_servex/postgres.py
Bind
dataclass
¶
Mount host path source at target inside a container. Read-only by default.
source is a path on the machine that runs the Docker daemon (with a remote
DOCKER_HOST, that is not the test machine). A relative path is made absolute
against the current directory. (#1)
Source code in src/pytest_servex/spec.py
Container
dataclass
¶
A container to start.
| PARAMETER | DESCRIPTION |
|---|---|
image
|
Image reference. Pin a tag; it is pulled only when missing.
TYPE:
|
ports
|
Container ports to publish on random host ports.
TYPE:
|
env
|
Environment variables. Stored sorted and read-only.
TYPE:
|
command
|
Replaces the image's default command when not empty.
TYPE:
|
mounts
|
|
networks
|
Networks to join. Start them first.
TYPE:
|
alias
|
The DNS name other containers on the same networks use.
TYPE:
|
ready
|
Returns True once the container accepts real work. Called in a loop until
True or until the start timeout. Return False for "not yet"; do not raise.
The default treats a started container as ready. Speak the service's own
protocol (a query, a
TYPE:
|
pull
|
When to pull the image, with the
TYPE:
|
entrypoint
|
Replaces the image's entrypoint when not empty.
TYPE:
|
user
|
TYPE:
|
working_dir
|
The working directory inside the container.
TYPE:
|
memory
|
Memory limit in bytes.
TYPE:
|
shm_size
|
Size of
TYPE:
|
host_config
|
Extra
TYPE:
|
Source code in src/pytest_servex/spec.py
__post_init__ ¶
Freeze env and host_config so a spec cannot change after it is made.
Source code in src/pytest_servex/spec.py
__hash__ ¶
Hash by value, with env as its sorted items.
Source code in src/pytest_servex/spec.py
Endpoint
dataclass
¶
Mount
dataclass
¶
Network
dataclass
¶
RunningContainer
dataclass
¶
A container that start created and started.
ports maps each container port to its published host port, as sorted pairs.
Source code in src/pytest_servex/spec.py
endpoint ¶
Return where container port is reachable from the test process.
| RAISES | DESCRIPTION |
|---|---|
KeyError
|
|
Source code in src/pytest_servex/spec.py
RunningNetwork
dataclass
¶
RunningVolume
dataclass
¶
Tmpfs
dataclass
¶
A tmpfs at target inside a container: data in memory, nothing written to disk.
options are mount options, for example "size=256m".
Source code in src/pytest_servex/spec.py
Volume
dataclass
¶
ValkeyDatabase
dataclass
¶
ValkeyServer
dataclass
¶
container_fixture ¶
container_fixture(
spec: Container, *, timeout: float = 60.0
) -> Callable[[], Iterator[RunningContainer]]
Return a session fixture that starts spec once per run and yields it running.
Assign it to a name in conftest.py; that name becomes the fixture name::
api = container_fixture(Container(image="myorg/api:1.4", ports=(8080,)))
def test_health(api):
host, port = api.endpoint(8080).host, api.endpoint(8080).port
Under pytest-parallex the container starts once, before the workers fork, and every worker shares it. Under pytest-xdist each worker starts its own. The container is removed at the end of the run; containers of killed runs are removed first.
Source code in src/pytest_servex/fixtures.py
logs ¶
Return the container's stdout and stderr as text, the last tail lines if given.
Source code in src/pytest_servex/lifecycle.py
refresh ¶
Return started with the ports Docker publishes now.
A restart gives the container new host ports, and started keeps the old ones. Call
this after a restart, then use endpoint on the result.
| RAISES | DESCRIPTION |
|---|---|
NotFound
|
The container is gone. |
KeyError
|
The container is stopped, so Docker publishes no ports. |
Source code in src/pytest_servex/lifecycle.py
remove_stale ¶
Remove the containers, networks, and volumes of killed runs on this host.
A normal run removes its own objects. A run killed with SIGKILL cannot, so the next run does it here. This replaces the Ryuk container testcontainers runs alongside each session. Returns how many objects were removed.
Source code in src/pytest_servex/lifecycle.py
running ¶
Start spec on enter and stop it on exit, even when the block raises.
Nest them in dependency order and they stop in reverse::
with running(net), running(pg) as started:
started.endpoint(5432)
Source code in src/pytest_servex/lifecycle.py
start ¶
Create spec in Docker and return what was started.
A container is started and waited on until spec.ready is True. Its networks and
volumes must exist already: start them first.
| RAISES | DESCRIPTION |
|---|---|
NotReady
|
The container was not ready within |
ImageNotFound
|
The image is missing and |
Conflict
|
A network or volume with that name exists already. |
DockerUnavailable
|
The Docker API cannot be reached. |
Source code in src/pytest_servex/lifecycle.py
stop ¶
Remove what start created. Something already gone is fine.
A container is removed with its anonymous volumes. A network or volume still in use
by a container raises Conflict: stop containers first.
Source code in src/pytest_servex/lifecycle.py
http_ready ¶
Return a check that passes once GET path on container port gets any answer below 500.
A 401 or 404 still means the server is up, which is what readiness asks.
Source code in src/pytest_servex/ready.py
log_ready ¶
Return a check that passes once pattern (a regex) matched times log lines.
times covers images that print the same line twice, such as Postgres, which logs
"ready to accept connections" once for its init server and once for the real one.
Prefer a protocol check where one exists: a log line can appear before the port is
served. (#2)