3

I want to write a unit test in Django for a function which has a celery send_task function.

How do I write its UT in Django and mock celery?

def push_to_queue(self, data):
        """ function to add task in the backend queue
        Args:
            Accept data dictionary
        returns: Nothing
        """
        try:
            LOG.info(
                "Pushing %s to queue %s " % (data, settings.SCHEDULER_STATUS_QUEUE_NAME)
            )
            # sqs handler
            handle = Celery("scheduler_service", broker="sqs://")
            handle.send_task(
                settings.BACKEND_TASK,
                queue=settings.SCHEDULER_STATUS_QUEUE_NAME,
                kwargs=data,
            )
            LOG.info(
                "Pushed message successfully to %s queue."
                % settings.SCHEDULER_STATUS_QUEUE_NAME
            )
        except Exception as err:
            LOG.error("Exception while pusing meesage to queue %s " % str(err))
2

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.