Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to publish python unittest results to azure pipeline

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.

like image 599
Kevin Avatar asked Oct 22 '25 04:10

Kevin


1 Answers

If your unit tests are written using unittest module, dont worry

pytest can run unittest as well !!!

Add below yaml code snippet to your azure pipeline.

  1. Run your unittest with pytest
  2. Generate Junit XML output
  3. Publish the output back to azure pipeline
- script: |
    cd $(Build.Repository.LocalPath)
    python -m pytest $(Build.Repository.LocalPath)/<unit_tests_path>/*.py --junitxml=test-unit.xml
  displayName: 'Run Unit Tests'

- task: PublishTestResults@2
  inputs:
    testResultsFormat: 'JUnit'
    testResultsFiles: 'test-unit.xml'
like image 175
its myview Avatar answered Oct 23 '25 21:10

its myview