# STM32自动化测试框架 **Repository Path**: yangdaying/STM32_auto_test ## Basic Information - **Project Name**: STM32自动化测试框架 - **Description**: - 实现串口通信基础类 (utils/serial_comm.py) - 实现设备抽象层 (src/board.py) - 添加基本 LED 功能测试(开关、翻转、多次翻转) - 添加压力测试和长稳测试 - 添加异常命令测试 - 集成 pytest fixture 和 conftest - 准备日志和 Allure 报告(后续完善) - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-05-21 - **Last Updated**: 2026-07-20 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # STM32 嵌入式自动化测试框架 基于 `pytest` 、 `pyserial` 和 `allure`的 STM32 自动化测试框架,用于对嵌入式设备(如 STM32F103 开发板)进行功能验证、压力测试、长稳测试等。 ## 📌 项目简介 本项目针对 STM32 开发板实现了一套完整的自动化测试方案: - 通过串口命令控制 LED 的亮灭、翻转、状态查询。 - 使用 `pytest` 组织测试用例,支持参数化、标记(冒烟/基础/压力)、日志记录。 - 采用分层架构:串口通信层、协议命令层、设备抽象层,易于扩展至其他外设。 ## 🛠 硬件环境 - **主控芯片**:STM32F103C8T6(Blue Pill 开发板) - **外设**:LED(高电平点亮,接 PB0)、按键KEY2(使用内部上拉电阻,接PB13) - **连接方式**:USB 转串口(CH340/CP2102 等) ## 💻 软件环境 - **操作系统**:Windows 10 - **开发工具**:STM32CubeIDE 1.19.0 - **Python 版本**:3.8+ - **Python 依赖**:pytest, pyserial, pytest-html, allure-pytest ## 📁 项目结构 ``` stm32_auto_test/ ├── firmware/ # STM32 固件工程(CubeIDE 项目) │ ├── Core/ │ ├── Drivers/ │ └── ... ├── src/ # Python 设备抽象层 │ ├── board.py # STM32Board 类,封装 LED 控制命令 │ └── init.py ├── utils/ # 通信与辅助工具 │ ├── serial_comm.py # 串口封装(带超时/重试) │ ├── commands.py # 协议命令定义 │ └── logger.py # 日志模块(可选) ├── tests/ # pytest 测试用例 │ ├── test_led_basic.py # 基础功能测试 │ ├── test_led_stress.py # 压力与长稳测试 │ └── test_led_robustness.py# 异常输入测试 ├── logs/ # 运行时日志目录(自动生成) ├── reports/ # 测试报告目录(自动生成) ├── conftest.py # 共享 fixture(board, reset_led) ├── requirements.txt # Python 依赖 ├── pytest.ini # pytest 配置 └── README.md ``` ## 🚀 快速开始 ### 1. 烧录固件 - 使用 STM32CubeIDE 打开 `firmware/` 目录下的工程。 - 编译并烧录到开发板。 - 确保串口驱动已安装,设备管理器中识别到 COM 口(如 `COM3`)。 ### 2. 安装 Python 依赖 ```bash cd stm32_auto_test pip install -r requirements.txt ``` ### 3. 修改串口号 在 tests\conftest.py 中,修改 SERIAL_PORT = "COM3" ,将 COM3 改为你的实际串口号。 ### 4. 运行测试 ```bash # 运行全部测试 pytest .\tests\ -v # 仅运行基础功能测试 pytest .\tests\test_led_basic.py -v # 运行压力测试 pytest .\tests\test_led_stress.py -v # 生成 allure 报告 pytest -vs .\tests\test_led_basic.py --alluredir=.\reports --clean-alluredir # 查看 allure 报告 allure serve .\reports ``` ## 🧪 测试用例说明 |测试类型| 文件| 说明| |---|---|---| |基础功能 |test_led_basic.py |状态查询、翻转、直接设置亮灭、多次交替| |压力测试 |test_led_stress.py |快速连续翻转(500次)、长时间交替(5分钟)| |鲁棒性 |test_led_robustness.py |非法命令、串口异常等| ## 📊 测试报告 - 控制台实时输出测试结果。 - 使用 pytest-html 生成 HTML 报告:pytest .\tests\ --html=reports\report.html - 使用 Allure 生成并查看报告: - 安装 allure-pytest - 生成富图形报告: pytest -vs .\tests\test_led_basic.py --color=no --alluredir=.\reports --clean-alluredir - 查看allure报告:allure serve .\reports - **测试报告示例图** ![Allure测试报告](https://gitee.com/yangdaying/STM32_auto_test/raw/main/data/allure_report.PNG) ## 🤝 扩展指南 添加新硬件(如温湿度传感器): 1. 在 utils\commands.py 增加命令码。 2. 在 src\board.py 中添加新方法(如 read_temperature())。 3. 在 tests\ 下新建测试文件 test_sensor.py,复用 board fixture。 ## 📝 注意事项 - 运行测试前请确保串口未被其他程序(如串口助手、CubeIDE 调试)占用。 - 用于测试的固件必须支持以下串口命令,可参见固件代码 firmware\key\Core\Src\main.c: - t - 翻转 LED 并返回状态 - q - 查询当前 LED 状态 - 1 / 0 - 强制点亮/熄灭 LED - 如果 LED 电路是高电平点亮,请确保固件中状态判断逻辑正确。