|
1 | | -# UCM Pytest Testing Framework |
| 1 | +# Pytest |
| 2 | +[简体中文](README_zh.md) |
| 3 | +A comprehensive Pytest testing framework featuring configuration management, database integration, performance testing, and HTML report generation. |
2 | 4 |
|
3 | | -A unified cache management testing framework based on pytest, supporting multi-level testing, flexible marking, performance data collection, and beautiful Allure report generation. |
| 5 | +## 📋 Features |
4 | 6 |
|
5 | | -## Framework Features |
| 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.) |
6 | 12 |
|
7 | | -- [x] 🏗️ **Multi-level Testing**: UnitTest(0) → Smoke(1) → Feature(2) → E2E(3) |
8 | | -- [x] 🏷️ **Flexible Marking**: Support for feature tags, platform tags, and reliability tags |
9 | | -- [x] 📊 **Data Collection**: Integrated InfluxDB performance data pushing |
10 | | -- [x] 📋 **Beautiful Reports**: Allure test report integration, supporting both static HTML and dynamic server modes |
11 | | -- [x] 🔧 **Configuration Management**: Flexible YAML-based configuration system |
12 | | -- [x] 🚀 **Automation**: Support for parallel test execution and automatic cleanup |
13 | | - |
14 | | -## Test Level Definitions |
15 | | - |
16 | | -| Level | Name | Description | Execution Time | |
17 | | -|-----|------|------|----------| |
18 | | -| 0 | UnitTest | Unit Tests | Every code commit | |
19 | | -| 1 | Smoke | Smoke Tests | Build verification | |
20 | | -| 2 | Feature | Feature Tests | When features are completed | |
21 | | -| 3 | E2E | End-to-End Tests | Before version release | |
22 | | - |
23 | | -## Directory Structure |
| 13 | +## 🗂️ Project Structure |
24 | 14 |
|
25 | 15 | ``` |
26 | | -test/ |
27 | | -├── config.yaml # Test framework configuration file |
28 | | -├── conftest.py # pytest configuration and fixtures, main program entry |
29 | | -├── pytest.ini # pytest markers and basic configuration |
30 | | -├── requirements.txt # Dependency package list |
31 | | -├── common/ # Common utility library |
| 16 | +pytest_demo/ |
| 17 | +├── common/ # Common modules |
32 | 18 | │ ├── __init__.py |
33 | | -│ ├── config_utils.py # Configuration file reading tools |
34 | | -│ ├── influxdb_utils.py # InfluxDB writing tools |
35 | | -│ └── allure_utils.py # Allure reporting tools |
36 | | -├── suites/ # Test case directory |
37 | | -│ ├── UnitTest/ # Unit tests (stage 0) |
38 | | -│ ├── Smoke/ # Smoke tests (stage 1) |
39 | | -│ ├── Feature/ # Feature tests (stage 2) |
40 | | -│ ├── E2E/ # End-to-end tests (stage 3) |
41 | | -│ └── test_demo_function.py# Example test cases |
42 | | -├── reports/ # Test report directory |
43 | | -└── logs/ # Test log directory |
| 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) |
44 | 33 | ``` |
45 | 34 |
|
46 | | -## Quick Start |
| 35 | +## 🚀 Quick Start |
47 | 36 |
|
48 | | -### 1. Environment Setup |
49 | | -```bash |
50 | | -# Install dependencies |
51 | | -pip install -r requirements.txt |
| 37 | +### Prerequisites |
52 | 38 |
|
53 | | -# Ensure Allure CLI is installed (for report generation) |
54 | | -# Download from: https://github.com/allure-framework/allure2/releases |
55 | | -``` |
| 39 | +- Python 3.8+ |
| 40 | +- MySQL 5.7+ (optional, for DB features) |
| 41 | +- Git |
56 | 42 |
|
57 | | -### 2. Configuration File |
58 | | -The main configuration file is `config.yaml`, containing the following configuration items: |
59 | | -- **reports**: Report generation configuration (HTML/Allure) |
60 | | -- **log**: Logging configuration |
61 | | -- **influxdb**: Performance data push configuration |
62 | | -- **llm_connection**: LLM connection configuration |
| 43 | +### Installation |
63 | 44 |
|
64 | | -### 3. Running Tests |
65 | | -```bash |
66 | | -# Run all tests |
67 | | -pytest |
| 45 | +1. **Install dependencies** |
| 46 | + ```bash |
| 47 | + pip install -r requirements.txt |
| 48 | + ``` |
68 | 49 |
|
69 | | -# Run specific level tests |
70 | | -pytest --stage=1 # Run smoke tests |
71 | | -pytest --stage=2+ # Run feature and end-to-end tests |
| 50 | +2. **Configure database** (optional) |
72 | 51 |
|
73 | | -# Run specific tag tests |
74 | | -pytest --feature=performance # Run performance-related tests |
75 | | -pytest --platform=gpu # Run GPU platform tests |
76 | | -pytest --reliability=high # Run high reliability tests |
| 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 | + ``` |
77 | 63 |
|
78 | | -# Combined filtering |
79 | | -pytest --stage=1 --feature=performance,accuracy # Performance and accuracy tests in smoke tests |
80 | | -``` |
| 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 |
81 | 75 |
|
82 | | -## Test Case Standards |
| 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 |
83 | 86 |
|
84 | | -### Basic Structure |
85 | 87 | ```python |
| 88 | +# suites/E2E/test_demo_performance.py |
86 | 89 | import pytest |
87 | | -import allure |
88 | | -from common.config_utils import config_utils as config_instance |
89 | | - |
90 | | -class TestExample: |
91 | | - """Test example class""" |
92 | | - |
93 | | - @pytest.mark.stage(2) |
94 | | - @pytest.mark.feature("performance") |
95 | | - @pytest.mark.platform("gpu") |
96 | | - def test_gpu_performance(self): |
97 | | - """Test GPU performance""" |
98 | | - # Arrange |
99 | | - test_data = config_instance.get_config("test_data") |
100 | | - |
101 | | - # Act & Assert |
102 | | - with allure.step("Execute GPU computation"): |
103 | | - result = perform_gpu_calculation(test_data) |
104 | | - assert result.is_valid |
105 | | - |
106 | | - # Collect performance data |
107 | | - from common.influxdb_utils import push_to_influx |
108 | | - push_to_influx("gpu_compute_time", result.duration, { |
109 | | - "test_name": "test_gpu_performance", |
110 | | - "platform": "gpu" |
111 | | - }) |
112 | | -``` |
113 | 90 |
|
114 | | -### Marking Usage Guidelines |
| 91 | +@pytest.fixture(scope="module", name="calc") |
| 92 | +def calculator(): |
| 93 | + return Calculator() |
115 | 94 |
|
116 | | -#### 1. Level Markers (Required) |
117 | | -```python |
118 | | -@pytest.mark.stage(0) # Unit tests |
119 | | -@pytest.mark.stage(1) # Smoke tests |
120 | | -@pytest.mark.stage(2) # Feature tests |
121 | | -@pytest.mark.stage(3) # End-to-end tests |
122 | | -``` |
| 95 | +@pytest.mark.feature("mark") |
| 96 | +class TestCalculator: |
| 97 | + def test_add(self, calc): |
| 98 | + assert calc.add(1, 2) == 3 |
123 | 99 |
|
124 | | -#### 2. Feature Markers (Recommended) |
125 | | -```python |
126 | | -@pytest.mark.feature("performance") # Performance tests |
127 | | -@pytest.mark.feature("accuracy") # Accuracy tests |
128 | | -@pytest.mark.feature("memory") # Memory tests |
| 100 | + def test_divide_by_zero(self, calc): |
| 101 | + with pytest.raises(ZeroDivisionError): |
| 102 | + calc.divide(6, 0) |
129 | 103 | ``` |
130 | 104 |
|
131 | | -#### 3. Platform Markers (Optional) |
132 | | -```python |
133 | | -@pytest.mark.platform("gpu") # GPU platform tests |
134 | | -@pytest.mark.platform("npu") # NPU platform tests |
135 | | -@pytest.mark.platform("cpu") # CPU platform tests |
136 | | -``` |
| 105 | +## 🏷️ Tagging System |
137 | 106 |
|
138 | | -#### 4. Reliability Markers (Optional) |
139 | | -```python |
140 | | -@pytest.mark.reliability("high") # High reliability tests |
141 | | -@pytest.mark.reliability("medium") # Medium reliability tests |
142 | | -@pytest.mark.reliability("low") # Low reliability tests |
143 | | -``` |
| 107 | +Multi-dimensional tags supported: |
144 | 108 |
|
145 | | -## Allure Report Integration |
| 109 | +### Stage tags |
| 110 | +- `stage(0)`: Unit tests |
| 111 | +- `stage(1)`: Smoke tests |
| 112 | +- `stage(2)`: Regression tests |
| 113 | +- `stage(3)`: Release tests |
146 | 114 |
|
147 | | -### 1. Basic Usage |
148 | | -```python |
149 | | -import allure |
150 | | - |
151 | | -@allure.feature('User Authentication') |
152 | | -@allure.story('Login Function') |
153 | | -def test_user_login(): |
154 | | - """Test user login functionality""" |
155 | | - with allure.step("Enter username and password"): |
156 | | - login_page.enter_credentials("user", "pass") |
157 | | - |
158 | | - with allure.step("Click login button"): |
159 | | - login_page.click_login() |
160 | | - |
161 | | - with allure.step("Verify successful login"): |
162 | | - assert dashboard_page.is_displayed() |
163 | | - |
164 | | - # Add attachment |
165 | | - allure.attach("Screenshot data", name="Login Screenshot", |
166 | | - attachment_type=allure.attachment_type.PNG) |
167 | | -``` |
| 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 |
168 | 128 |
|
169 | | -### 2. Report Configuration |
170 | | -Configure Allure reports in `config.yaml`: |
171 | | -```yaml |
172 | | -reports: |
173 | | - allure: |
174 | | - enabled: true |
175 | | - html_enable: true |
176 | | - serve_mode: true # Use dynamic server mode |
177 | | - serve_host: "localhost" |
178 | | - serve_port: 8081 |
179 | | - directory: "allure-results" |
| 129 | +# Run by platform |
| 130 | +pytest --platform=gpu |
180 | 131 | ``` |
181 | 132 |
|
182 | | -### 3. Report Viewing |
183 | | -- **Static HTML Mode**: Automatically generates static HTML reports after test completion |
184 | | -- **Dynamic Server Mode**: Starts Allure server, providing interactive report interface |
| 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 | +``` |
185 | 160 |
|
186 | | -## Performance Data Collection |
| 161 | +### Config Access |
187 | 162 |
|
188 | | -### InfluxDB Integration |
| 163 | +Read settings easily: |
189 | 164 | ```python |
190 | | -from common.influxdb_utils import push_to_influx |
191 | | - |
192 | | -# Collect performance data in tests |
193 | | -def test_performance_metrics(): |
194 | | - start_time = time.time() |
195 | | - |
196 | | - # Execute test logic |
197 | | - result = perform_operation() |
198 | | - |
199 | | - # Push performance data to InfluxDB |
200 | | - push_to_influx("operation_duration", time.time() - start_time, { |
201 | | - "test_name": "test_performance_metrics", |
202 | | - "operation_type": "calculation", |
203 | | - "success": str(result.success) |
204 | | - }) |
| 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") |
205 | 169 | ``` |
206 | 170 |
|
207 | | -## Extensions and Customization |
| 171 | +## 🛠️ Development Guide |
208 | 172 |
|
209 | | -### Adding New Markers |
210 | | -1. Add new marker definitions in the `markers` section of `pytest.ini` |
211 | | -2. Keep the `markers =` and `# end of markers` lines unchanged |
212 | | -3. Re-run tests to use new markers |
| 173 | +### Adding New Tests |
213 | 174 |
|
214 | | -### Custom Configuration |
215 | | -Customize through `config.yaml`: |
216 | | -- Report format and storage location |
217 | | -- Log level and output format |
218 | | -- InfluxDB connection parameters |
219 | | -- LLM service configuration |
| 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 |
0 commit comments