Writing unit test cases that target Azure Dev Fabric

We are currently building an application that writes to Azure blob-storage as part of its business process.  As I was writing test cases for it, my Blob-storage operations were failing because that code is now running under the context of the test case instead of my web role.  So I needed a way to make sure that:

  1. DevFabric is running when my test cases start
  2. The DataConnectionString string is setup to hit DevFabric (I don’t really want to hit the “real” cloud for these test cases)

So I added a method marked with the “AssemblyInitialize” attribute in my Unit test project.  In that method, I check to see if DevFabric has started, if it is not started, i kick it off and wait for it to run.  Once that is going, I need to get the Data Connection String from my test project’s app.config file.  And now, when the code to access Blob storage gets called, it will have all the information it needs in order to successfully complete the operation. 

I have a lot of test cases that hit Azure, so that’s why I used AssemblyInitialize…this code could’ve been placed inside one test case or in a “ClassInitialize” method.

[AssemblyInitialize]
        public static void AzureInitialize(TestContext testContext)
        {
            if( System.Diagnostics.Process.GetProcessesByName("DSService").Length == 0)
            {
                System.Diagnostics.ProcessStartInfo start = new System.Diagnostics.ProcessStartInfo();
                start.Arguments = "/devstore:start";
                start.FileName = @"C:\Program Files\Windows Azure SDK\v1.2\bin\csrun.exe";
 
                var proc = new System.Diagnostics.Process();
                proc.StartInfo = start;
                proc.Start();
                proc.WaitForExit();
            }
 
            CloudStorageAccount.SetConfigurationSettingPublisher((configName, configSetter) => configSetter(ConfigurationManager.AppSettings[configName]));
            var azureAccount = CloudStorageAccount.FromConfigurationSetting("DataConnectionString");
        }

This is what I added in app.config:


    "DataConnectionString" value="UseDevelopmentStorage=true" />
  

About esteban

Esteban is the Founder and Chief Technologist at Nebbia Technology, an ALM consulting and Azure-powered technology company. He is a software developer with a passion for ALM, TFS, Azure, and software development best practices. Esteban is a Microsoft Visual Studio ALM MVP and ALM Ranger, Pluralsight author, and the president of ONETUG (Orlando .NET User Group).


Leave a Reply

Your email address will not be published. Required fields are marked *

Are you human? *