Run automated tests by using Azurite

Learn how to write automated tests against private endpoints for Azure Blob Storage by using the Azurite storage emulator.

Run tests on your local machine

  1. Install the latest version of Python

  2. Install Azure Storage Explorer

  3. Install and run Azurite:

    Option 1: Use npm to install, then run Azurite locally

    # Install Azurite
    npm install -g azurite
    
    # Create an Azurite directory
    mkdir c:\azurite
    
    # Launch Azurite locally
    azurite --silent --location c:\azurite --debug c:\azurite\debug.log
    

    Option 2: Use Docker to run Azurite

    docker run -p 10000:10000 mcr.microsoft.com/azure-storage/azurite azurite-blob --blobHost 0.0.0.0
    
  4. In Azure Storage Explorer, select Attach to a local emulator

    Screenshot of Azure Storage Explorer connecting to Azure Storage source.

  5. Provide a Display name and Blobs port number to connect Azurite and use Azure Storage Explorer to manage local blob storage.

    Screenshot of Azure Storage Explorer attaching to a local emulator.

  6. Create a virtual Python environment

    python -m venv .venv
    
  7. Create a container and initialize environment variables. Use a PyTest conftest.py file to generate tests. Here is an example of a conftest.py file:

    from azure.storage.blob import BlobServiceClient
    import os
    
    def pytest_generate_tests(metafunc):
       os.environ['AZURE_STORAGE_CONNECTION_STRING'] = 'DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;BlobEndpoint=http://127.0.0.1:10000/devstoreaccount1;'
       os.environ['STORAGE_CONTAINER'] = 'test-container'
    
       # Create a container for Azurite for the first run
       blob_service_client = BlobServiceClient.from_connection_string(os.environ.get("AZURE_STORAGE_CONNECTION_STRING"))
       try:
          blob_service_client.create_container(os.environ.get("STORAGE_CONTAINER"))
       except Exception as e:
          print(e)
    

    Note

    The value shown for AZURE_STORAGE_CONNECTION_STRING is the default value for Azurite, it's not a private key.

  8. Install dependencies listed in a requirements.txt file

    pip install -r requirements.txt
    
  9. Run tests:

    python -m pytest ./tests
    

After running tests, you can see the files in Azurite blob storage by using Azure Storage Explorer.

Screenshot of Azure Storage Explorer showing files generated by the tests.

Run tests on Azure Pipelines

After running tests locally, make sure the tests pass on Azure Pipelines. Use a Docker Azurite image as a hosted agent on Azure, or use npm to install Azurite. The following Azure Pipelines example uses npm to install Azurite.

trigger:
- master

steps:
- task: UsePythonVersion@0
  displayName: 'Use Python 3.7'
  inputs:
    versionSpec: 3.7

- bash: |
    pip install -r requirements_tests.txt
  displayName: 'Setup requirements for tests'

- bash: |
    sudo npm install -g azurite
    sudo mkdir azurite
    sudo azurite --silent --location azurite --debug azurite\debug.log &
  displayName: 'Install and Run Azurite'

- bash: |
    python -m pytest --junit-xml=unit_tests_report.xml --cov=tests --cov-report=html --cov-report=xml ./tests
  displayName: 'Run Tests'

- task: PublishCodeCoverageResults@1
  inputs:
    codeCoverageTool: Cobertura
    summaryFileLocation: '$(System.DefaultWorkingDirectory)/**/coverage.xml'
    reportDirectory: '$(System.DefaultWorkingDirectory)/**/htmlcov'

- task: PublishTestResults@2
  inputs:
    testResultsFormat: 'JUnit'
    testResultsFiles: '**/*_tests_report.xml'
    failTaskOnFailedTests: true

After running the Azure Pipelines tests, you should see output similar to this:

Screenshot of Azure Pipelines test results.

Next steps