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

使用TestProject API获取测试报告PDF的关联匹配问题求助

问题分析

你的核心问题在于jobID与所属projectID没有建立关联:当前list_jobId只是把所有项目的jobID平铺存储,没有记录每个job属于哪个project。而executionId函数中直接按索引取list_projectId[i]和list_jobId[i],只会把第i个project和第i个job强行配对,这显然不符合实际的从属关系(一个project可能有多个job),导致API调用时project与job不匹配,请求失败。

解决方案

我们需要把每个jobID和它所属的projectID绑定在一起,比如用元组(project_id, job_id)的列表来存储。之后遍历这个绑定列表,就能正确调用API获取executionID,进而下载PDF报告。

修改后的完整代码

import requests
import json
from jsonpath_ng import parse

API_key = 'your_api_key_here'
headers = {'Authorization': API_key}

# 存储(project_id, job_id)配对的列表,替代原来的两个单独列表
job_project_pairs = []
execution_ids = []

def parse_json(response_data, jsonpath_expr):
    """解析JSON数据,返回匹配的结果列表"""
    parsed_expr = parse(jsonpath_expr)
    matches = parsed_expr.find(response_data)
    return [match.value for match in matches]

def get_all_projects():
    """获取所有项目ID"""
    url = 'https://api.testproject.io/v2/projects?_start=0'
    response = requests.get(url, headers=headers)
    response.raise_for_status()  # 捕获请求错误
    return parse_json(response.json(), '$..id')

def get_jobs_for_projects(project_ids):
    """获取每个项目下的job,并与所属projectID绑定"""
    for project_id in project_ids:
        url = f'https://api.testproject.io/v2/projects/{project_id}/jobs?onlyScheduled=false&_start=0'
        response = requests.get(url, headers=headers)
        response.raise_for_status()
        job_ids = parse_json(response.json(), '$..id')
        # 将当前projectID与每个jobID配对存入列表
        for job_id in job_ids:
            job_project_pairs.append((project_id, job_id))

def get_execution_ids():
    """根据绑定的project-job对获取executionID"""
    for project_id, job_id in job_project_pairs:
        url = f'https://api.testproject.io/v2/projects/{project_id}/jobs/{job_id}/reports?_start=0'
        response = requests.get(url, headers=headers)
        response.raise_for_status()
        exec_ids = parse_json(response.json(), '$..id')
        execution_ids.extend(exec_ids)

def download_pdf_reports():
    """下载并保存所有execution对应的PDF报告"""
    for exec_id in execution_ids:
        url = f'https://api.testproject.io/v2/reports/{exec_id}/pdf'
        response = requests.get(url, headers=headers)
        response.raise_for_status()
        # 以executionID为文件名保存PDF
        with open(f'report_{exec_id}.pdf', 'wb') as f:
            f.write(response.content)
        print(f"已保存报告: report_{exec_id}.pdf")

# 执行流程
if __name__ == '__main__':
    project_ids = get_all_projects()
    print("----------所有项目ID: ----------")
    print(project_ids)
    
    get_jobs_for_projects(project_ids)
    print("\n----------所有Project-Job配对: ----------")
    print(job_project_pairs)
    
    get_execution_ids()
    print("\n----------所有Execution ID: ----------")
    print(execution_ids)
    
    # 下载PDF报告
    download_pdf_reports()

关键修改说明

  1. 绑定project与job关系:用job_project_pairs列表存储(project_id, job_id)元组,确保每个job都能找到所属的project。
  2. 优化解析函数:将parsing函数改为parse_json,直接返回解析结果,避免全局变量的副作用,代码更清晰。
  3. 增加错误处理:添加response.raise_for_status(),在API请求失败时抛出异常,方便排查问题。
  4. 补充PDF下载逻辑:新增download_pdf_reports函数,根据executionID调用PDF下载API并保存文件,完成你的最终目标。
  5. 代码结构优化:用函数返回值替代全局变量传递,提升代码的可维护性。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.25 11:54:06