Azure / azure-functions-python-worker

Python worker for Azure Functions.
http://aka.ms/azurefunctions
MIT License
331 stars 100 forks source link

How to test ServiceBus Trigger? Create ServiceBusMessage in unit tests #1321

Closed karpikpl closed 7 months ago

karpikpl commented 9 months ago

Is your question related to a specific version? If so, please specify: no

What binding does your question apply to, if any? (e.g. Blob Trigger, Event Hub Binding, etc)

ServiceBus

Question

How to create ServiceBusMessage in unit tests? Could you add examples for unit testing bindings other than http?

I'm getting "does not accept parameters" error when I try to create the ServiceBusMessage.

karpikpl commented 9 months ago

Unless I'm missing something in my code, in order to test Service Bus trigger functions, ServiceBusMessage needs to be mocked?

from unittest.mock import Mock
import azure.functions as func

def test_your_function():
    mock_msg = Mock(spec=func.ServiceBusMessage)
    mock_msg.get_body.return_value = b"Your mock message body"

    # Call your Azure function
    result = your_function(mock_msg)

I'm wondering why HTTP Request has constructor but ServiceBusMessage doesn't?