Run Azure DevOps Unit Tests with the Azure Storage Emulator on Hosted build agents
Solved: How to run Unit Tests with Azure Storage Emulator on a Hosted Build Agent in Azure DevOps.
Working with Azure is great in many ways. However when it comes to running iterative tests over and over again, which perhaps in themselves does a lot of storage operations to Azure Storage (Tables, Queues, Blobs, ...), it's nice to be able to use the Azure Storage Emulator.
Using the emulator for local dev work is common practice, but in order for it to run on your Azure DevOps pipe (I'm using Hosted agents), there's some tricks you can do.
- Ensure you're using a Windows-based Hosted agent
- Run a command-line tool to set up the local database
- Run a command-line tool to launch the Storage Emulator in your hosted build agent
Here's what you can do to accomplish this:
In my build definition, this is what I'm doing - don't worry, it's easy as 1-2-3-4.
- Select your pipeline (this is where you can change the Agent Pool & Spec).
- Ensure that you're running on a Windows-based agent. This is required for the emulator to work in this capacity. I'm successfully using
vs2017-win2016for my current setup.
- Add a Command Line Task (more details on the content of that task further down)
- Add your dotnet test task as usual
In the above steps, the only real difference to what my pipeline looked like previously is that there's a script being executed just before I start to run my tests which use the Storage Emulator.
The (3) - Command Line script content - looks like this:
sqllocaldb create MSSQLLocalDB sqllocaldb start MSSQLLocalDB "C:\Program Files (x86)\Microsoft SDKs\Azure\Storage Emulator\AzureStorageEmulator.exe" start
With those modifications, the command line have successfully created and started the
MSSQLLocdalDB instance in the MicrosoftSQL Server Express LocalDB - this is what the Storage Emulator relies on to execute and store data.
That's it - all tests are coming out green, and are successfully running the Storage Emulator on the hosted Azure DevOps build agents. Wohoo!