Jalankan pengujian otomatis dengan menggunakan Azurite

Pelajari cara menulis pengujian otomatis terhadap endpoint pribadi untuk Azure Blob Storage dengan menggunakan emulator penyimpanan Azurite.

Jalankan pengujian pada komputer lokal Anda

  1. Pasang versi Python terbaru

  2. Pasang Penjelajah Microsoft Azure Storage

  3. Pasang dan jalankan Azurite:

    Opsi 1: Gunakan npm untuk memasang, lalu jalankan Azurite secara lokal

    # 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
    

    Opsi 2: Gunakan Docker untuk menjalankan Azurite

    docker run -p 10000:10000 mcr.microsoft.com/azure-storage/azurite azurite-blob --blobHost 0.0.0.0
    
  4. Di Penjelajah Microsoft Azure Storage, pilih Lampirkan ke emulator lokal

    Cuplikan layar Azure Storage Explorer yang tersambung ke sumber Azure Storage.

  5. Berikan nomor Nama tampilan dan port Blob untuk menyambungkan Azurite dan menggunakan Penjelajah Microsoft Azure Storage untuk mengelola penyimpanan blob lokal.

    Cuplikan layar Azure Storage Explorer yang dilampirkan ke emulator lokal.

  6. Membuat lingkungan Python virtual

    python -m venv .venv
    
  7. Membuat kontainer dan menginisialisasi variabel lingkungan. Gunakan file PyTestconftest.py untuk menghasilkan pengujian. Berikut adalah contoh file conftest.py:

    from azure.storage.blob import BlobServiceClient
    import os
    
    def pytest_generate_tests(metafunc):
       os.environ['AZURE_STORAGE_CONNECTION_STRING'] = 'DefaultEndpointsProtocol=http;AccountName=[name];AccountKey=[key];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)
    

    Catatan

    Nilai yang ditampilkan untuk AZURE_STORAGE_CONNECTION_STRING adalah nilai default untuk Azurite, itu bukan kunci pribadi.

  8. Memasang dependensi yang tercantum dalam file requirements.txt

    pip install -r requirements.txt
    
  9. Jalankan pengujian:

    python -m pytest ./tests
    

Setelah menjalankan pengujian, Anda dapat melihat file di penyimpanan blob Azurite dengan menggunakan Penjelajah Microsoft Azure Storage.

Cuplikan layar Azure Storage Explorer memperlihatkan file yang dihasilkan oleh pengujian.

Menjalankan pengujian di Azure Pipelines

Setelah menjalankan pengujian secara lokal, pastikan pengujian lulus pada Azure Pipelines. Gunakan gambar Docker Azurite sebagai agen yang di-hosting di Azure, atau gunakan npm untuk memasang Azurite. Contoh Azure Pipelines berikut menggunakan npm untuk memasang 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

Setelah menjalankan pengujian Azure Pipelines, Anda akan melihat output yang mirip dengan ini:

Cuplikan layar hasil pengujian Azure Pipelines.

Langkah berikutnya