2

Azure provide documentation for unit testing HTTP and Queue Python functions. This is done by creating a mock QueueMessage or HTTPRequest. However, there is no documentation for creating a unit test for Timer functions.

There is a TimerRequest class, but creating a new instance of it fails with the following error:

req = func.TimerRequest()

TypeError: Can't instantiate abstract class TimerRequest with abstract methods past_due

What is the correct approach for creating a unit test for a Python Azure timer function?

4
  • can't you just abstract your actual business logic into another routine and the timer-triggered Function is doing nothing else but calling that routine? Then you can also write your unit test against that one. Or what do you want to test about the Timer in specific? Commented Aug 11, 2020 at 9:39
  • I could do that but that's a workaround for inability to test the actual function, as is possible for Queue and HTTP trigger types. I want to test the function as it would be called. Commented Aug 11, 2020 at 11:23
  • again my question: what do you really want to test? the trimer trigger itself has no inputs (compared to the HTTP or queue-triggered functions) Commented Aug 11, 2020 at 12:11
  • It has no inputs but it can run with the attribute past_due set or not. Commented Aug 11, 2020 at 12:35

1 Answer 1

4

The accepted answer wasn't working for me anymore, in the end I created a class with the past_due attribute set to True, and that did the trick:

import unittest
from azure.functions import TimerRequest
from my_func import main

class MockTimer():
    def __init__(self):
        self.past_due = True


class TestFunction(unittest.TestCase):
    def test_my_function(self):
        timer = MockTimer()
        resp = main(timer)
Sign up to request clarification or add additional context in comments.

Comments

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.