Skip to content

Instantly share code, notes, and snippets.

@johndowns
Created November 7, 2018 11:18
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 johndowns/6701ac83e18734ac4566bfdc43c41d35 to your computer and use it in GitHub Desktop.
Save johndowns/6701ac83e18734ac4566bfdc43c41d35 to your computer and use it in GitHub Desktop.
public class FunctionTestFixture : IDisposable
{
private readonly Process _funcHostProcess;
public int Port { get; } = 7001;
public readonly HttpClient Client = new HttpClient();
public FunctionTestFixture()
{
var dotnetExePath = Environment.ExpandEnvironmentVariables(ConfigurationHelper.Settings.DotnetExecutablePath);
var functionHostPath = Environment.ExpandEnvironmentVariables(ConfigurationHelper.Settings.FunctionHostPath);
var functionAppFolder = Path.GetRelativePath(Directory.GetCurrentDirectory(), ConfigurationHelper.Settings.FunctionApplicationPath);
_funcHostProcess = new Process
{
StartInfo =
{
FileName = dotnetExePath,
Arguments = $"\"{functionHostPath}\" start -p {Port}",
WorkingDirectory = functionAppFolder
}
};
var success = _funcHostProcess.Start();
if (!success)
{
throw new InvalidOperationException("Could not start Azure Functions host.");
}
Client.BaseAddress = new Uri($"http://localhost:{Port}");
}
public virtual void Dispose()
{
if (!_funcHostProcess.HasExited)
{
_funcHostProcess.Kill();
}
_funcHostProcess.Dispose();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment