In a recent post, I described a way to run integration tests against precompiled C# Azure Functions using the v2 runtime. In that post, we looked at an example of invoking an HTTP-triggered function from within an integration test.
Of course, there are plenty of other triggers available for Azure Functions too. Recently I needed to write an integration test against a timer-triggered function and decided to investigate the best way to do this.
The Azure Functions runtime provides a convenient API for invoking a timer-trigger function. You can issue an HTTP
POST request against an endpoint for your function, and the function runtime will start up and trigger the function. I wasn't able to find any proper documentation about this, so this blog post is a result of some of my experimentation.
To invoke a timer-triggered function named
MyFunction, you need to issue an HTTP request as follows:
Replace with either your real Azure Functions hostname - such as
myfunctionsapp.azurewebsites.net - or, if you're running through the Azure Functions CLI, use
localhost and the port number you're running it on, such as
Interestingly, invoking this endpoint immediately returns an HTTP 201 response, but the function runs separately. This makes sense, though, since timer-trigger functions are not intended to return data in the same way that HTTP-triggered functions are.
I've created an updated version of the GitHub repository from the previous post with an example of running a test against a timer-triggered function. In this example, the function simply writes a message to an Azure Storage queue, which we can then look at to confirm the function has run. Normally the function would only run once a week, at 9.30am on Mondays, but our integration test triggers it each time it runs to verify that it works correctly.
In this version of the test fixture we also wait for the app to start before our test runs. We do this by polling the
/ endpoint with
GET requests until it responds. This ensures that we can access the timer invocation HTTP endpoint successfully from our tests.
Of course, just like in the previous post's integration tests, a timer-triggered integration test can run from an Azure Pipelines build too, so you can include timer-triggered functions in your continuous integration and testing practices alongside the rest of your code. In fact, the same
build.yaml that we used in the previous post can be used to run these tests, too.