Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,15 @@
**/output/**
.venv/**
**/__pycache__/**
*.egg-info/**
*.egg-info/**
reports/
dataset/
logs/
.*
*.log
result_outputs/
results/
.cache/
backup/
$null
*__pycache__/
13 changes: 13 additions & 0 deletions test/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
reports/
dataset/
logs/
result_outputs/
results/
.cache/
backup/
$null
*__pycache__/
.*
*.log
start.bat
!.gitignore
179 changes: 179 additions & 0 deletions test/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
# Pytest
[简体中文](README_zh.md)
A comprehensive Pytest testing framework featuring configuration management, database integration, performance testing, and HTML report generation.

## 📋 Features

- **Modern Testing Framework**: Complete test solution built on Pytest 7.0+
- **Configuration Management**: YAML-based config with thread-safe singleton pattern
- **Database Integration**: Built-in MySQL support with automatic result storage
- **HTML Reports**: Auto-generated pytest HTML test reports
- **Tagging System**: Multi-dimensional test tags (stage, feature, platform, etc.)

## 🗂️ Project Structure

```
pytest_demo/
├── common/ # Common modules
│ ├── __init__.py
│ ├── config_utils.py # Configuration utilities
│ ├── db_utils.py # Database utilities
│ └── capture_utils # Return-value capture utilities
├── results/ # Result storage folder
├── suites/ # Test suites
│ ├── UnitTest # Unit tests
│ ├── Feature # Feature tests
│ └── E2E/ # End-to-end tests
│ └── test_demo_performance.py # Sample test file
├── config.yaml # Main config file
├── conftest.py # Pytest config
├── pytest.ini # Pytest settings
├── requirements.txt # Dependencies
└── README.md # This doc (CN)
```

## 🚀 Quick Start

### Prerequisites

- Python 3.8+
- MySQL 5.7+ (optional, for DB features)
- Git

### Installation

1. **Install dependencies**
```bash
pip install -r requirements.txt
```

2. **Configure database** (optional)

Edit `config.yaml`:
```yaml
database:
backup: "results/"
host: "127.0.0.1"
port: 3306
name: "ucm_pytest"
user: "root"
password: "123456"
charset: "utf8mb4"
```

3. **Run tests**
```bash
# Run all tests
pytest

# Run tests by tag
pytest --stage=1
pytest --feature=performance
```

## ⚙️ Configuration

### config.yaml

Full YAML-based config. Key sections:

- **reports**: Report settings (HTML, timestamp, etc.)
- **database**: MySQL connection details

## 🧪 Test Examples

### Basic functional test

```python
# suites/E2E/test_demo_performance.py
import pytest

@pytest.fixture(scope="module", name="calc")
def calculator():
return Calculator()

@pytest.mark.feature("mark")
class TestCalculator:
def test_add(self, calc):
assert calc.add(1, 2) == 3

def test_divide_by_zero(self, calc):
with pytest.raises(ZeroDivisionError):
calc.divide(6, 0)
```

## 🏷️ Tagging System

Multi-dimensional tags supported:

### Stage tags
- `stage(0)`: Unit tests
- `stage(1)`: Smoke tests
- `stage(2)`: Regression tests
- `stage(3)`: Release tests

### Functional tags
- `feature`: Module tag
- `platform`: Platform tag (GPU/NPU)

### Usage

```bash
# Run smoke tests and above
pytest --stage=1+

# Run by feature
pytest --feature=performance
pytest --feature=performance,reliability

# Run by platform
pytest --platform=gpu
```

### HTML Reports

Auto-generated timestamped HTML reports:
- Location: `reports/pytest_YYYYMMDD_HHMMSS/report.html`
- Detailed results, errors, timing
- Customizable title & style

### Database Storage

If enabled, results are auto-saved to MySQL.
To add new record types, ask DB admin to create tables; otherwise only local files are used.

Example:
```python
@pytest.mark.feature("capture") # Must be top decorator
@export_vars
def test_capture_mix():
assert 1 == 1
return {
'_name': 'demo',
'_data': {
'length': 10086, # single value
'accuracy': [0.1, 0.2, 0.3], # list
'loss': [0.1, 0.2, 0.3], # list
}
}
```

### Config Access

Read settings easily:
```python
from common.config_utils import config_utils
# Get config
db_config = config_utils.get_config("database")
api_config = config_utils.get_nested_config("easyPerf.api")
```

## 🛠️ Development Guide

### Adding New Tests

1. Create test files under `suites/` categories
2. Apply appropriate tags
3. Naming: `test_*.py`
4. Use fixtures & marks for data management
5. Keep custom marks concise and aligned with overall goals
182 changes: 182 additions & 0 deletions test/README_zh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
# Pytest 项目
Pytest 测试框架,包括配置管理、数据库集成、性能测试和 HTML 报告生成。

## 📋 项目特性

- **现代化测试框架**: 基于 Pytest 7.0+ 的完整测试解决方案
- **配置管理**: 支持 YAML 配置文件,线程安全的单例模式配置管理
- **数据库集成**: 内置 MySQL 数据库支持,自动结果存储
- **HTML 报告**: 自动生成pytest HTML 测试报告
- **标记系统**: 支持多维度测试标记(阶段、功能、平台等)

## 🗂️ 项目结构

```
pytest_demo/
├── common/ # 公共模块
│ ├── __init__.py
│ ├── config_utils.py # 配置管理工具
│ ├── db_utils.py # 数据库工具
│ └── capture_utils # 返回值捕获工具
├── results/ # 结果存储目录
├── suites/ # 测试套件
│ ├── UnitTest # 单元测试
│ ├── Feature # 功能测试
│ └── E2E/ # 端到端测试
│ └── test_demo_performance.py # 示例测试文件
├── config.yaml # 主配置文件
├── conftest.py # Pytest 配置文件
├── pytest.ini # Pytest 配置
├── requirements.txt # 项目依赖
└── README.md # 本文档
```

## 🚀 快速开始

### 环境要求

- Python 3.8+
- MySQL 5.7+ (可选,用于数据库功能)
- Git

### 安装步骤

1. **安装依赖**
```bash
pip install -r requirements.txt
```

2. **配置数据库**(可选)

编辑 `config.yaml` 文件中的数据库配置:
```yaml
database:
backup: "results/"
host: "127.0.0.1"
port: 3306
name: "ucm_pytest"
user: "root"
password: "123456"
charset: "utf8mb4"
```

3. **运行测试**
```bash
# 运行所有测试
pytest

# 运行特定标记的测试
pytest --stage=1
pytest --feature=performance
```

## ⚙️ 配置说明


### config.yaml 配置

项目支持完整的 YAML 配置管理,主要配置项包括:

- **reports**: 报告配置(HTML 报告、时间戳等)
- **database**: 数据库连接配置

## 🧪 测试示例

### 基础功能测试

```python
# suites/E2E/test_demo_performance.py
import pytest

@pytest.fixture(scope="module", name="calc")
def calculator():
return Calculator()

@pytest.mark.feature("mark")
class TestCalculator:
def test_add(self, calc):
assert calc.add(1, 2) == 3

def test_divide_by_zero(self, calc):
with pytest.raises(ZeroDivisionError):
calc.divide(6, 0)
```

## 🏷️ 测试标记系统

项目支持多维度的测试标记:

### 测试阶段标记
- `stage(0)`: 单元测试
- `stage(1)`: 冒烟测试
- `stage(2)`: 回归测试
- `stage(3)`: 发布测试

### 功能标记
- `feature`: 功能模块标记
- `platform`: 平台标记(GPU/NPU)

### 使用示例

```bash
# 运行冒烟测试及以上的所有测试
pytest --stage=1+

# 运行特定功能的测试
pytest --feature=performance
pytest --feature=performance, reliability
# 运行特定平台的测试
pytest --platform=gpu
```


### HTML 报告

项目自动生成带时间戳的 HTML 测试报告:
- 报告位置:`reports/pytest_YYYYMMDD_HHMMSS/report.html`
- 包含详细的测试结果、错误信息和执行时间
- 支持自定义报告标题和样式

### 数据库存储

如果启用数据库功能,测试结果会自动存储到 MySQL 数据库。
若需要新增记录,请联系管理人员在数据库新增对应表;否则只能保存至本地文件。
使用方式示例:
```python
@pytest.mark.feature("capture") # pytest 的标签必须在上面,否则无法正常使用标记功能
@export_vars
def test_capture_mix():
assert 1 == 1
return {
'_name': 'demo',
'_data': {
'length': 10086, # single value
'accuracy': [0.1, 0.2, 0.3], # list
'loss': [0.1, 0.2, 0.3], # list
}
}

```


### 配置管理

可以通过配置工具便捷读取参数:
```python
from common.config_utils import config_utils
# 获取配置
db_config = config_utils.get_config("database")
api_config = config_utils.get_nested_config("easyPerf.api")
```



## 🛠️ 开发指南

### 添加新测试

1. 在 `suites/` 目录下的各个分类下创建新的测试文件
2. 使用适当的测试标记
3. 遵循命名规范:`test_*.py`
4. 使用 fixture 及mark 进行测试数据管理
5. 自定义 mark 标签不易过细,应当与整体功能目标相符合
Empty file added test/common/__init__.py
Empty file.
Loading