0

I am new to python and want to write unit testcase for pub/sub asynchronous pull feature . I understand that we need to mock the function for subscriber,event message but I am not sure how that can be done. Also how to test the assertion when we are creating the stream and not returning anything .

from google.cloud import pubsub_v1

def pubsub_listener():
   project_id = <<Environment Variable>>
   subscription_id = <<Environment Variable>>
   subscriber = pubsub_v1.SubscriberClient()
   subscription_path = subscriber.subscription_path(project_id, subscription_id)

   def callback(message: pubsub_v1.subscriber.message.Message) -> None:
      logging.info(f"Received {message}.")
      message.ack()

   streaming_pull_future = subscriber.subscribe(subscription_path, callback=callback)
   logging.info(f"Listening for messages on {subscription_path}..\n")

   with subscriber:
      try:
          streaming_pull_future.result(timeout=timeout)
      except TimeoutError:
          streaming_pull_future.cancel() 
          streaming_pull_future.result()  
if __name__ =='__main__':
    pubsub_listener()
     

1 Answers1

2

It's being described here; for example, where data and mock_context are variable:

def test_pubsub_listener(capsys):
    data = {}
    mock_context = mock.Mock()
    main.pubsub_listener(data, mock_context)
    out, err = capsys.readouterr()
    assert ...

Just unsure what to assert, while not knowing what it might return,
it should respond with "Listening for messages on" and "Received".

Unwrapping pubsub_listener.callback(data) might make it more testable,
because this is the part which processes the actual pub/sub event payload.

Writing unit-tests requires testable code to begin with... which means,
you'd have to pass project_id and subscription_id into the function.

This is what makes it not testable - and also the portability is poor:

project_id = "your-project-id"
subscription_id = "your-subscription-id"
Martin Zeitler
  • 1
  • 19
  • 155
  • 216
  • That example is for a cloud function but here I am using this application on GCE . Hence I am not passing any context to my function – ForeverStudent Jul 12 '22 at 05:56
  • 1
    Assuming this is a pub/sub listener, it should still take some kind of event as data argument. Void functions, which return void are difficult to unit-test. One can often only call them and check for no exception, as there's nothing to assert. Better to test functions which take an argument and return something. – Martin Zeitler Jul 12 '22 at 06:00
  • Possibly related: https://stackoverflow.com/questions/31739455/how-to-do-junit-test-with-google-cloud-pub-sub – Martin Zeitler Jul 12 '22 at 06:23