Eseguire test automatizzati usando Azurite
Informazioni su come scrivere test automatizzati su endpoint privati per Archiviazione BLOB di Azure usando l'emulatore di archiviazione Azurite.
Eseguire test nel computer locale
Installare la versione più recente di Python
Installare Azure Storage Explorer
Installare ed eseguire Azurite:
Opzione 1: Usare npm per installare, quindi eseguire Azurite localmente
# 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
Opzione 2: Usare Docker per eseguire Azurite
docker run -p 10000:10000 mcr.microsoft.com/azure-storage/azurite azurite-blob --blobHost 0.0.0.0
In Azure Storage Explorer selezionare Collega a un emulatore locale
Specificare un numero di portaNome visualizzato e BLOB per connettere Azurite e usare Azure Storage Explorer per gestire l'archiviazione BLOB locale.
Creare un ambiente Python virtuale
python -m venv .venv
Creare un contenitore e inizializzare le variabili di ambiente. Usare un file PyTestconftest.py per generare test. Ecco un esempio di un file di conftest.py:
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)
Nota
Il valore visualizzato per
AZURE_STORAGE_CONNECTION_STRING
è il valore predefinito per Azurite, non è una chiave privata.Installare le dipendenze elencate in un file direquirements.txt
pip install -r requirements.txt
Eseguire test:
python -m pytest ./tests
Dopo aver eseguito i test, è possibile visualizzare i file nell'archiviazione BLOB di Azurite usando Azure Storage Explorer.
Eseguire test in Azure Pipelines
Dopo aver eseguito test in locale, assicurarsi che i test vengano passati in Azure Pipelines. Usare un'immagine Docker Azurite come agente ospitato in Azure o usare npm per installare Azurite. L'esempio seguente di Azure Pipelines usa npm per installare 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
Dopo aver eseguito i test di Azure Pipelines, verrà visualizzato l'output simile al seguente: