软件测试如何抓取log

时间:2025-01-27 09:55:45 主机游戏

软件测试中抓取log的方法主要有以下几种:

使用pytest-log插件

安装:使用pip安装pytest-log插件。

示例代码:

```python

test_calculator.py

import logging

logger = logging.getLogger(__name__)

def calculate_sum(a, b):

logger.info(f"计算 {a} + {b} 的和")

result = a + b

logger.debug(f"计算结果: {result}")

return result

def test_calculate_sum(caplog):

result = calculate_sum(3, 5)

assert result == 8

assert "计算 3 + 5 的和" in caplog.text

assert "计算结果: 8" in caplog.text

```

小贴士:caplog夹具会自动捕获测试函数执行期间的所有日志记录,不需要额外的配置。

使用ADB命令

通过USB连接手机:确保手机已开启USB调试模式,并通过USB连接到电脑。

抓取不同级别的日志

radio日志:`adb logcat -b radio -v time > log_radio.txt`

main日志:`adb logcat -b main -v time > log_main.txt`

events日志:`adb logcat -b events -v time > log_events.txt`

实时抓取日志

清除旧日志:`adb logcat -c`

开始抓取:`adb logcat -v time > logcat.log`

结束抓取:按Ctrl+C

查看日志文件:在命令行中查看生成的log文件,例如:`type logcat.log`

使用Android设备上的开发者选项

进入Engineer Mode:通过拨号界面输入指令`**3646633**`。

选择MTKLogger并开启log。

使用命令行工具

查看日志文件内容

`cat /etc/spring.log`

`tail /etc/spring.log`

`tac /etc/spring.log`

建议

选择合适的方法:根据测试环境和需求选择最合适的抓取方法。

自动化测试:在自动化测试中,可以使用pytest-log插件简化日志捕获过程。

跨平台:如果需要跨平台抓取日志,可以考虑使用ADB命令,因为它适用于Android设备。