当前位置: 首页 > news >正文

Python subprocess.run 使用注意事项,避免出现list index out of range

 在执行iOS UI 自动化专项测试的时候,在运行第一遍的时候遇到了这样的错误:

2024-12-04 20:22:27 ERROR conftest pytest_runtest_makereport 106  Test test_open_stream.py::TestOpenStream::test_xxx_open_stream[iPhoneX-xxx-1-250] failed with error: list index out of range

对应客户端上的状态是:

得出判断应该是:开流失败,对应的状态没有回来,程序中没有获取到。那么当这种情况发生时如何解决上面的bug呢?看下代码:

def grep_from_sandbox_log(grep_condition, iphone_model):# iOS沙盒目录log_date = datetime.now().strftime("%Y%m%d")mount_path = f"/Users/testmanzhang/ios_sandbox/{iphone_model}/Documents/Logs/"log_file_path = f"{mount_path}glazero_app_ios_{log_date}.log"result = Noneget_state_cmd = f"grep {grep_condition} {log_file_path}"if os.path.isfile(log_file_path):result = subprocess.run(get_state_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)if result.stderr:logger.error(f"grep 错误: \n {result.stderr}")else:logger.error(f"日志文件不存在: {log_file_path}")return result
def get_dev_play_state_result(click_time, iphone_model, sn):grep_condition = f"'previewSuccess  deviceId = {sn}'"get_result = grep_from_sandbox_log(grep_condition, iphone_model)# 分析获取的结果if get_result:lines = result_get_success.stdout.splitlines()last_line = lines[-1]logger.info(f"最后一行唤醒成功的结果: {last_line}\n")

问题出现在grep_from_sandbox_log方法中的

result = subprocess.run(get_state_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)

如果结果是空的时候,返回的result是:

CompletedProcess(args='truncate -s 0 /Users/testmanzhang/ios_sandbox/iPhoneX/Documents/Logs/glazero_app_ios_20241204.log', returncode=0, stdout='', stderr='')

这时候在get_dev_play_state_result方法中,对获取到的返回结果进行判断也是True,这样问题就发生了,lines其实是一个空列表,再去使用[-1]去获取数据就报错了。

    if get_result:lines = result_get_success.stdout.splitlines()last_line = lines[-1]

解决这个问题要在grep_from_sandbox_log方法中对result进行处理,即对result.stdout进行处理,把处理完成后的结果返回给其他方法,例如,get_dev_play_state_result,不能直接返回result。

在grep_from_sandbox_log方法中调整一下:

    if os.path.isfile(log_file_path):result = subprocess.run(get_state_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)if result.stderr:logger.error(f"grep 错误: \n {result.stderr}")elif result.returncode == 0 and result.stdout == '':logger.info(f"返回的return code为'',stdout为''")elif result.returncode == 0 and result.stdout != '':logger.info(f"返回的内容不为空,返回result.stdout的内容")get_result = result.stdout

http://www.lryc.cn/news/498932.html

相关文章:

  • 包管理器npm,cnpm,yarn和pnpm
  • 树莓派4B使用opencv读取摄像头配置指南
  • Spring Boot 进阶话题:部署
  • Python 3 和 MongoDB 的集成使用
  • perl语言中模式匹配的左右关系
  • 【漏洞复现】网动统一通信平台(ActiveUC)接口iactiveEnterMeeting存在信息泄露漏洞
  • C++ STL 容器系列(三)list —— 编程世界的万能胶,数据结构中的百变精灵
  • Java经典面试题总结(附答案)2025
  • Stylus 浏览器扩展开发-Cursor AI辅助
  • DAY35|动态规划Part03|LeetCode:01背包问题 二维、01背包问题 一维、416. 分割等和子集
  • 创建空向量:std::vector<int> v,刚创建时大小为0
  • VBA基础2
  • 计算机网络-GRE基础实验二
  • JSON 使用
  • Leetcode—1539. 第 k 个缺失的正整数【简单】
  • 深入浅出:PHP 控制结构与循环语句
  • 深入解析 Loss 减少方式:mean和sum的区别及其在大语言模型中的应用 (中英双语)
  • c++ auto
  • python中的列表、元组、字典的介绍与使用
  • 深入浅出:PHP中的表单处理全解析
  • 双绞线直连两台电脑的方法及遇到的问题
  • 2024年认证杯SPSSPRO杯数学建模D题(第一阶段)AI绘画带来的挑战解题全过程文档及程序
  • Qt 设置QLineEdit控件placeholderText颜色
  • 麒麟 V10 系统(arm64/aarch64)离线安装 docker 和 docker-compose
  • Windows基线自动化检查脚本
  • 离谱的梯形滤波器——增加过渡点
  • tauri下的两个常用rust web框架:Leptos和Trunk
  • pubmed关键词搜索技能1:待更新
  • 【技巧】Mac上如何显示键盘和鼠标操作
  • ISO26262-(Timing Monitoring)在多核MCU的TPU上功能安全ASILB与ASILD有什么区别