Monday, December 26, 2011

Unit Test Initialization and Cleanup for Windows Azure Storage Emulator

My unit tests add and delete entities from local Windows Azure dev storage,  at their most basic. If the Azure Storage emulator isn’t started, the tests don’t fail quickly – they just sit there acting like the test framework is hung. In order to ensure that the tests proceed, I needed to make sure the emulator was started before the tests ran.

 

I started with code found in this thread on StackOverflow. I needed to make sure the Storage Emulator is started before the test classes are called and that the emulator is shut down when the tests are done. I wrote this class and added it as a separate file to my test assembly.

 

More information about CSRun.exe and the Process class are available in MSDN.

 

// -----------------------------------------------------------------------
// <copyright file="AssemblySpecific.cs" company="BerryIntl">
// Berry International 2011
// </copyright>
// -----------------------------------------------------------------------

namespace Wp7AzureMgmt.Dashboard.Test
{
    using System;
    using System.Text;
    using System.Collections.Generic;
    using System.Linq;
    using System.Diagnostics;
    using System.IO;
    using Microsoft.VisualStudio.TestTools.UnitTesting;

    /// <summary>
    /// Class containing assembly specific test initialization and cleanup
    /// </summary>
    [TestClass]
    public class AssemblySpecific
    {
        /// <summary>
        /// Location of csrun.exe - may be different base on install point and azure sdk version
        /// </summary>
        private const string AzureSDKBin = @"C:\Program Files\Windows Azure Emulator\emulator";

        /// <summary>
        /// Code to run before ClassInitialize or TestInitialize
        /// </summary>
        /// <param name="context">TestContext context</param>
        [AssemblyInitialize]
        public static void MyAssemblyInitialize(TestContext context)
        {
            List<Process> processStatus = Process.GetProcessesByName("DSService.exe").ToList();

            if (( processStatus == null ) || ( processStatus.Count == 0 ))
            {
                ProcessStartInfo processStartInfo = new ProcessStartInfo()
                {
                    FileName = Path.Combine(AzureSDKBin, "csrun.exe"),
                    Arguments = "/devstore",
                };
                using (Process process = Process.Start(processStartInfo))
                {
                    process.WaitForExit();
                }
            }
        }

        /// <summary>
        /// Code to run before ClassCleanup or TestCleanup
        /// </summary>
        [AssemblyCleanup]
        public static void MyAssemblyInitialize()
        {
            List<Process> processStatus = Process.GetProcessesByName("DSService.exe").ToList();

            if ((processStatus != null) || (processStatus.Count > 0))
            {
                ProcessStartInfo processStartInfo = new ProcessStartInfo()
                {
                    FileName = Path.Combine(AzureSDKBin, "csrun.exe"),
                    Arguments = "/devstore:shutdown",
                };
                using (Process process = Process.Start(processStartInfo))
                {
                    process.WaitForExit();
                }
            }
        }
    }
}

1 comment: