TimerTrigger invocation support for the Azure Functions Test Framework. Provides InvokeTimerAsync(...) — an extension on IFunctionsTestHost that lets you trigger timer-triggered functions directly from integration tests without scheduling or waiting.
using AzureFunctions.TestFramework.Core;
using AzureFunctions.TestFramework.Timer;
using Microsoft.Azure.Functions.Worker;
public class TimerFunctionTests : IAsyncLifetime
{
private IFunctionsTestHost _testHost;
public async Task InitializeAsync()
{
_testHost = await new FunctionsTestHostBuilder()
.WithFunctionsAssembly(typeof(MyTimerFunction).Assembly)
.BuildAndStartAsync();
}
[Fact]
public async Task HeartbeatTimer_RunsSuccessfully()
{
// Invoke with default TimerInfo (IsPastDue = false)
var result = await _testHost.InvokeTimerAsync("HeartbeatTimer");
Assert.True(result.Success);
}
[Fact]
public async Task HeartbeatTimer_WhenPastDue_RunsSuccessfully()
{
var timerInfo = new TimerInfo { IsPastDue = true };
var result = await _testHost.InvokeTimerAsync("HeartbeatTimer", timerInfo);
Assert.True(result.Success);
}
public async Task DisposeAsync()
{
await _testHost.StopAsync();
_testHost.Dispose();
}
}Task<FunctionInvocationResult> InvokeTimerAsync(
this IFunctionsTestHost host,
string functionName,
TimerInfo? timerInfo = null,
CancellationToken cancellationToken = default)functionName— the name of the timer function (case-insensitive).timerInfo— optionalTimerInfopassed to the function. Whennull, a defaultTimerInfowithIsPastDue = falseand no schedule status is used.
Use FunctionInvocationResult to inspect output bindings produced by the function:
var result = await _testHost.InvokeTimerAsync("HeartbeatTimer");
Assert.True(result.Success);
// Read a named output binding
var message = result.ReadOutputAs<string>("OutputMessage");
Assert.Equal("heartbeat", message);MIT