Class AzureFunctionTestFactory<TEntryPoint>

Namespace
XBullet.EasyTesting.AzureFunctions
Assembly
XBullet.EasyTesting.AzureFunctions.dll

Reuses an isolated Functions application's entry point and container for in-process tests.

public class AzureFunctionTestFactory<TEntryPoint> : IAsyncDisposable where TEntryPoint : class

Type Parameters

TEntryPoint

A type in the executable Functions assembly, commonly its public partial Program class.

Inheritance
AzureFunctionTestFactory<TEntryPoint>
Implements
Inherited Members
Extension Methods

Remarks

The factory owns one lazily created test host. Program runs until its first generic host is built; hosted services and worker transport are never started by this factory. Register test invocation middleware and scenario resources through ConfigureTestHost(AzureFunctionTestHostBuilder). Dispose active scenarios before disposing this factory. Startup code preceding host construction still executes normally.

Constructors

AzureFunctionTestFactory()

Creates a factory that lazily runs the Functions entry point.

public AzureFunctionTestFactory()

Properties

StartupTimeout

Gets the maximum wait for Program to build its host.

protected virtual TimeSpan StartupTimeout { get; }

Property Value

TimeSpan

A positive finite timer duration, defaulting to thirty seconds.

Remarks

Cancellation or timeout cannot interrupt application code; a late-built host is stopped and disposed.

Methods

ConfigureHost(IHostBuilder)

Applies test configuration to the production host before it builds services.

protected virtual void ConfigureHost(IHostBuilder builder)

Parameters

builder IHostBuilder

The borrowed generic host builder adapter. Do not build or retain it.

Remarks

Called during HostBuilding. HostApplicationBuilder may reject changes to already-initialized host settings.

ConfigureTestHost(AzureFunctionTestHostBuilder)

Configures function registrations, test invocation middleware, and scenario resources.

protected virtual void ConfigureTestHost(AzureFunctionTestHostBuilder builder)

Parameters

builder AzureFunctionTestHostBuilder

The borrowed builder over the production service collection. Do not build or retain it.

Remarks

Production function classes are discovered automatically. Worker-native middleware is not replayed automatically.

ConfigureTestServices(IServiceCollection)

Applies final dependency overrides after production and test-host registrations.

protected virtual void ConfigureTestServices(IServiceCollection services)

Parameters

services IServiceCollection

The borrowed production service collection. Do not build a separate provider or retain it.

CreateHostAsync(CancellationToken)

Gets or constructs the factory-owned function test host.

public Task<AzureFunctionTestHost> CreateHostAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

Cancels gate acquisition and the startup wait.

Returns

Task<AzureFunctionTestHost>

The borrowed, cached host. Dispose its scenarios before disposing the factory; do not dispose this host separately.

Remarks

Calls are serialized. Completed initialization failures can be retried. Application code continuing after a canceled startup wait can outlive that wait and its configuration callbacks.

Exceptions

ObjectDisposedException

The factory has been disposed.

ArgumentNullException

GetArguments() returned null.

ArgumentOutOfRangeException

StartupTimeout is not a positive finite timer duration.

InvalidOperationException

The entry-point assembly is not executable, arguments contain null elements, or Program exits without building a supported host.

OperationCanceledException

cancellationToken canceled gate acquisition or the startup wait.

TimeoutException

Program did not build its host within StartupTimeout.

DisposeAsync()

Disposes the factory-owned host and its service provider asynchronously.

public ValueTask DisposeAsync()

Returns

ValueTask

A task representing disposal of the host and its owned services.

Remarks

Dispose active scenarios first. An active scenario rejects disposal without closing the factory, allowing disposal to be retried. Service disposal failures propagate and leave the factory closed. Repeated disposal after the factory has closed has no effect.

Exceptions

InvalidOperationException

A scenario is still active on the factory-owned host.

GetArguments()

Supplies command-line arguments passed to Program.

protected virtual string[] GetArguments()

Returns

string[]

A non-null array with non-null elements, defaulting to an empty array. It is copied before execution.

See Also