You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure DevOps流水线调用Python脚本内pytest函数报错求助

问题:Azure DevOps流水线调用pytest函数报错Syntax error near unexpected token 'run_pytest(Pytest2)'

错误原因

流水线中的script任务默认执行的是Shell(Linux/macOS)或CMD/PowerShell(Windows)脚本,而非Python代码。你在Shell环境里写Python的函数调用语法(run_pytest('Pytest2')),Shell解释器无法识别,因此抛出语法错误。

另外,PythonScript@0任务里定义的run_pytest函数仅在该任务的Python进程中有效,不同流水线步骤是独立的进程,后续步骤无法直接调用之前步骤里的Python函数。

解决方案

方案1:合并逻辑到同一个PythonScript任务

将函数定义和调用逻辑放在同一个PythonScript@0任务中,确保所有代码在Python环境执行:

parameters:
- name: jobName
  type: string

jobs:
- job: ${{ parameters.jobName }}
  variables:
    my_id: "your_id_here"
    my_token: "your_token_here"
  steps:
  - ${{ if or(eq(parameters.jobName, 'Pytest1'), eq(parameters.jobName, 'Pytest2')) }}:
    - task: PythonScript@0
      inputs:
        scriptSource: 'inline'
        script: |
          import subprocess
          import os
          import sys

          def run_pytest(test_case):
              test_case_name = ''
              if test_case == 'Pytest2':
                  test_case_name = 'not Pytest2'
              else:
                  test_case_name = 'Pytest2'
              result = subprocess.run(['python', '-m', 'pytest', '-v', 'tests', '-k', test_case_name])
              return result.returncode

          exit_code = run_pytest('${{ parameters.jobName }}')
          if exit_code != 0:
              print('Tests failed')
              sys.exit(1)
      displayName: 'Run Pytest'

  - ${{ if not(or(eq(parameters.jobName, 'Pytest1'), eq(parameters.jobName, 'Pytest2'))) }}:
    - script: |
        echo "Invalid jobName: ${{ parameters.jobName }}"
        exit 1
      displayName: 'Invalid Job Name'

方案2:拆分步骤时确保调用代码在Python环境执行

如果需要拆分步骤,可在后续步骤中使用PythonScript@0任务重新定义函数并调用(或通过临时文件共享函数逻辑),示例如下:

parameters:
- name: jobName
  type: string

jobs:
- job: ${{ parameters.jobName }}
  variables:
    my_id: "your_id_here"
    my_token: "your_token_here"
  steps:
  - ${{ if or(eq(parameters.jobName, 'Pytest1'), eq(parameters.jobName, 'Pytest2')) }}:
    # 步骤1:将函数逻辑写入临时文件
    - task: PythonScript@0
      inputs:
        scriptSource: 'inline'
        script: |
          with open('pytest_runner.py', 'w') as f:
              f.write('''
import subprocess
import sys

def run_pytest(test_case):
    test_case_name = ''
    if test_case == 'Pytest2':
        test_case_name = 'not Pytest2'
    else:
        test_case_name = 'Pytest2'
    result = subprocess.run(['python', '-m', 'pytest', '-v', 'tests', '-k', test_case_name])
    return result.returncode
''')
      displayName: 'Prepare Pytest Runner'

    # 步骤2:调用临时文件中的函数
    - task: PythonScript@0
      inputs:
        scriptSource: 'inline'
        script: |
          import sys
          sys.path.insert(0, '.')
          from pytest_runner import run_pytest

          exit_code = run_pytest('${{ parameters.jobName }}')
          if exit_code != 0:
              print('Tests failed')
              sys.exit(1)
      displayName: 'Run Pytest'

  - ${{ if not(or(eq(parameters.jobName, 'Pytest1'), eq(parameters.jobName, 'Pytest2'))) }}:
    - script: |
        echo "Invalid jobName: ${{ parameters.jobName }}"
        exit 1
      displayName: 'Invalid Job Name'

内容的提问来源于stack exchange,提问作者Hari

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 10:51:33