How to publish python unittest results to azure pipeline

Viewed 531

I am trying to build a pipeline azure DevOps. I build a basic Flask website and wrote a Unittest script for it. It basically all works perfect. When i commit to azure repos the pipeline will do his thing and the test will run. The thing i want is to see the test results, i see all these tutorials for Pytest but not for Unittest.

trigger:
- Development

jobs:
- job: 'Test'
  pool:
    vmImage: 'ubuntu-latest' # other options: 'macOS-latest', 'windows-latest'
  strategy:
    matrix:
      Python37:
        python.version: '3.7'

  steps:
  - task: UsePythonVersion@0
    inputs:
      versionSpec: '$(python.version)'

  - script: |
      python -m pip install --upgrade pip
      python -m pip install -e .
    displayName: 'Install dependencies'
  
  - script: |
      python -m unittest discover -p "*.py" > results.txt
    displayName: unittesting

This is my Yaml file that runs the pipeline.

This is how my results look when running the pipeline. Pipeline results

Is there a way to publish these results with Unittest and have them in azure pipeline.

1 Answers

You'll first need to make your test script generate results in a format that DevOps can understand, i.e. JUnit XML. There's an example in MS docs (which includes coverage as well):

- script: |
    pip install pytest pytest-azurepipelines
    pip install pytest-cov
    pytest --doctest-modules --junitxml=junit/test-results.xml --cov=. --cov-report=xml
  displayName: 'pytest'

Alternatively, using unittest-xml-reporting should give you results in JUnit XML format as well.

Once you have that, then you can use Publish Test Results task to upload results and make them visible in DevOps UI, i.e.:

- task: PublishTestResults@2
  inputs:
    testResultsFormat: 'JUnit'
    testResultsFiles: '**/test-*.xml' 
Related