1

I'm working on a flask API, which one of the endpoint is to receive a message and publish it to PubSub. Currently, in order to test that endpoint, I will have to manually spin-up a PubSub emulator from the command line, and keep it running during the test. It working just fine, but it wouldn't be ideal for automated test.

I wonder if anyone knows a way to spin-up a test PubSub emulator from python? Or if anyone has a better solution for testing such an API?

xiu shi
  • 727
  • 8
  • 22

2 Answers2

1

As far as I know, there is no Python native Google Cloud PubSub emulator available.

You have few options, all of them require launching an external program from Python:

  • Just invoke the gcloud command you mentioned: gcloud beta emulators pubsub start [options] directly from your python application to start this as an external program.

  • The PubSub emulator which comes as part of Cloud SDK is a JAR file bootstrapped by the bash script present in CLOUD_SDK_INSTALL_DIR/platform/pubsub-emulator/bin/cloud-pubsub-emulator. You could possibly run this bash script directly.

Here is a StackOverflow answer which covers multiple ways to launch an external program from Python.

Also, it is not quite clear from your question how you're calling the PubSub APIs in Python.

  • For unit tests, you could consider setting up a wrapper over the code which actually invokes the Cloud PubSub APIs, and inject a fake for this API wrapper. This way, you can test the rest of the code which invokes just your fake API wrapper instead of the real API wrapper and not worry about starting any external programs.

  • For integration tests, the PubSub emulator will definitely be useful.

Tuxdude
  • 47,485
  • 15
  • 109
  • 110
0

This is how I usually do:

1. I create a python client class which does publish and subscribe with the topic, project and subscription used in emulator.

Note: You need to set PUBSUB_EMULATOR_HOST=localhost:8085 as env in your python project.

2. I spin up a pubsub-emulator as a docker container.

Note: You need to set some envs, mount volumes and expose port 8085.

set following envs for container:

  • PUBSUB_EMULATOR_HOST
  • PUBSUB_PROJECT_ID
  • PUBSUB_TOPIC_ID
  • PUBSUB_SUBSCRIPTION_ID
  1. Write whatever integration tests you want to. Use publisher or subscriber from client depending on your test requirements.
bh4r4th
  • 3,760
  • 1
  • 21
  • 25