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.1.0.tar.gz (6.1 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.1.0-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pytest_gcppubsub-0.1.0.tar.gz
  • Upload date:
  • Size: 6.1 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.1.0.tar.gz
Algorithm Hash digest
SHA256 b90bc9474a4274b912cc7e2e57d07d4bd9b2e87584bf4725e7e9948024c9e6e8
MD5 b8d1aaa95a68b6b63fd2010b6bd0c767
BLAKE2b-256 ad3bba5e3077dc71fa8c7d2a7dee53ff79df8ffaf6337550fc1d670181d8d9e5

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_gcppubsub-0.1.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.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_gcppubsub-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 64bb6a3040776bd0cd8c36fc6c7a6e916bdbbe573eea8779ec2aa91245688a8b
MD5 f49c010917a031c54bc0ec52deee73cb
BLAKE2b-256 2f5bf809e92eb052c4b3489783693a3ec110dff99951fc9c34c27fb214c5d063

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_gcppubsub-0.1.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