Forked from bragma/AzureStorageEmulatorManager.cs
Last active
February 3, 2020 13:56
-
-
Save SeanFeldman/f0d4dde66b537896ed388331e00b1d88 to your computer and use it in GitHub Desktop.
SetUp Fixture for NUnit starting the emulator before tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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