Geeks With Blogs
Rohit Gupta Engaging talk on Microsoft Technologies ....My Resume

While developing applications for Windows Azure, we are all aware that we need to create Web and Worker Roles and use the Azure Compute Emulator (previously known as the Development Fabric) for running and testing applications in the local IIS server.

However to run unit tests against libraries that are being consumed from your web/worker roles we need to perform some preliminary steps before we can start using the CloudStorageAccount class within our Unit Tests.

The First step is to start the Storage Service if it is not already started which can be done in a method decorated with the AssemblyInitialze attribute.
The Second step is to call the CloudStorageAccount.SetConfigurationSettingPublisher method from a method decorated with the ClassInitialize attribute, so that we can then successfully use the CloudStorageAccount.FromConfigurationSetting("DataConnectionString") call from within any of the TestMethods.

The code to start the Storage Service can be something like the following:

   1: [AssemblyInitialize]
   2: public static void AssemblyInitialize(TestContext context)
   3: {
   4:     var count = Process.GetProcessesByName("DSService").Length;
   5:     if (count == 0)
   6:     {
   7:         var start = new ProcessStartInfo
   8:         {
   9:             Arguments = "/devstore:start",
  10:             FileName = @"C:\Program Files\Windows Azure SDK\v1.4\bin\csrun.exe"
  11:         };
  13:         var proc = new Process { StartInfo = start };
  14:         proc.Start();
  15:         proc.WaitForExit();
  16:     }
  17: }

Before we can start using the _storageAccount from within our unit tests we need the following code to be configured and setup:

First in the app.config file in the AppSettings section we need to add the following entry:

   1: <configuration>
   2:   <appSettings>
   3:     <add key="DataConnectionString" value="UseDevelopmentStorage=true" />
   4:   </appSettings>
   5: </configuration>

Then in the ClassInitialize static method we need the following code:

   1: [ClassInitialize()]
   2: public static void Init(TestContext testContext)
   3: {
   4:     CloudStorageAccount.SetConfigurationSettingPublisher((configName, configSetter) =>
   5:     {
   6:         var connectionString = ConfigurationManager.AppSettings[configName];
   7:         configSetter(connectionString);
   8:     });
   9: }

Finally in the TestMethod() we can use the _storageAccount and work directly against the Azure table/blob/queue storage as shown below:

   1: [TestMethod]
   2: public void FileExists(string pathWithFileName)
   3: {
   4:     pathWithFileName = pathWithFileName.Replace("\\", "/");
   5:     var storageAccount = CloudStorageAccount.FromConfigurationSetting("DataConnectionString");
   6:     _blobClient = storageAccount.CreateCloudBlobClient();
   7:     _blobClient.Timeout = new TimeSpan(1, 0, 0);
   8:     _blobClient.ParallelOperationThreadCount = 2;
  10:     // Get the container
  11:     _blobContainer = _blobClient.GetContainerReference(_blobContainerName);
  12:     //Get the file from the blob container
  13:     var blob = _blobContainer.GetBlobReference(pathWithFileName);
  14:     Assert.Istrue(blob.Exists());
  15: }
Posted on Thursday, August 18, 2011 7:14 AM | Back to top

Comments on this post: Running Unit Tests against Azure Table and Blob Storage

# re: Running Unit Tests against Azure Table and Blob Storage
Requesting Gravatar...
Thanks for your sharing. I think the approach you mentioned here is to start the storage emulator before the test started, which is OK. But I think the better way is to mock the data context. Then in the test code I can define the data I need and test, regardless of the real storage.
Left by Shaun on Aug 18, 2011 8:49 PM

# re: Running Unit Tests against Azure Table and Blob Storage
Requesting Gravatar...
Shaun, Using a mock for storage would mean not testing against either the azure development storage or the Windows Azure Storage service. If we did not want to test against azure and simply use a mock then i would certainly taken that route. If you know of a better implementation please don't mind sharing it !!!
Left by Rohit Gupta on Sep 04, 2011 10:11 PM

Your comment:
 (will show your gravatar)

Copyright © Rohit Gupta | Powered by: