【问题标题】:Running Azure DevOps pipeline exists with ##[error]Bash exited with code '1'运行 Azure DevOps 管道存在 ##[error]Bash exited with code '1'
【发布时间】:2025-12-27 00:40:07
【问题描述】:

简介:

我创建了一个简单的添加函数来更深入地了解 Azure Dev Ops 的工作原理。当我故意让 test_method2() 失败时,管道存在,##[error]Bash 以代码“1”退出。因此,在 Azure Dev Ops 中,缺少测试概述(哪些很好,哪些不好)和代码覆盖率。我希望在测试失败的情况下仍然提供概述测试和代码覆盖率。总而言之,测试失败很好,但应该报告并且不应该停止该过程。

请注意,当我修复测试时,即 'def test_method2(): assert add(15,5) == 20 ' 管道按预期工作。

我的问题是需要什么来确保在测试失败的情况下仍然提供测试和代码覆盖率的概述?

calc_lean.py:

def add(x, y):
    """Add Function"""
    return x + y

test_calc_lean.py:

import pytest
from calc_lean import add

def test_method1():
    assert add(3,5) == 8

def test_method2():
    assert add(15,5) == 21 

azure-pipelines.yml:

trigger:
- master

pool:
  vmImage: 'ubuntu-latest'
strategy:
  matrix:
    Python37:
      python.version: '3.7'

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

- script: |
    python -m pip install --upgrade pip
    pip install -r requirements.txt
  displayName: 'Install dependencies'

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

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

- task: PublishCodeCoverageResults@1
  displayName: 'Code Coverage'
  inputs:
    codeCoverageTool: Cobertura
    summaryFileLocation: '$(System.DefaultWorkingDirectory)/**/coverage.xml'

【问题讨论】:

  • 如果您找到了解决方案,您可以将其添加为回复。很高兴知道您的问题已经解决。
  • 您好 EvR2020,您可以将您的解决方案作为新回复和mark it as an answer 移至下方,这也将对社区中的其他人有所帮助。

标签: python-3.x azure-devops azure-pipelines


【解决方案1】:

解决方案:问题已在其他帖子中讨论过。其中Failed Cypress test exit in 'bash exited with code 1' in Azure DevOps

添加'||真的'解决了它。也就是说,

pytest --doctest-modules --junitxml=junit/test-results.xml --cov=. --cov-report=xml  || true

抑制错误

【讨论】: