AzureFunctions.Testing
0.1.6
dotnet add package AzureFunctions.Testing --version 0.1.6
NuGet\Install-Package AzureFunctions.Testing -Version 0.1.6
<PackageReference Include="AzureFunctions.Testing" Version="0.1.6" />
paket add AzureFunctions.Testing --version 0.1.6
#r "nuget: AzureFunctions.Testing, 0.1.6"
// Install AzureFunctions.Testing as a Cake Addin #addin nuget:?package=AzureFunctions.Testing&version=0.1.6 // Install AzureFunctions.Testing as a Cake Tool #tool nuget:?package=AzureFunctions.Testing&version=0.1.6
Azure.Functions.Testing
Azure.Functions.Testing is an integration testing helper library for Azure Functions in the style of WebApplicationFactory.
The package leverages the Azure Function Core Tools to configure an launch an Azure Functions project and allow a Test project to make HTTP requests to the Function HTTP Trigger endpoints.
When running the Function via Azure Function Core Tools, additional arguments can be specified in the constructor of the FunctionApplicationFactory
.
The func start documentation defines a list of valid arguments.
The Azure.Functions.Testing NuGet package is distributed under a MIT Licence allowing the package to be freely used in commercial applications.
Example: Testing a Function app with FunctionApplicationFactory
// The FunctionApplicationFactory need to know where to find the code of
// the Function project.
//
// FunctionLocator.FromProject looks for a solution in the parent folders
// then works down till it finds a matching child folder.
// As long as the functionProjectFolder is unique (and you are using a solution)
// this should work.
const string functionProjectFolder = "[Folder containing the function project e.g. Dotnet.Function.Demo]";
var locator = FunctionLocator.FromProject(functionProjectFolder)
// As an alternative you can use FunctionLocator.FromPath and
// specify the full path to the Function project.
// Or, implement IFunctionLocator yourself
// ----------------------------------------------------------------------
// Once we have the Function project code, we can create a factory.
// Tests will be more efficient if this is done once.
// There are different ways to do this depending on the testing framework you are using
// e.g. CollectionDefinition in xUnit (https://xunit.net/docs/shared-context),
// SetUpFixture in NUnit
// (https://docs.nunit.org/articles/nunit/writing-tests/attributes/setupfixture.html),
// AssemblyInitialize/AssemblyCleanup in MSTest etc.
using var factory = new FunctionApplicationFactory(locator)
// Or, FunctionApplicationFactory(locator, additionalArgs)
// to pass additional arguments to the `func start`
// ----------------------------------------------------------------------
// Adjust the start up delay, depending on build time of Function project
factory.StartupDelay = TimeSpan.FromSeconds(5);
// ----------------------------------------------------------------------
// Create the pre-configured client from the factory...
using var client = await factory.CreateClient();
// The client is configured with the correct BaseAddress
// so requests only need a relative directory
const string uri = "/a/relative/api";
var response = await client.GetAsync(uri);
response.StatusCode.Should().Be(HttpStatusCode.OK);
Example: Capturing output from Azure Function Core Tools
The func
tools write output to the console.
It can be useful to capture this output during test runs.
However, some testing frameworks do not capture/output console messages by default.
A TestOutputConsoleAdapter can be used in xUnit to provide this
kind of logging capability.
This can then be configured via injection of the xUnit ITestOutputHelper
and setting of the
output and error streams
public HelloFeatureThatSharesFixture(ITestOutputHelper output)
{
var adapter = new TestOutputConsoleAdapter(output)
Console.SetOut(adapter);
Console.SetError(adapter);
}
To get more detailed ouput the verbose
and debug
flags can be passed.
using var factory = new FunctionApplicationFactory(locator, "--verbose", "--debug")
If you are not using a local.settings.json
file or you are running the tests in a CI pipeline you may need to pass a language
argument to the FunctionApplicationFactory
so the function runtime can determine the project.
using var factory = new FunctionApplicationFactory(locator, "--verbose", "--debug", "--csharp")
If you are using Azure DevOps pipelines for your CI pipelines the FuncToolsInstaller@0 task can be used to install dependencies.
Version History
Version | Major Changes |
---|---|
0.1.6 | Fixed intermittent bug where Stop method would error as the process had already exited |
0.1.5 | Optimizations and performance fixes |
0.1.4 | Fixed bug when using health check and function is not receiving requests |
0.1.3 | Added support for health check on startup |
0.1.2 | Added support for running in CI pipelines as well as locally |
0.1.1 | Expose Start and Stop methods to allow initialization |
0.1.0 | Initial version |
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.