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

如何在pytest执行完成后获取Report Portal的报告链接?

获取Report Portal测试报告链接的办法

有几种实用的方式可以拿到报告链接,方便你推送到Slack:

  • 用Report Portal API直接查询
    测试跑完后,通过官方API获取对应测试运行的报告链接:

    1. 先在测试执行过程中记录测试运行的ID——可以借助pytest-reportportal的钩子函数,比如pytest_runtest_logfinish,或者直接从插件的服务实例里取launch_id
    2. 调用GET /api/v1/{项目名}/run/{运行ID}接口,返回的JSON里有个web_url字段,就是报告的直接访问链接
    3. 拿到链接后写个简单脚本就能推去Slack

    给个Python示例片段:

    import requests
    
    def get_rp_report_link(project_name, run_id, rp_api_url, rp_api_token):
        headers = {'Authorization': f'Bearer {rp_api_token}'}
        api_url = f"{rp_api_url}/api/v1/{project_name}/run/{run_id}"
        resp = requests.get(api_url, headers=headers)
        resp.raise_for_status()
        return resp.json()['web_url']
    
  • 修改pytest配置,让链接输出到命令行
    可以在项目的conftest.py里加个pytest钩子,测试结束时自动打印报告链接:

    def pytest_runtest_terminal_summary(terminalreporter, exitstatus, config):
        rp_service = getattr(config, '_rp_service', None)
        if rp_service and hasattr(rp_service, 'launch_id'):
            launch_id = rp_service.launch_id
            # 替换成你的Report Portal地址
            report_link = f"https://你的RP地址/ui/#{config.getoption('rp_project')}/launches/all/{launch_id}"
            terminalreporter.write_line(f"\nReport Portal 报告链接: {report_link}")
    

    之后执行pytest --reportportal,命令行最后就会输出链接,你用脚本捕获这个输出就行。

  • 用Report Portal的回调功能
    在rp.yml配置文件里设置post_launch_callback,指定一个自定义脚本路径。测试运行完成后,RP会自动触发这个脚本,你可以在脚本里通过插件传递的参数拿到运行ID,构造报告链接后直接推送到Slack。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 21:03:21