Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save SeanFeldman/f0d4dde66b537896ed388331e00b1d88 to your computer and use it in GitHub Desktop.
Save SeanFeldman/f0d4dde66b537896ed388331e00b1d88 to your computer and use it in GitHub Desktop.
SetUp Fixture for NUnit starting the emulator before tests
namespace AzureTableStorage.Tests
{
using System.Diagnostics;
using System.Linq;
// Start/stop azure storage emulator from code:
// http://stackoverflow.com/questions/7547567/how-to-start-azure-storage-emulator-from-within-a-program
public static class AzureStorageEmulatorManager
{
const string AzureStorageEmulatorPath = @"C:\Program Files (x86)\Microsoft SDKs\Azure\Storage Emulator\AzureStorageEmulator.exe";
const string Win7ProcessName = "WAStorageEmulator";
const string Win8ProcessName = "WASTOR~1";
const string Win10ProcessName = "AzureStorageEmulator";
static readonly ProcessStartInfo startStorageEmulator = new ProcessStartInfo
{
FileName = AzureStorageEmulatorPath,
Arguments = "start",
UseShellExecute = false
};
static readonly ProcessStartInfo stopStorageEmulator = new ProcessStartInfo
{
FileName = AzureStorageEmulatorPath,
Arguments = "stop",
};
static Process GetProcess()
{
return Process.GetProcessesByName(Win10ProcessName).FirstOrDefault()
?? Process.GetProcessesByName(Win8ProcessName).FirstOrDefault()
?? Process.GetProcessesByName(Win7ProcessName).FirstOrDefault();
}
static bool IsProcessStarted()
{
return GetProcess() != null;
}
public static void StartStorageEmulator()
{
if (IsProcessStarted())
{
return;
}
using (var process = Process.Start(startStorageEmulator))
{
process.WaitForExit();
}
}
public static void StopStorageEmulator()
{
using (var process = Process.Start(stopStorageEmulator))
{
process.WaitForExit();
}
}
}
}
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Table;
using NUnit.Framework;
using System;
using System.Linq;
using System.Collections.Generic;
namespace AzureTableStorage.Tests
{
[SetUpFixture]
public class StartStopAzureEmulator
{
private bool _wasUp;
[SetUp]
public void StartAzureBeforeAllTestsIfNotUp()
{
if (!AzureStorageEmulatorManager.IsProcessStarted())
{
AzureStorageEmulatorManager.StartStorageEmulator();
_wasUp = false;
}
else
{
_wasUp = true;
}
}
[TearDown]
public void StopAzureAfterAllTestsIfWasDown()
{
if (!_wasUp)
{
AzureStorageEmulatorManager.StopStorageEmulator();
}
else
{
// Leave as it was before testing...
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment