Skip to content

Instantly share code, notes, and snippets.

@paulbatum

paulbatum/Test1.cs

Created Dec 22, 2017
Embed
What would you like to do?
Example test project for testing the default http trigger in azure functions
using System;
using System.Diagnostics;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using FunctionApp22_UnitTesting;
using Microsoft.Azure.WebJobs.Host;
using Xunit;
namespace FunctionAppUnitTesting
{
public class Tests1
{
[Fact]
public async Task TestSomething()
{
var configuration = new HttpConfiguration();
var request = new HttpRequestMessage(HttpMethod.Get, new Uri("http://myserver.com/api/run?name=paul"));
request.Content = new StringContent(string.Empty);
request.Properties[System.Web.Http.Hosting.HttpPropertyKeys.HttpConfigurationKey] = configuration;
var traceWriter = new ConsoleTraceWriter(TraceLevel.Verbose);
var result = await Function1.Run(request, traceWriter);
Assert.Equal(HttpStatusCode.OK, result.StatusCode);
}
}
public class ConsoleTraceWriter : TraceWriter
{
public ConsoleTraceWriter(TraceLevel traceLevel) : base(traceLevel)
{
}
public override void Trace(TraceEvent traceEvent)
{
if (Level < traceEvent.Level)
{
return;
}
Console.WriteLine(traceEvent.Message);
}
}
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net461</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\FunctionApp22-UnitTesting\FunctionApp22-UnitTesting.csproj" />
</ItemGroup>
<ItemGroup>
<Reference Include="Microsoft.CSharp" />
</ItemGroup>
</Project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.