Hi All! Welcome back to the second part of the Series. In this last article, we’re going to see how we can setup the testing pipeline for our Azure Functions on Azure DevOps.

Last time we saw how we could structure our test project using XUnit Fixtures and how it is possible to launch the Function Host as an external tool to test the boundaries of our services.

This is great on localhost, but now, of course, we want it to be part of our CI/CD pipeline.

Now, I’m assuming you already know how to use Azure DevOps to build your pipelines. But in case you need a refresher, you can find a lot of information here.

So let’s jump into the code, we’ll discuss the details after:

pool:
  name: windows-2019

variables:  
  functionAppName: 'MyLovelyFunction'  
  workingDirectory: '$(System.DefaultWorkingDirectory)/$(functionAppName)'  
  ASPNETCORE_ENVIRONMENT: 'ci'

stages:
- stage: Build
  displayName: Build stage

  jobs:
  - job: Build
    displayName: Build

    steps:            
    - script: | 
        "%ProgramFiles(x86)%\Microsoft SDKs\Azure\Storage Emulator\AzureStorageEmulator.exe" init /server "(localdb)\MsSqlLocalDb"
        "%ProgramFiles(x86)%\Microsoft SDKs\Azure\Storage Emulator\AzureStorageEmulator" start
        cp '$(workingDirectory)\ci.settings.json' '$(workingDirectory)\local.settings.json'
        dotnet test
      workingDirectory: $(System.DefaultWorkingDirectory)
      displayName: Test 

    - task: [email protected]
      displayName: Build Release
      inputs:
        command: 'build'
        projects: |
          $(workingDirectory)/*.csproj
        arguments: --output $(System.DefaultWorkingDirectory)/publish_output --configuration Release

So, the first thing is to specify the type of agent we want to use. Now, since we’re going to rely on the Azure Functions CLI and the Azure Storage Emulator, we need an agent that has them installed. Luckily for us, Microsoft is offering managed agents as well, so we don’t have to worry about this.

In our case, I’ve selected the windows-2019 agent, which comes with a quite comprehensive list of software pre-installed.

A note about the Storage Emulator: it has been discontinued in favor of Azurite. However, I haven’t been able to run my tests using the latter. I’ll probably blog more about this in the next weeks.

The first step in our pipeline contains all we need to setup the agent and start the tests. Let’s analyze the script line by line.

The first two lines will instruct the Storage Emulator to init the local server and start the engine:

%ProgramFiles(x86)%\Microsoft SDKs\Azure\Storage Emulator\AzureStorageEmulator.exe" init /server "(localdb)\MsSqlLocalDb"
"%ProgramFiles(x86)%\Microsoft SDKs\Azure\Storage Emulator\AzureStorageEmulator" start

This third line is actually optional. In case you have custom options for your CI environment, you can define a custom config file and replace local.settings.json, which is mandatory for the Functions Host to run:

cp '$(workingDirectory)\ci.settings.json' '$(workingDirectory)\local.settings.json'

Aaaand at last:

dotnet test

That’s it!