Skip to content

[BUG] It is not possible to unit test a Python timer function #737

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
davidmytton opened this issue Aug 15, 2020 · 4 comments · May be fixed by Azure/azure-functions-python-library#286
Open
Assignees
Labels

Comments

@davidmytton
Copy link

Azure provides 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

Having posted on StackOverflow to ask about the correct approach for this e.g. if I want to test behaviour of a function which has past_due set, but had no answers, I'm opening this bug to request that Python functions allow unit testing.

@anthonychu
Copy link
Member

@davidmytton
Copy link
Author

Thanks, this worked. Can I suggest that this is added to the docs as an example alongside the others?

@joaosantos15
Copy link

joaosantos15 commented Jul 9, 2021

That 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 (also on stack overflow):

    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)

@hallvictoria
Copy link
Contributor

hallvictoria commented May 28, 2025

Hi everyone, sorry for the delay on this. We're currently working on a fix that will allow for unit testing of TimerRequest without the additional workarounds. You'll be able to create a TimerRequest object like this: func.TimerRequest(). This change will be included in azure-functions>=1.25.0.

I've updated our wiki to show specifically the TimerRequest example. With azure-functions >= 1.21.0, you'll also be able to test the functions by invoking them directly (ie, by calling python function_app.py). The wiki covers the setup, and we're in the process of updating the developer reference as well to include this information.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants