在本教程中,我们将展示如何使用 pytest
记录日志信息并生成包含当前日期和时间的日志文件和 HTML 报告。我们将以 WiFi 和蓝牙功能测试为例。
步骤 1:安装必要的包
首先,确保已安装必要的 Python 包:
pip install pytest pytest-html
步骤 2:创建测试脚本
创建一个名为 test_BTWifi.py
的测试脚本,其中包含 WiFi 和蓝牙的测试用例。
import subprocess
import time
import logging
import pytest
def run_adb_command(command):
"""运行 ADB 命令并返回输出。"""
try:
logging.info(f"执行命令: {
command}")
result = subprocess.run(command, shell=True, capture_output=True, text=True, encoding='utf-8', errors='ignore')
output = result.stdout.strip() if result.stdout else None
logging.info(f"命令输出: {
output}")
return output
except Exception as e:
logging.error(f"执行命令 '{
command}' 时出错: {
e}