# auto-test **Repository Path**: IT_Ruan/auto-test ## Basic Information - **Project Name**: auto-test - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-09-03 - **Last Updated**: 2025-09-07 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # UI自动化测试平台 基于 pytest 和 seleniumbase 的完整 UI 自动化测试平台,支持多浏览器、多环境、数据驱动测试。 ## 🚀 特性 - **Page Object Model (POM)**: 封装页面操作,提高代码复用性和维护性 - **数据驱动测试**: 支持 JSON/YAML 格式的测试数据,使用 pytest.parametrize 实现 - **多浏览器支持**: Chrome、Firefox、Edge、Safari - **多环境配置**: 测试、预生产、生产环境 - **自动截图**: 测试失败时自动截图,支持手动截图和元素截图 - **丰富的报告**: HTML 报告和 Allure 报告 - **并行执行**: 支持多进程并行执行测试 - **失败重试**: 自动重试失败的测试用例 - **日志管理**: 彩色控制台输出和文件日志记录 - **配置管理**: 统一的配置管理,支持环境变量覆盖 ## 📁 项目结构 ``` auto-test/ ├── tests/ # 测试用例目录 │ ├── login/ # 登录模块测试 │ │ └── test_login.py │ └── dashboard/ # 仪表板模块测试 │ └── test_dashboard.py ├── pages/ # 页面对象类 │ ├── base_page.py # 基础页面类 │ ├── login_page.py # 登录页面类 │ └── dashboard_page.py # 仪表板页面类 ├── data/ # 测试数据 │ ├── login_data.json # 登录测试数据 │ ├── dashboard_data.json # 仪表板测试数据 │ └── user_data.json # 用户数据 ├── utils/ # 工具模块 │ ├── logger.py # 日志管理 │ ├── config_manager.py # 配置管理 │ └── screenshot_helper.py # 截图辅助 ├── reports/ # 测试报告 │ └── screenshots/ # 截图目录 ├── conftest.py # pytest 全局配置 ├── config.yaml # 项目配置文件 ├── pytest.ini # pytest 配置 ├── requirements.txt # 依赖包 ├── run_tests.py # 主执行脚本 ├── run_smoke_tests.py # 冒烟测试脚本 └── run_regression_tests.py # 回归测试脚本 ``` ## 🛠️ 安装指南 ### 1. 环境要求 - Python 3.11+ - Chrome/Firefox/Edge 浏览器 - 对应的浏览器驱动(SeleniumBase 会自动管理) ### 2. 安装依赖 ```bash # 克隆项目 git clone cd auto-test # 安装依赖 pip install -r requirements.txt ``` ### 3. 配置设置 编辑 `config.yaml` 文件,配置测试环境: ```yaml environments: test: base_url: "https://your-test-site.com" api_url: "https://api-test.example.com" ``` ## 🎯 使用方法 ### 基本用法 ```bash # 执行所有测试 python run_tests.py # 执行登录模块测试 python run_tests.py --module login # 执行仪表板模块测试 python run_tests.py --module dashboard # 使用 Firefox 浏览器 python run_tests.py --browser firefox # 无头模式执行 python run_tests.py --headless # 指定测试环境 python run_tests.py --env staging ``` ### 高级用法 ```bash # 并行执行(2个进程) python run_tests.py --parallel 2 # 执行冒烟测试 python run_tests.py --markers smoke # 执行回归测试 python run_tests.py --markers regression # 失败重试2次 python run_tests.py --reruns 2 # 生成 Allure 报告 python run_tests.py --report-format allure # 自定义报告名称 python run_tests.py --report-name my_test_report ``` ### 快捷脚本 ```bash # 执行冒烟测试 python run_smoke_tests.py # 执行回归测试 python run_regression_tests.py ``` ### 直接使用 pytest ```bash # 执行所有测试 pytest tests/ # 执行特定标记的测试 pytest -m smoke # 执行特定模块 pytest tests/login/ # 生成 HTML 报告 pytest --html=reports/report.html --self-contained-html # 并行执行 pytest -n 2 # 详细输出 pytest -v -s ``` ## 📊 测试报告 ### HTML 报告 测试完成后,HTML 报告会生成在 `reports/` 目录下: - 包含测试结果统计 - 失败用例的错误信息 - 自动截图链接 - 执行时间统计 ### Allure 报告 ```bash # 生成 Allure 报告 python run_tests.py --report-format allure # 查看 Allure 报告 allure serve reports/allure-results ``` ## 🔧 配置说明 ### 浏览器配置 在 `config.yaml` 中配置浏览器选项: ```yaml browser: default: "chrome" headless: false window_size: "1920,1080" implicit_wait: 10 ``` ### 环境配置 支持多环境配置: ```yaml environments: test: base_url: "https://test.example.com" staging: base_url: "https://staging.example.com" production: base_url: "https://www.example.com" ``` ### 测试配置 ```yaml test: default_env: "test" max_retries: 2 screenshot_on_failure: true parallel_workers: 2 ``` ## 📝 编写测试用例 ### 1. 创建页面对象 ```python from pages.base_page import BasePage from selenium.webdriver.common.by import By class MyPage(BasePage): # 元素定位器 BUTTON = (By.ID, "my-button") def click_button(self): self.click(self.BUTTON) ``` ### 2. 编写测试用例 ```python import pytest from pages import MyPage class TestMyFeature: def test_my_function(self, page_driver): page = MyPage(page_driver) page.open("/my-page") page.click_button() assert page.is_element_visible(page.SUCCESS_MESSAGE) ``` ### 3. 数据驱动测试 ```python @pytest.mark.parametrize("test_data", [ {"input": "value1", "expected": "result1"}, {"input": "value2", "expected": "result2"} ]) def test_with_data(self, page_driver, test_data): # 使用 test_data 进行测试 pass ``` ## 🏷️ 测试标记 项目支持以下测试标记: - `@pytest.mark.smoke`: 冒烟测试 - `@pytest.mark.regression`: 回归测试 - `@pytest.mark.login`: 登录相关测试 - `@pytest.mark.dashboard`: 仪表板相关测试 - `@pytest.mark.slow`: 运行较慢的测试 - `@pytest.mark.critical`: 关键功能测试 ## 🐛 调试技巧 ### 1. 查看详细日志 ```bash python run_tests.py --verbose -s ``` ### 2. 单步调试 在测试代码中添加断点: ```python import pdb; pdb.set_trace() ``` ### 3. 截图调试 ```python def test_debug(self, page_driver): page = MyPage(page_driver) page.take_screenshot("debug_screenshot") ``` ### 4. 保持浏览器打开 在 `conftest.py` 中注释掉 `tearDown()` 调用。 ## 🤝 贡献指南 1. Fork 项目 2. 创建功能分支 3. 提交更改 4. 推送到分支 5. 创建 Pull Request ## 📄 许可证 MIT License ## 📞 支持 如有问题,请提交 Issue 或联系项目维护者。