A Pytest fixture for managing Google Cloud Platform PubSub emulator
Project description
pytest-gcppubsub
A pytest plugin that manages the GCP Pub/Sub emulator lifecycle. Start the emulator automatically when your tests run — no manual setup required.
Features
- Automatic emulator management — starts
gcloud beta emulators pubsub startbefore tests and stops it after - pytest-xdist support — parallel workers share a single emulator instance via file-lock coordination
- Environment configuration — sets
PUBSUB_EMULATOR_HOSTandPUBSUB_PROJECT_IDsogoogle-cloud-pubsubclients connect automatically - Auto port assignment — use
--pubsub-port=0to pick a free port, avoiding conflicts - Async compatible — session-scoped fixture works with
pytest-asyncioout of the box
Prerequisites
The Google Cloud SDK must be installed with the Pub/Sub emulator component:
gcloud components install pubsub-emulator
Installation
pip install pytest-gcppubsub
To also install the google-cloud-pubsub client library (for the optional client fixtures):
pip install pytest-gcppubsub[client]
Quick Start
Request the pubsub_emulator fixture in your tests:
def test_publish_message(pubsub_emulator):
from google.cloud import pubsub_v1
publisher = pubsub_v1.PublisherClient()
topic_path = publisher.topic_path(pubsub_emulator.project, "my-topic")
publisher.create_topic(request={"name": topic_path})
future = publisher.publish(topic_path, b"hello world")
future.result()
The plugin starts the emulator once per session and sets the environment variables so all google-cloud-pubsub clients route to it automatically.
Fixtures
pubsub_emulator (session-scoped)
Starts the Pub/Sub emulator and yields an EmulatorInfo object:
| Attribute | Type | Description |
|---|---|---|
host |
str |
Emulator host (e.g. localhost) |
port |
int |
Emulator port |
project |
str |
GCP project ID |
host_port |
str |
Combined host:port string |
Sets PUBSUB_EMULATOR_HOST and PUBSUB_PROJECT_ID environment variables for the session and restores them on teardown.
pubsub_publisher_client (function-scoped)
Returns a pubsub_v1.PublisherClient connected to the emulator. Skips the test if google-cloud-pubsub is not installed.
pubsub_subscriber_client (function-scoped)
Returns a pubsub_v1.SubscriberClient connected to the emulator. Skips the test if google-cloud-pubsub is not installed.
Configuration
Settings can be provided via CLI flags or pyproject.toml / pytest.ini. CLI flags take precedence.
| CLI Flag | ini Option | Default | Description |
|---|---|---|---|
--pubsub-host |
pubsub_emulator_host |
localhost |
Emulator bind host |
--pubsub-port |
pubsub_emulator_port |
8085 |
Emulator port (0 for auto) |
--pubsub-project |
pubsub_project_id |
test-project |
GCP project ID |
--pubsub-timeout |
pubsub_emulator_timeout |
15 |
Startup timeout (seconds) |
Example pyproject.toml:
[tool.pytest.ini_options]
pubsub_project_id = "my-test-project"
pubsub_emulator_port = "0"
pytest-xdist Support
When running with pytest-xdist, the plugin coordinates workers so that only the first worker starts the emulator. Subsequent workers attach to the running instance. The last worker to finish tears it down. This uses file-lock based coordination and handles stale processes from crashed runs.
pytest -n auto # all workers share one emulator
Async Tests
The pubsub_emulator fixture is session-scoped and synchronous, which is compatible with async test functions. Since PUBSUB_EMULATOR_HOST is set in the environment, async clients like PublisherAsyncClient connect to the emulator automatically:
import pytest
from google.cloud.pubsub_v1 import PublisherAsyncClient
@pytest.fixture
async def async_publisher(pubsub_emulator):
return PublisherAsyncClient()
async def test_async_publish(async_publisher, pubsub_emulator):
topic = f"projects/{pubsub_emulator.project}/topics/my-topic"
await async_publisher.create_topic(request={"name": topic})
License
MIT
Project details
Release history Release notifications | RSS feed
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_gcppubsub-1.0.0.tar.gz.
File metadata
- Download URL: pytest_gcppubsub-1.0.0.tar.gz
- Upload date:
- Size: 6.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e6130c9184aa4e194bc7c486c9cc2bb2f39e6db5d45f6ea3ee3caf8d68122120
|
|
| MD5 |
e5eff9c6266863631a50e327e26a5625
|
|
| BLAKE2b-256 |
051db1a27a8e28b7f837d71d2f9d60ddc5e9cf7fcde98a8918894d4b00287d7b
|
Provenance
The following attestation bundles were made for pytest_gcppubsub-1.0.0.tar.gz:
Publisher:
publish.yml on nealepetrillo/pytest-gcppubsub
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pytest_gcppubsub-1.0.0.tar.gz -
Subject digest:
e6130c9184aa4e194bc7c486c9cc2bb2f39e6db5d45f6ea3ee3caf8d68122120 - Sigstore transparency entry: 1076217504
- Sigstore integration time:
-
Permalink:
nealepetrillo/pytest-gcppubsub@4700f6b4570ad7a8c0b17bf1c4999b84e0af61e6 -
Branch / Tag:
refs/tags/v1.0.0 - Owner: https://github.com/nealepetrillo
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@4700f6b4570ad7a8c0b17bf1c4999b84e0af61e6 -
Trigger Event:
release
-
Statement type:
File details
Details for the file pytest_gcppubsub-1.0.0-py3-none-any.whl.
File metadata
- Download URL: pytest_gcppubsub-1.0.0-py3-none-any.whl
- Upload date:
- Size: 8.2 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 |
80482e819f0d4c15952ed6973a39a9c75deabc788d1dfc3d0b057d6e0495e863
|
|
| MD5 |
d413625f4ae3b033330af1f7f9a7afd4
|
|
| BLAKE2b-256 |
c26042ce5aa9e45caf8950a26825fd1342d7ffe8aaddba1b2b3e318acb152203
|
Provenance
The following attestation bundles were made for pytest_gcppubsub-1.0.0-py3-none-any.whl:
Publisher:
publish.yml on nealepetrillo/pytest-gcppubsub
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pytest_gcppubsub-1.0.0-py3-none-any.whl -
Subject digest:
80482e819f0d4c15952ed6973a39a9c75deabc788d1dfc3d0b057d6e0495e863 - Sigstore transparency entry: 1076217554
- Sigstore integration time:
-
Permalink:
nealepetrillo/pytest-gcppubsub@4700f6b4570ad7a8c0b17bf1c4999b84e0af61e6 -
Branch / Tag:
refs/tags/v1.0.0 - Owner: https://github.com/nealepetrillo
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@4700f6b4570ad7a8c0b17bf1c4999b84e0af61e6 -
Trigger Event:
release
-
Statement type: