Skip to content

Commit 6492d6b

Browse files
committed
[Feature]Integration of the pytest testing framework for convenient end-to-end testing, etc., and automatic recording of required data
1 parent 7c8c9a3 commit 6492d6b

File tree

13 files changed

+1024
-1
lines changed

13 files changed

+1024
-1
lines changed

.gitignore

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,15 @@
4949
**/output/**
5050
.venv/**
5151
**/__pycache__/**
52-
*.egg-info/**
52+
*.egg-info/**
53+
reports/
54+
dataset/
55+
logs/
56+
.*
57+
*.log
58+
result_outputs/
59+
results/
60+
.cache/
61+
backup/
62+
$null
63+
*__pycache__/

test/.gitignore

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
reports/
2+
dataset/
3+
logs/
4+
result_outputs/
5+
results/
6+
.cache/
7+
backup/
8+
$null
9+
*__pycache__/
10+
.*
11+
*.log
12+
start.bat
13+
!.gitignore

test/README.md

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
# Pytest
2+
[简体中文](README_zh.md)
3+
A comprehensive Pytest testing framework featuring configuration management, database integration, performance testing, and HTML report generation.
4+
5+
## 📋 Features
6+
7+
- **Modern Testing Framework**: Complete test solution built on Pytest 7.0+
8+
- **Configuration Management**: YAML-based config with thread-safe singleton pattern
9+
- **Database Integration**: Built-in MySQL support with automatic result storage
10+
- **HTML Reports**: Auto-generated pytest HTML test reports
11+
- **Tagging System**: Multi-dimensional test tags (stage, feature, platform, etc.)
12+
13+
## 🗂️ Project Structure
14+
15+
```
16+
pytest_demo/
17+
├── common/ # Common modules
18+
│ ├── __init__.py
19+
│ ├── config_utils.py # Configuration utilities
20+
│ ├── db_utils.py # Database utilities
21+
│ └── capture_utils # Return-value capture utilities
22+
├── results/ # Result storage folder
23+
├── suites/ # Test suites
24+
│ ├── UnitTest # Unit tests
25+
│ ├── Feature # Feature tests
26+
│ └── E2E/ # End-to-end tests
27+
│ └── test_demo_performance.py # Sample test file
28+
├── config.yaml # Main config file
29+
├── conftest.py # Pytest config
30+
├── pytest.ini # Pytest settings
31+
├── requirements.txt # Dependencies
32+
└── README.md # This doc (CN)
33+
```
34+
35+
## 🚀 Quick Start
36+
37+
### Prerequisites
38+
39+
- Python 3.8+
40+
- MySQL 5.7+ (optional, for DB features)
41+
- Git
42+
43+
### Installation
44+
45+
1. **Install dependencies**
46+
```bash
47+
pip install -r requirements.txt
48+
```
49+
50+
2. **Configure database** (optional)
51+
52+
Edit `config.yaml`:
53+
```yaml
54+
database:
55+
backup: "results/"
56+
host: "127.0.0.1"
57+
port: 3306
58+
name: "ucm_pytest"
59+
user: "root"
60+
password: "123456"
61+
charset: "utf8mb4"
62+
```
63+
64+
3. **Run tests**
65+
```bash
66+
# Run all tests
67+
pytest
68+
69+
# Run tests by tag
70+
pytest --stage=1
71+
pytest --feature=performance
72+
```
73+
74+
## ⚙️ Configuration
75+
76+
### config.yaml
77+
78+
Full YAML-based config. Key sections:
79+
80+
- **reports**: Report settings (HTML, timestamp, etc.)
81+
- **database**: MySQL connection details
82+
83+
## 🧪 Test Examples
84+
85+
### Basic functional test
86+
87+
```python
88+
# suites/E2E/test_demo_performance.py
89+
import pytest
90+
91+
@pytest.fixture(scope="module", name="calc")
92+
def calculator():
93+
return Calculator()
94+
95+
@pytest.mark.feature("mark")
96+
class TestCalculator:
97+
def test_add(self, calc):
98+
assert calc.add(1, 2) == 3
99+
100+
def test_divide_by_zero(self, calc):
101+
with pytest.raises(ZeroDivisionError):
102+
calc.divide(6, 0)
103+
```
104+
105+
## 🏷️ Tagging System
106+
107+
Multi-dimensional tags supported:
108+
109+
### Stage tags
110+
- `stage(0)`: Unit tests
111+
- `stage(1)`: Smoke tests
112+
- `stage(2)`: Regression tests
113+
- `stage(3)`: Release tests
114+
115+
### Functional tags
116+
- `feature`: Module tag
117+
- `platform`: Platform tag (GPU/NPU)
118+
119+
### Usage
120+
121+
```bash
122+
# Run smoke tests and above
123+
pytest --stage=1+
124+
125+
# Run by feature
126+
pytest --feature=performance
127+
pytest --feature=performance,reliability
128+
129+
# Run by platform
130+
pytest --platform=gpu
131+
```
132+
133+
### HTML Reports
134+
135+
Auto-generated timestamped HTML reports:
136+
- Location: `reports/pytest_YYYYMMDD_HHMMSS/report.html`
137+
- Detailed results, errors, timing
138+
- Customizable title & style
139+
140+
### Database Storage
141+
142+
If enabled, results are auto-saved to MySQL.
143+
To add new record types, ask DB admin to create tables; otherwise only local files are used.
144+
145+
Example:
146+
```python
147+
@pytest.mark.feature("capture") # Must be top decorator
148+
@export_vars
149+
def test_capture_mix():
150+
assert 1 == 1
151+
return {
152+
'_name': 'demo',
153+
'_data': {
154+
'length': 10086, # single value
155+
'accuracy': [0.1, 0.2, 0.3], # list
156+
'loss': [0.1, 0.2, 0.3], # list
157+
}
158+
}
159+
```
160+
161+
### Config Access
162+
163+
Read settings easily:
164+
```python
165+
from common.config_utils import config_utils
166+
# Get config
167+
db_config = config_utils.get_config("database")
168+
api_config = config_utils.get_nested_config("easyPerf.api")
169+
```
170+
171+
## 🛠️ Development Guide
172+
173+
### Adding New Tests
174+
175+
1. Create test files under `suites/` categories
176+
2. Apply appropriate tags
177+
3. Naming: `test_*.py`
178+
4. Use fixtures & marks for data management
179+
5. Keep custom marks concise and aligned with overall goals

test/README_zh.md

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
# Pytest 项目
2+
Pytest 测试框架,包括配置管理、数据库集成、性能测试和 HTML 报告生成。
3+
4+
## 📋 项目特性
5+
6+
- **现代化测试框架**: 基于 Pytest 7.0+ 的完整测试解决方案
7+
- **配置管理**: 支持 YAML 配置文件,线程安全的单例模式配置管理
8+
- **数据库集成**: 内置 MySQL 数据库支持,自动结果存储
9+
- **HTML 报告**: 自动生成pytest HTML 测试报告
10+
- **标记系统**: 支持多维度测试标记(阶段、功能、平台等)
11+
12+
## 🗂️ 项目结构
13+
14+
```
15+
pytest_demo/
16+
├── common/ # 公共模块
17+
│ ├── __init__.py
18+
│ ├── config_utils.py # 配置管理工具
19+
│ ├── db_utils.py # 数据库工具
20+
│ └── capture_utils # 返回值捕获工具
21+
├── results/ # 结果存储目录
22+
├── suites/ # 测试套件
23+
│ ├── UnitTest # 单元测试
24+
│ ├── Feature # 功能测试
25+
│ └── E2E/ # 端到端测试
26+
│ └── test_demo_performance.py # 示例测试文件
27+
├── config.yaml # 主配置文件
28+
├── conftest.py # Pytest 配置文件
29+
├── pytest.ini # Pytest 配置
30+
├── requirements.txt # 项目依赖
31+
└── README.md # 本文档
32+
```
33+
34+
## 🚀 快速开始
35+
36+
### 环境要求
37+
38+
- Python 3.8+
39+
- MySQL 5.7+ (可选,用于数据库功能)
40+
- Git
41+
42+
### 安装步骤
43+
44+
1. **安装依赖**
45+
```bash
46+
pip install -r requirements.txt
47+
```
48+
49+
2. **配置数据库**(可选)
50+
51+
编辑 `config.yaml` 文件中的数据库配置:
52+
```yaml
53+
database:
54+
backup: "results/"
55+
host: "127.0.0.1"
56+
port: 3306
57+
name: "ucm_pytest"
58+
user: "root"
59+
password: "123456"
60+
charset: "utf8mb4"
61+
```
62+
63+
3. **运行测试**
64+
```bash
65+
# 运行所有测试
66+
pytest
67+
68+
# 运行特定标记的测试
69+
pytest --stage=1
70+
pytest --feature=performance
71+
```
72+
73+
## ⚙️ 配置说明
74+
75+
76+
### config.yaml 配置
77+
78+
项目支持完整的 YAML 配置管理,主要配置项包括:
79+
80+
- **reports**: 报告配置(HTML 报告、时间戳等)
81+
- **database**: 数据库连接配置
82+
83+
## 🧪 测试示例
84+
85+
### 基础功能测试
86+
87+
```python
88+
# suites/E2E/test_demo_performance.py
89+
import pytest
90+
91+
@pytest.fixture(scope="module", name="calc")
92+
def calculator():
93+
return Calculator()
94+
95+
@pytest.mark.feature("mark")
96+
class TestCalculator:
97+
def test_add(self, calc):
98+
assert calc.add(1, 2) == 3
99+
100+
def test_divide_by_zero(self, calc):
101+
with pytest.raises(ZeroDivisionError):
102+
calc.divide(6, 0)
103+
```
104+
105+
## 🏷️ 测试标记系统
106+
107+
项目支持多维度的测试标记:
108+
109+
### 测试阶段标记
110+
- `stage(0)`: 单元测试
111+
- `stage(1)`: 冒烟测试
112+
- `stage(2)`: 回归测试
113+
- `stage(3)`: 发布测试
114+
115+
### 功能标记
116+
- `feature`: 功能模块标记
117+
- `platform`: 平台标记(GPU/NPU)
118+
119+
### 使用示例
120+
121+
```bash
122+
# 运行冒烟测试及以上的所有测试
123+
pytest --stage=1+
124+
125+
# 运行特定功能的测试
126+
pytest --feature=performance
127+
pytest --feature=performance, reliability
128+
# 运行特定平台的测试
129+
pytest --platform=gpu
130+
```
131+
132+
133+
### HTML 报告
134+
135+
项目自动生成带时间戳的 HTML 测试报告:
136+
- 报告位置:`reports/pytest_YYYYMMDD_HHMMSS/report.html`
137+
- 包含详细的测试结果、错误信息和执行时间
138+
- 支持自定义报告标题和样式
139+
140+
### 数据库存储
141+
142+
如果启用数据库功能,测试结果会自动存储到 MySQL 数据库。
143+
若需要新增记录,请联系管理人员在数据库新增对应表;否则只能保存至本地文件。
144+
使用方式示例:
145+
```python
146+
@pytest.mark.feature("capture") # pytest 的标签必须在上面,否则无法正常使用标记功能
147+
@export_vars
148+
def test_capture_mix():
149+
assert 1 == 1
150+
return {
151+
'_name': 'demo',
152+
'_data': {
153+
'length': 10086, # single value
154+
'accuracy': [0.1, 0.2, 0.3], # list
155+
'loss': [0.1, 0.2, 0.3], # list
156+
}
157+
}
158+
159+
```
160+
161+
162+
### 配置管理
163+
164+
可以通过配置工具便捷读取参数:
165+
```python
166+
from common.config_utils import config_utils
167+
# 获取配置
168+
db_config = config_utils.get_config("database")
169+
api_config = config_utils.get_nested_config("easyPerf.api")
170+
```
171+
172+
173+
174+
## 🛠️ 开发指南
175+
176+
### 添加新测试
177+
178+
1.`suites/` 目录下的各个分类下创建新的测试文件
179+
2. 使用适当的测试标记
180+
3. 遵循命名规范:`test_*.py`
181+
4. 使用 fixture 及mark 进行测试数据管理
182+
5. 自定义 mark 标签不易过细,应当与整体功能目标相符合

test/common/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)