A Pytest fixture for managing Google Cloud Firestore emulator
Project description
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 (
gcloudCLI) 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.
Project details
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_firestore-1.1.1.tar.gz.
File metadata
- Download URL: pytest_firestore-1.1.1.tar.gz
- Upload date:
- Size: 7.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
03f2ba88052f7d7bf7fc21da95aa0402b7f90da33a79e7abbf00f339874ebc39
|
|
| MD5 |
1e6fbcf81dff48e7baffd2b205262433
|
|
| BLAKE2b-256 |
7899cf16196556e4cfc46a6cfec82827aef4bfcda027c6012f0aea3142c5560b
|
Provenance
The following attestation bundles were made for pytest_firestore-1.1.1.tar.gz:
Publisher:
publish.yml on nealepetrillo/pytest-firestore
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pytest_firestore-1.1.1.tar.gz -
Subject digest:
03f2ba88052f7d7bf7fc21da95aa0402b7f90da33a79e7abbf00f339874ebc39 - Sigstore transparency entry: 1076144189
- Sigstore integration time:
-
Permalink:
nealepetrillo/pytest-firestore@b666b407cd3268d09f71cd686759744eb70b6672 -
Branch / Tag:
refs/tags/1.1.1 - Owner: https://github.com/nealepetrillo
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@b666b407cd3268d09f71cd686759744eb70b6672 -
Trigger Event:
release
-
Statement type:
File details
Details for the file pytest_firestore-1.1.1-py3-none-any.whl.
File metadata
- Download URL: pytest_firestore-1.1.1-py3-none-any.whl
- Upload date:
- Size: 8.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6ab945a8d6e6a9cbd6df0c73b1e6149b5505dd0d680911252fd5193bec022214
|
|
| MD5 |
c81ba6a59d9cabcc23d395b139475d30
|
|
| BLAKE2b-256 |
53548fcc5c11b07c6e00af132d0b9ce3843a88b2490125449fc98ef501187c67
|
Provenance
The following attestation bundles were made for pytest_firestore-1.1.1-py3-none-any.whl:
Publisher:
publish.yml on nealepetrillo/pytest-firestore
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pytest_firestore-1.1.1-py3-none-any.whl -
Subject digest:
6ab945a8d6e6a9cbd6df0c73b1e6149b5505dd0d680911252fd5193bec022214 - Sigstore transparency entry: 1076144226
- Sigstore integration time:
-
Permalink:
nealepetrillo/pytest-firestore@b666b407cd3268d09f71cd686759744eb70b6672 -
Branch / Tag:
refs/tags/1.1.1 - Owner: https://github.com/nealepetrillo
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@b666b407cd3268d09f71cd686759744eb70b6672 -
Trigger Event:
release
-
Statement type: