Skip to main content

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 start before tests and stops it after
  • pytest-xdist support — parallel workers share a single emulator instance via file-lock coordination
  • Environment configuration — sets PUBSUB_EMULATOR_HOST and PUBSUB_PROJECT_ID so google-cloud-pubsub clients connect automatically
  • Auto port assignment — use --pubsub-port=0 to pick a free port, avoiding conflicts
  • Async compatible — session-scoped fixture works with pytest-asyncio out 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest_gcppubsub-0.2.0.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytest_gcppubsub-0.2.0-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file pytest_gcppubsub-0.2.0.tar.gz.

File metadata

  • Download URL: pytest_gcppubsub-0.2.0.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pytest_gcppubsub-0.2.0.tar.gz
Algorithm Hash digest
SHA256 661a4d81fd0b3021e8f89d07ff7d5485ddf39532f63b4532652c5561929a47ad
MD5 d65b1ecc7d629337285a9bb96e093176
BLAKE2b-256 174ab8bcd31b361c3c1bd0b4b931ad12f6f29ed473fdfb159f3f27a1ba0f6847

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_gcppubsub-0.2.0.tar.gz:

Publisher: publish.yml on nealepetrillo/pytest-gcppubsub

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pytest_gcppubsub-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_gcppubsub-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3950697a1110130a7568e37b8919b5be01b7678ea4b8c0dede10951c1da18f08
MD5 b365a986b3abb53eaede8c1c9c555869
BLAKE2b-256 8820e0cce68b648217c769a2b4e3ca606ca282dc8607d668030e1d8d7da7d2d5

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_gcppubsub-0.2.0-py3-none-any.whl:

Publisher: publish.yml on nealepetrillo/pytest-gcppubsub

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page