Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@paulbatum
Created December 22, 2017 23:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulbatum/d1da9b920421ec8906f2c7bfb81c52e1 to your computer and use it in GitHub Desktop.
Save paulbatum/d1da9b920421ec8906f2c7bfb81c52e1 to your computer and use it in GitHub Desktop.
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