Как реализовать параллельное pytesting с покрытием кода в Azure CI - PullRequest
0 голосов
/ 09 июля 2020

Мне удалось реализовать параллельное pytesting в Azure CI. См. Это репо для справки. Но все же покрытие кода работает не так, как ожидалось. Он работает индивидуально, но не объединяет все тесты.

Здесь - это конфигурационный файл Azure, который я использую:

# Python test sample
# Sample that demonstrates how to leverage the parallel jobs capability of Azure Pipelines to run python tests in parallel.
# Parallelizing tests helps in reducing the time spent in testing and can speed up the pipelines significantly.
variables:
  disable.coverage.autogenerate: 'true'

jobs:

- job: 'ParallelTesting'
  pool:
    vmImage: 'windows-latest'
  strategy:
    parallel: 3

  steps:
  - task: UsePythonVersion@0
    inputs:
      versionSpec: '3.7'
      addToPath: true
      architecture: 'x64'

  - script: |
      python -m pip install --upgrade pip setuptools wheel
    displayName: 'Install tools'

  - script: 'pip install -r $(System.DefaultWorkingDirectory)/requirements.txt' 
    displayName: 'Install dependencies'

  - powershell: ./DistributeTests.ps1 
    displayName: 'PowerShell Script to distribute tests'

  - script: |
      pip install pytest-azurepipelines pytest-cov
    displayName: 'Install Pytest dependencies'

  - script: |
      echo $(pytestfiles)
      pytest $(pytestfiles) --junitxml=junit/$(pytestfiles)-results.xml --cov=. --cov-report=xml --cov-report=html
    displayName: 'Pytest'
    continueOnError: true

  - task: PublishTestResults@2
    displayName: 'Publish Test Results **/*-results.xml'
    inputs:
      testResultsFiles: '**/*-results.xml'
      testRunTitle: $(python.version)
    condition: succeededOrFailed()

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

И сценарий PowerShell для распространения тестов:

<#  
.SYNOPSIS  
    Distribute the tests in VSTS pipeline across multiple agents 
.DESCRIPTION  
    This script slices tests files across multiple agents for faster execution.
    We search for specific type of file structure (in this example test*), and slice them according to agent number
    If we encounter multiple files [file1..file10] and if we have 2 agents, agent1 executes tests odd number of files while agent2 executes even number of files
    For detalied slicing info: https://docs.microsoft.com/en-us/vsts/pipelines/test/parallel-testing-any-test-runner
    We use JUnit style test results to publish the test reports.
#>

$tests = Get-ChildItem .\ -Filter "test*" # search for test files with specific pattern.
$totalAgents = [int]$Env:SYSTEM_TOTALJOBSINPHASE # standard VSTS variables available using parallel execution; total number of parallel jobs running
$agentNumber = [int]$Env:SYSTEM_JOBPOSITIONINPHASE  # current job position
$testCount = $tests.Count

# below conditions are used if parallel pipeline is not used. i.e. pipeline is running with single agent (no parallel configuration)
if ($totalAgents -eq 0) {
    $totalAgents = 1
}
if (!$agentNumber -or $agentNumber -eq 0) {
    $agentNumber = 1
}

Write-Host "Total agents: $totalAgents"
Write-Host "Agent number: $agentNumber"
Write-Host "Total tests: $testCount"

$testsToRun= @()

# slice test files to make sure each agent gets unique test file to execute
For ($i=$agentNumber; $i -le $testCount;) {
    $file = $tests[$i-1]
    $testsToRun = $testsToRun + $file
    Write-Host "Added $file"
    $i = $i + $totalAgents 
 }

# join all test files seperated by space. pytest runs multiple test files in following format pytest test1.py test2.py test3.py
$testFiles = $testsToRun -Join " "
Write-Host "Test files $testFiles"
# write these files into variable so that we can run them using pytest in subsequent task. 
Write-Host "##vso[task.setvariable variable=pytestfiles;]$testFiles" 

Если вы посмотрите на конвейер , можно увидеть, что тесты pytest проходят нормально. Соответственно, он также создает отчет о покрытии кода. Я считаю, что проблема заключается в объединении отчетов о покрытии кода в один.

Azure Pipeline

Now if looking for the сводка по последнему запуску , можно заметить, что на каждый запуск приходится только одно вложение. Скорее всего, это последнее выполненное задание. В данном случае test_ chrome .py-results. xml.

Планы тестов

...