You have been working on a project that utilizes the Azure Storage Services (Blob, Table, Queue) and to test it, you have written your unit/integration tests while connected to a storage account in Azure.
But you figure that there must be a free alternative to this.
And you are correct, because you are doing local development at this point, why would you need to spin up a paid storage account for this!

Enter Azurite an open source Azure Storage API compatible emulator that can also run in a docker container.
Perfect that is what we need 👌.

Let’s jump into the code

We created a simple library that wraps some common operations against the new Azure.Storage.Blobs v12 SDK.
We have also pulled the latest Azurite image from DockerHub.

AzureBlobStorageProvider wraps common operations against the Azure.Storage.Blobs SDK.

Along with the library we have created also a xUnit project which contains 4 classes:

  • AzureBlob — Is an abstract class which provides helper functionalities.
  • AzureBlobStorageFixture —Is a fixture class that provides data construction & destruction for all tests (see more about shared test content in xUnit).
  • AzureBlobStorageProviderTests — Contains the unit tests we run against Azurite.
  • AzureStorageEmulator — Contains code to start & stop the Azurite docker container.

The AzureBlobStorageFixture class uses the AzureStorageEmulator to start/stop the Azurite docker container, to initialize the BlobServiceClient, and does data clean up at the end of the tests.

AzureBlobStorageFixture used in local environment.

Let’s briefly discuss the parts of the code:

  • AZURE_STORAGE_CONN_STRING — Is an environmental variable that represents the connection string to the azure storage account.
    We could have easily put the connection string in there, but we need the code to be flexible so it can run in the build pipeline in Azure DevOps (see part 2/2).

  • InitializeEmulator — Determines if we are currently in a local or DevOps environment via the TF_BUILD variable (This is present as a system variable in Azure DevOps Services).

    If we are in a local environment we set the connection string to “UseDevelopmentStorage=true” which is a shorthand way to connect the BlobServiceClient to the Azurite Storage Emulator. At the end we call the DockerProcess.StartEmulator method which starts the container.

AzureStorageEmulator class provides a simple way to start & stop the Azurite docker container.

  • Dispose — Deletes all containers within the storage emulator and stops the service when the tests have finished.
    This ensures we always begin with a clean storage for an other test run.

We can see in the images below that Azurite is running in a docker container and that all our tests are passing (NCrunch is used as the testing tool):

Azurite running in a docker container.

All tests are passing (shown via NCrunch).

Summary

This article was aimed to explain how to create a project with running tests while using Azurite Storage Emulator, in a local environment.

To see how we can create a debuggable NuGet package from our library, publish it to a feed in Azure DevOps, and run our tests in the build pipeline please visit part 2/2 of this article.

If you found this article helpful please give it a share in your favorite forums 😉.
The solution project is available on GitHub.