Skip to content

Latest commit

 

History

History
82 lines (62 loc) · 2.51 KB

File metadata and controls

82 lines (62 loc) · 2.51 KB

AzureFunctions.TestFramework.Timer

NuGet

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.

Usage

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();
    }
}

API

Task<FunctionInvocationResult> InvokeTimerAsync(
    this IFunctionsTestHost host,
    string functionName,
    TimerInfo? timerInfo = null,
    CancellationToken cancellationToken = default)
  • functionName — the name of the timer function (case-insensitive).
  • timerInfo — optional TimerInfo passed to the function. When null, a default TimerInfo with IsPastDue = false and no schedule status is used.

Output binding capture

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);

References

License

MIT