Skip to main content

pytest-firestore

A pytest plugin that manages a Google Cloud Firestore emulator instance for your test session. It starts the emulator before tests run, sets the FIRESTORE_EMULATOR_HOST environment variable so client libraries connect automatically, and tears everything down when the session ends.

Supports pytest-xdist out of the box — multiple workers share a single emulator process via file-lock coordination.

Requirements

  • Python >= 3.12
  • pytest >= 7.0
  • Google Cloud SDK (gcloud CLI) with the Firestore emulator component:
gcloud components install cloud-firestore-emulator

Installation

pip install pytest-firestore

To also install a Firestore client for use with the convenience fixtures:

pip install pytest-firestore[client]

Quick start

Request the firestore_emulator fixture in any test. The emulator starts once per session.

def test_write_and_read(firestore_emulator):
    from google.cloud import firestore

    db = firestore.Client(project=firestore_emulator.project)
    db.collection("users").document("alice").set({"name": "Alice"})
    doc = db.collection("users").document("alice").get()
    assert doc.to_dict()["name"] == "Alice"

Or use the ready-made client fixtures (requires google-cloud-firestore):

def test_with_client(firestore_client):
    firestore_client.collection("items").document("1").set({"n": 1})
    assert firestore_client.collection("items").document("1").get().exists


async def test_async(firestore_async_client):
    await firestore_async_client.collection("items").document("2").set({"n": 2})
    doc = await firestore_async_client.collection("items").document("2").get()
    assert doc.exists

Fixtures

Fixture Scope Description
firestore_emulator session Starts the emulator and returns an EmulatorInfo object. Sets FIRESTORE_EMULATOR_HOST for the duration of the session.
firestore_client function Returns a google.cloud.firestore.Client connected to the emulator. Skips if google-cloud-firestore is not installed.
firestore_async_client function Returns a google.cloud.firestore.AsyncClient connected to the emulator. Skips if google-cloud-firestore is not installed.

EmulatorInfo

The firestore_emulator fixture yields an EmulatorInfo dataclass with the following attributes:

Attribute Type Description
host str Emulator hostname (e.g. "localhost")
port int Emulator port (e.g. 8080)
project str GCP project ID (e.g. "test-project")
host_port str Combined "host:port" string

Configuration

Settings can be provided via CLI flags or pyproject.toml / pytest.ini. CLI flags take precedence over ini values.

CLI flag ini option Default Description
--firestore-host firestore_emulator_host localhost Emulator bind host
--firestore-port firestore_emulator_port 8080 Emulator port. Use 0 to auto-select a free port.
--firestore-project firestore_project_id test-project GCP project ID passed to the emulator
--firestore-timeout firestore_emulator_timeout 15 Seconds to wait for the emulator to accept connections

Example pyproject.toml

[tool.pytest.ini_options]
firestore_emulator_host = "localhost"
firestore_emulator_port = "0"          # auto-select a free port
firestore_project_id = "my-test-project"
firestore_emulator_timeout = "30"

Example CLI usage

pytest --firestore-port 0 --firestore-project my-project

Using with pytest-xdist

No extra configuration is needed. When tests run under xdist, the plugin detects worker processes and coordinates through a shared lock file so that only the first worker starts the emulator. All other workers join the existing instance. When the last worker finishes, the emulator is terminated.

pytest -n auto

Auto-port selection

Set the port to 0 to have the plugin pick a free port automatically. This is useful in CI environments where port conflicts may occur:

pytest --firestore-port 0

Environment variable

While the session fixture is active, FIRESTORE_EMULATOR_HOST is set to host:port. This is the standard variable that google-cloud-firestore client libraries check to route traffic to the emulator instead of production. The original value (if any) is restored after the session ends.

Development

# Install in editable mode with dev dependencies
pip install -e ".[dev]"

# Run unit tests (no gcloud required)
pytest tests/test_emulator_unit.py tests/test_plugin_unit.py -v

# Run integration tests (requires gcloud + emulator component)
pytest tests/test_plugin_integration.py -v

# Lint and type-check
ruff check .
mypy pytest_firestore

License

MIT — see LICENSE.

Release files for pytest-firestore 1.1.1

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-firestore 1.1.1
File Size Uploaded
pytest_firestore-1.1.1.tar.gz 7.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytest-firestore 1.1.1
File Interpreter ABI Platform
pytest_firestore-1.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 15.5 kB

Release files / pytest_firestore-1.1.1.tar.gz

Download URL pytest_firestore-1.1.1.tar.gz
Size 7.0 kB
Tags Source
SHA-256 checksum
How to use checksums
03f2ba88052f7d7bf7fc21da95aa0402b7f90da33a79e7abbf00f339874ebc39
BLAKE2b-256 checksum
How to use checksums
7899cf16196556e4cfc46a6cfec82827aef4bfcda027c6012f0aea3142c5560b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Mar 10, 2026.

Transparency log

Release files / pytest_firestore-1.1.1-py3-none-any.whl

Download URL pytest_firestore-1.1.1-py3-none-any.whl
Size 8.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6ab945a8d6e6a9cbd6df0c73b1e6149b5505dd0d680911252fd5193bec022214
BLAKE2b-256 checksum
How to use checksums
53548fcc5c11b07c6e00af132d0b9ce3843a88b2490125449fc98ef501187c67
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Mar 10, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

1.1.1 This release

2 release files

1.1.0

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.1.0

2 release 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